‘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.
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
We can simply iterate through all the elements and check for the difference between the current index and the previous index. If there’s a fake coin the difference will be 0. If the first element of ‘sum’ is 0 then the fake coin is at index 0.
Algorithm:
If the fake coin is present in range [l,r], then ‘sum[r] - sum[l-1] < r-l+1’. We can use this fact to binary search over the array ‘sum’. Let ‘m = (r+l)/2 ‘.If ‘sum[m+1] == sum[m]’ the fake coin is at index ‘m+1’ . If ‘sum[r] - sum[m] < r-m’, we binary search in range [m+1,r], otherwise we binary search in range [l,m].
Element Count in Ranges
First Digit One
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store