Akash and Akhil are playing a game. They have N balls numbered from 0 to N-1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order from say, 0,1,2,3 to 3,2,1,0. He further asks Akash to reverse the position of the balls N times, each time starting from one position further to the right, till he reaches the last ball. So, Akash has to reverse the positions of the ball starting from position, then from position, then from the position, and so on. At the end of the game, Akhil will ask Akash the final position of any ball numbered K. Akash will win the game if he can answer. Help Akash.
Example:
Input: n = 3, k = 1
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){int n = 3, k = 1;int i = n - 1, j = 0;int index = 0;int ans = 1;while (i >= j){if (i == k){ans = index;break;}index++;if (j == k){ans = index;break;}index++;i--;j++;}cout << ans << "\n";return 0;}
No comments:
Post a Comment