Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given an array ‘sum’ which is the prefix sum of an array of coins ‘C’ where ‘C[i]’ is ‘1’ if the coin is real, or ‘0’ if the coin is fake. There is exactly one fake coin in the array.

Return the index of the fake coin. Assume the array to be 0-indexed.

For Example:

```
‘sum’ = {1,1,2,3}
Index of the fake coin is 1.
For the given ‘sum’, ‘C’ will be {1, 0, 1, 1}. Thus the index of the fake coin is 1.
```

Detailed explanation

```
The first line contains an integer ‘T’, denoting the number of test cases.
For each test case:
The first line contains an integer ‘N’, representing the size of the array ‘sum’.
The second line contains ‘N’ integers, denoting the elements of the array 'sum'.
```

```
For each testcase, return the index of the fake coin.
```

```
1 <= ‘T’ <= 10^5
2 <= ‘N’ <= 10^5
0 <= ‘C[i]’ <= 1
0 <= ‘sum[i]’ <= ‘N-1’
Sum of ‘N’ for all testcases <= 10^5
There is exactly one ‘0’ in ‘C’.
Time Limit: 1 sec
```

```
2
4
1 2 2 3
5
1 2 3 4 4
```

```
2
4
```

```
For Test 1:
‘sum’ = {1, 2, 2, 3}
‘C’ = {1, 1, 0, 1}
Index of the fake coin is 2.
For Test 2:
‘sum' = {1, 2, 3, 4, 4}
‘C’ = {1, 1, 1, 1, 0}
Index of the fake coin is 4.
```

```
3
3
1 2 2
3
1 1 2
3
0 1 2
```

```
2
1
0
```