Can someone help me solve this problem in C#?

Uros Zeradjanin 0 Reputation points
2024-10-05T15:14:35.93+00:00

There is a number 1 written on the board. We have a sequence a of n natural numbers, and in the i-th step (1 ≤ i ≤ n), we erase the current number on the board and replace it with the product of that number and the number a[i]. After each step, determine whether the current number on the board is a perfect square.

Input

The first line of standard input contains a natural number n (1 ≤ n ≤ 10,000) representing the length of the sequence a. The next line contains n natural numbers (between 1 and 1 billion) separated by spaces, which are the elements of the sequence a in the order they are multiplied by the current number on the board.

Output

For each element of the sequence a, in the order of input, print yes if its product with the current number on the board is a perfect square, otherwise print no. Time limit is 0,1 seconde

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 23,700 Reputation points MVP
    2024-10-05T21:02:30.02+00:00

    To solve this problem, you need to keep track of the number on the board as it is updated by multiplying it with each element from the sequence a, and after each step, we must check if the resulting number is a perfect square.

    Pseudocode:

    1. Read the input.
    2. Start with the number 1 on the board.
    3. For each element in the sequence, multiply it with the current number on the board and check if the result is a perfect square.
    4. Output "yes" or "no" based on whether the current product is a perfect square.
    using System;
    class PerfectSquareChecker
    {
        // Helper function to check if a number is a perfect square
        static bool IsPerfectSquare(long x)
        {
            if (x < 0) return false;
            long sqrtX = (long)Math.Sqrt(x);
            return sqrtX * sqrtX == x;
        }
        static void Main()
        {
            // Read the input
            int n = int.Parse(Console.ReadLine());  // Number of elements in the sequence
            string[] input = Console.ReadLine().Split();  // Sequence of numbers as strings
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = long.Parse(input[i]);  // Parse each element into a long array
            }
            // Start with 1 on the board
            long currentNumber = 1;
            // Process each number in the sequence
            for (int i = 0; i < n; i++)
            {
                currentNumber *= a[i];  // Multiply current number with a[i]
                
                // Check if the current number is a perfect square
                if (IsPerfectSquare(currentNumber))
                {
                    Console.WriteLine("yes");
                }
                else
                {
                    Console.WriteLine("no");
                }
            }
        }
    }
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.