Last remaining value from 2^N integers after deleting Max from even sum and Min from odd sum pair
Given a Non-Negative integer N, denoting the first 2N positive integers where consecutive integers starting from 1 are paired with each other, the task is to find the last number remaining after performing the following operations:
- Consider a pair (x, y) of two consecutive integers which are not yet deleted from the 2N integers.
- If the sum of two integers x + y is even then delete x (the minimum one).
- If the sum of two integers x + y is odd then delete y (the maximum one).
Examples:
Input: N = 2
Output: 3
Explanation:
For N = 2 we have 2^N i.e; 4 integers and 4/2 pairs which are as follows-:
(1, 2), (3, 4)
Now after performing the operations:
1 + 2 = 3 which is odd, delete y. So remains 1.
3 + 4 = 7 which is odd, delete y. So remains 3
Now the remaining numbers are 1 and 3 and the pair is (1, 3).
Sum = 1 + 3 = 4 which is even, so delete 1.
So the last remaining element is 3.
Input: N = 3
Output: 7
Explanation:
For N = 2 we have 2^N i.e; 8 integers and 8/2 pairs which are as follows-:
(1, 2), (3, 4), (5, 6) and (7, 8)
Now after performing the operations:
1 + 2 = 3 which is odd, delete y. So remains 1.
3 + 4 = 7 which is odd, delete y. So remains 3.
5 + 6 = 11 which is odd, delete y. So remains 5.
7 + 8 = 15 which is odd, delete y. So remains 7.
Now the remaining numbers are 1, 3, 5 and 7 and the pairs are (1, 3), (5, 7).
1 + 3 = 4 which is even, delete x. So remains 3.
5 + 7 = 12 which is even, delete x. So remains 7.
Now the remaining numbers are 3 and 7 and the pair is (3, 7)
3 + 7 = 10 which is even, so delete 3.
So the last remaining element is 7.
Approach: The given problem can be solved using the below mathematical observation:
- The idea is to consider when the sum is odd or even while adding two numbers.
- If we add one even and one odd number then sum will always be an odd number
- If we add two odd numbers or two even numbers then sum will always be an even number.
Based on the above observation the solution for the problem can be derived as follows:
- For the first time, each pair has an odd number and an even number where the even number is the greater one. So the sum is always odd and the even number gets deleted.
- In the next steps, remaining numbers are all odd so the sum of pairs will always be even. Therefore the alternative odd numbers starting from the first (smaller one) get removed in each step.
- From the above observation clearly, the maximum among all odd numbers will be the last remaining one i.e. 2N -1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int lastdigit( int N)
{
long long int ans = (1 << N) - 1;
return ans;
}
int main()
{
int N = 3;
cout << lastdigit(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long lastdigit( int N)
{
long ans = ( 1 << N) - 1 ;
return ans;
}
public static void main (String[] args) {
int N = 3 ;
System.out.println(lastdigit(N));
}
}
|
Python3
def lastdigit(N):
ans = ( 1 << N) - 1
return ans
N = 3
print (lastdigit(N))
|
C#
using System;
public class GFG{
static long lastdigit( int N)
{
long ans = (1 << N) - 1;
return ans;
}
static public void Main (){
int N = 3;
Console.Write(lastdigit(N));
}
}
|
Javascript
<script>
function lastdigit(N)
{
let ans = (1 << N) - 1;
return ans;
}
let N = 3;
document.write(lastdigit(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
21 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...