Given a sorted array arr[] of the first N Natural Numbers and an integer X, the task is to print the last remaining element after performing the below operations (N – 1) times:
Examples:
Input: N = 5, arr[] = {1, 2, 3, 4, 5}, X = 1
Output: 3
Explanation:
The operations are performed as follows:
- Rotating the array right by 1 unit modifies the array to {5, 1, 2, 3, 4}. Deleting the array element modifies the array to {5, 1, 2, 3}.
- Rotating the array right by 1 unit modifies the array to {3, 5, 1, 2}. Deleting the array element modifies the array to {3, 5, 1}.
- Rotating the array right by 1 unit modifies the array to {1, 3, 5}. Deleting the array element modifies the array to {1, 3}.
- Rotating the array right by 1 unit modifies the array to {3, 1}. Deleting the array element modifies the array to {3}.
Therefore, the last remaining element is 3.
Input: N = 5, arr[] = {1, 2, 3, 4, 5}, X = 2
Output: 3
Naive Approach: The simplest approach to solve the given problem is to push all the numbers of the range [1, N] in a deque and perform the given operations (N – 1) times according to the given value of X and then print the last remaining element.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be optimized based on the following observations:
- If the value of X is 1, then the last left element will be the difference between the smallest power of 2 greater N and N.
- If the value of X is 2, then the last left element will be 2*(N – D) + 1, where D represents the largest power of 2 less than or equal to N.
Follow the steps below to solve the problem:
- Store the power of 2 greater than N in a variable, say nextPower.
- If the value of X is 1, then print the value (nextPower – N) as the result.
- Otherwise, print the value 2*(N – nextPower / 2) + 1 as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rotate( int arr[], int N, int X)
{
long long int nextPower = 1;
while (nextPower <= N)
nextPower *= 2;
if (X == 1)
return nextPower - N;
long long int prevPower = nextPower / 2;
return 2 * (N - prevPower) + 1;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int X = 1;
int N = sizeof (arr) / sizeof (arr[0]);
cout << rotate(arr, N, X);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int rotate( int arr[], int N, int X)
{
long nextPower = 1 ;
while (nextPower <= N)
nextPower *= 2 ;
if (X == 1 )
return ( int ) nextPower - N;
long prevPower = nextPower / 2 ;
return 2 * (N - ( int )prevPower) + 1 ;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int X = 1 ;
int N =arr.length;
System.out.println(rotate(arr, N, X));
}
}
|
Python3
def rotate(arr, N, X):
nextPower = 1
while (nextPower < = N):
nextPower * = 2
if (X = = 1 ):
ans = nextPower - N
return ans
prevPower = nextPower / / 2
return 2 * (N - prevPower) + 1
arr = [ 1 , 2 , 3 , 4 , 5 ]
X = 1
N = len (arr)
print (rotate(arr, N, X))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int rotate( int []arr, int N, int X)
{
int nextPower = 1;
while (nextPower <= N)
nextPower *= 2;
if (X == 1)
return nextPower - N;
int prevPower = nextPower / 2;
return 2 * (N - prevPower) + 1;
}
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int X = 1;
int N = arr.Length;
Console.Write(rotate(arr, N, X));
}
}
|
Javascript
<script>
function rotate(arr, N, X) {
let nextPower = 1;
while (nextPower <= N)
nextPower *= 2;
if (X == 1)
return nextPower - N;
let prevPower = nextPower / 2;
return 2 * (N - prevPower) + 1;
}
let arr = [1, 2, 3, 4, 5];
let X = 1;
let N = arr.length;
document.write(rotate(arr, N, X));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)