Based on the problem in GeeksForGeeks here. I came across a solution here.
Can someone please help me understand the solution. Primarily I need help in the following block:
if(n==1) cout<<arr[0]<<endl;
else if(n%2) {
ll ind = n-3;
ind = floor(ind/4);
ind = 3+ind;
cout<<arr[ind-1]<<endl;
} else {
ll ind = n-2;
ind = floor(ind/4);
ind = 2+ind;
cout<<arr[ind-1]<<endl;
}
For each size of array, a particular position is the answer (i.e .independent of array elements).
For any array of size 8, 2 nd position (i.e. 3rd element) gives the answer.
Lets look at some examples:
And so on.
For even size : floor( (n-3)/4 )+2
gives the position.
For odd size : floor( (n-2)/4 )+1
gives the position.
For even size : floor( (n-3)/4 )+2
gives the position.
For odd size : floor( (n-2)/4 )+1
gives the position.
CORRECT:
For Odd size : floor( (n-3)/4 )+2
gives the position.
For Even size : floor( (n-2)/4 )+1
gives the position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With