Help... getting WA in beautiful arrays question!!!

The beautiful arrays question says that :-
An array a is called beautiful if for every pair of numbers ai, aj, (i ≠ j), there exists an ak such that ak = ai * aj. Note that k can be equal to i or j too.

Find out whether the given array a is beautiful or not!
Input Format:- First line of the input contains an integer T denoting the number of test cases. T test cases follow.

First line of each test case contains an integer n denoting number of elements in a.

Next line contains n space separated integers denoting the array a.

My code is this:-import java.util.;
import java.lang.


class Array{

public static void main(String[] args){

  Scanner scan = new Scanner(;

  long x;

  int t = scan.nextInt();

  for(int i=0; i<t; i++) {int count=0;

     int n= scan.nextInt();

      for(int j=0; j<n; j++) {


         if(x!=0 && x!=1) {count++;}
      if(count>1 ) System.out.println("no");

      else System.out.println("yes");





I am not able to find the bug in the code… please help!!!

Okey… You are missing an important case, the -1’s… This array is beauty too {-1, -1, 1} and it has two digits different to 1 and 0… You have to be careful with it because for example {-1, -1}…

Try it again, I think you can solve it with this

Some things you need to consider are-

  1. An array of size 1 is beautiful by definition (give forums a search, its clarified by admin)
  2. If array has 2 or more elements such that abs(a[i])>1 , its NOT beautiful.
  3. Now the Q narrows down to array containing elements {1,0,-1}.
  4. There are some around 7-9 cases. Array of only 0, array of 0 and 1, array of 0,1 and -1, array of only 1…and so on. Take pen and paper and see how you should make condition so taht your code is able to find if array is beautiful or not.
  5. If problem persists, get back to us here ^^

thnx I solved the problem… slight error in observation!!!