[Solution] Geometric Mean Inequality solution codechef

Geometric Mean Inequality solution codechef – You are given an array AA of length NN containing the elements 1−1 and 11 only. Determine if it is possible to rearrange the array AA in such a way that AiAi is not the geometric mean of Ai1Ai−1 and Ai+1Ai+1, for all ii such that 2iN12≤i≤N−1.

[Solution] Geometric Mean Inequality solution codechef

YY is said to be the geometric mean of XX and ZZ if Y2=XZY2=X⋅Z.

Input Format

  • The first line contains a single integer TT – the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer NN – the size of the array AA.
  • The second line of each test case contains NN space-separated integers A1,A2,,ANA1,A2,…,AN denoting the array AA.

Output Format

For each test case, output Yes if it is possible to rearrange AA in such a way that AiAi is not the geometric mean of Ai1Ai−1 and Ai+1Ai+1, where 2iN12≤i≤N−1. Otherwise output No.

You may print each character of Yes and No in uppercase or lowercase (for example, yesyEsYES will be considered identical).


  • 1T2001≤T≤200
  • 3N10003≤N≤1000
  • Ai{1,1}Ai∈{−1,1}

[Solution] Geometric Mean Inequality solution codechef


1 1 1 -1 -1
1 1 1
1 -1 -1 -1 -1 1

Sample Output 1 


Geometric Mean Inequality solution Explanation

Test case 1: We can rearrange the array AA to [1,1,1,1,1][1,1,−1,−1,1]. One can see that Ai2Ai1Ai+1Ai2≠Ai−1⋅Ai+1, for any 2iN12≤i≤N−1.

Test case 2: None of the rearrangements of AA satisy the given condition.

For Solution

Click here

Leave a Comment