Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array rotate and delete

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;
}
like image 365
bitan Avatar asked Jan 27 '23 08:01

bitan


2 Answers

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:

  • size=1, position=0

  • size=2, position=1
  • size=3, position=2
  • size=4, position=1
  • size=5, position=2

  • size=6, position=2
  • size=7, position=3
  • size=8, position=2
  • size=9, position=3

  • size=10, position=3
  • size=11, position=4
  • size=12, position=3
  • size=13, position=4

  • size=14, position=4
  • size=15, position=5
  • size=16, position=4
  • size=17, position=5

  • size=18, position=5
  • size=19, position=6
  • size=20, position=5
  • size=21, position=6

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.

like image 125
Amrutha Ajayakumar Avatar answered Feb 01 '23 15:02

Amrutha Ajayakumar


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.

like image 26
abhi Avatar answered Feb 01 '23 16:02

abhi