We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fba140e commit 22a0572Copy full SHA for 22a0572
scripts/algorithms/E/Elimination Game/Elimination Game.cpp
@@ -1,17 +1,16 @@
1
class Solution {
2
public:
3
int lastRemaining(int n) {
4
- int beg = 1, d = 1, len = n;
5
- bool fromleft = true;
6
-
7
- while(len != 1 ) {
8
- if(fromleft || (len&1))
9
- beg += d;
10
- len >>= 1;
11
- d <<= 1;
12
- fromleft = !fromleft;
+ bool left=true;
+ int head=1,step=1;//step is the difference between adjacent elements.
+ while(n>1){
+ if(left || (n&1)){//(n&1)->odd
+ head=head+step;
+ }
+ step=step*2;
+ n=n/2;
+ left=!left;
13
}
14
15
- return beg;
+ return head;
16
17
-};
+};
0 commit comments