数组旋转和删除

bit*_*tan 4 c++ arrays algorithm data-structures

基于 GeeksForGeeks here 中的问题。我在这里遇到了一个解决方案。

有人可以帮助我理解解决方案。主要我需要以下块的帮助:

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;
}
Run Code Online (Sandbox Code Playgroud)

小智 5

对于每个大小的数组,一个特定的位置是答案(即 .independent 数组元素)。

对于任何大小为 8 的数组,第 2 个位置(即第 3 个元素)给出了答案。

让我们看一些例子:

  • 大小=1,位置=0

  • 大小=2,位置=1
  • 大小=3,位置=2
  • 大小=4,位置=1
  • 大小=5,位置=2

  • 大小=6,位置=2
  • 大小=7,位置=3
  • 大小=8,位置=2
  • 大小=9,位置=3

  • 大小=10,位置=3
  • 大小=11,位置=4
  • 大小=12,位置=3
  • 大小=13,位置=4

  • 大小=14,位置=4
  • 大小=15,位置=5
  • 大小=16,位置=4
  • 大小=17,位置=5

  • 大小=18,位置=5
  • 大小=19,位置=6
  • 大小=20,位置=5
  • 大小=21,位置=6

等等。

对于偶数:floor( (n-3)/4 )+2给出位置。

对于奇数大小:floor( (n-2)/4 )+1给出位置。