Thursday, 9 December 2021

Spy Detected Problem !!!

 PROBLEM 35: Spy Detected!


Problem Reference: Codeforces


You are given an array a consisting of n (n3) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array [4,11,4,4] all numbers except one are equal to 4).

Print the index of the element that does not equal others. The numbers in the array are numbered from one.


Input:

The first line contains a single integer t (1t100). Then t test cases follow.

The first line of each test case contains a single integer n (3n100) — the length of the array a.

The second line of each test case contains n integers a1,a2,,an (1ai100).

It is guaranteed that all the numbers except one in the a array are the same.


Output:

For each test case, output a single integer — the index of the element that is not equal to others.


Solution: 


  1. t = int(input())
  2. i = 0
  3. while i < t:
  4. n = int(input())
  5. a = [int(a) for a in input().split()]
  6. j = 1
  7. while j <= len(a):
  8. if a[j] == a[j-1]:
  9. j = j + 1
  10. else:
  11. break
  12. if j == 1 and a[j] == a[j+1]:
  13. print(j)
  14. else:
  15. print(j+1)
  16. i = i + 1


The above solution is in python langauge


Example
input
4
4
11 13 11 11
5
1 4 4 4 4
10
3 3 3 3 10 3 3 3 3 3
3
20 20 10
output
2
1
5
3

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...