like suppose the array is 2-1-1-3-5.
so the input will be 2 and we have to return 4.
explanation : 2-2=0
1-2 = -1 which is less than zero
1-2 = -1 which is less than zero
3-2 = 1 so we do sum+=1;
5-2 = 3 so we do sum+=3;
and return sum=4;
like suppose the array is 2-1-1-3-5.
so the input will be 2 and we have to return 4.
explanation : 2-2=0
1-2 = -1 which is less than zero
1-2 = -1 which is less than zero
3-2 = 1 so we do sum+=1;
5-2 = 3 so we do sum+=3;
and return sum=4;
@priyamthakuria27 You can use binary search to find the number of elements lesser than a particular value. Its pretty straight forward.
i did not understand.
did you understand my question?
given vakue : 1. sum of elements of the array
2. number of elements of the array
3. key (2 in this case)
sample test case : 2-1-1-3-5
sum_of_elements : 12
no_of_elements : 5
key : 2
output : 4
the complexity to be solved in is O(1)
i could not frame the doubt.
yes, please??
@priyamthakuria27 It’s quiet simple.
See the sum of all elements except that element will be known to you. Just subtract away the value of the element n-1 times from the total sum except the current value. You’ll get the answer in 0(1) time
except which element?
Except two in this case. Just use prefix sum.
you got it wrong. take this test case
sample test case : 3-1-1-3-5
sum_of_elements : 12
no_of_elements : 5
key : 2
output : 5
No I got it correct. See it’s simple. Can you call me at 7070110885. I’ll explain it in detail
10 mins. thank you so much