Last remaining value from 2^N integers after deleting Max from even sum and Min from odd sum pair
Last Updated :
21 Apr, 2022
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++
// C++ program to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate
// the last remaining number
long long int lastdigit(int N)
{
// 1<<n is 2^n
long long int ans = (1 << N) - 1;
return ans;
}
// Driver code
int main()
{
int N = 3;
// Function call
cout << lastdigit(N) << endl;
return 0;
}
Java
// Java program to implement the approach
import java.io.*;
class GFG {
// Function to calculate
// the last remaining number
static long lastdigit(int N)
{
// 1<<n is 2^n
long ans = (1 << N) - 1;
return ans;
}
// Driver code
public static void main (String[] args) {
int N = 3;
// Function call
System.out.println(lastdigit(N));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 code for the above approach
# Function to calculate
# the last remaining number
def lastdigit(N):
# 1<<n is 2^n
ans = (1 << N) - 1
return ans
# Driver code
N = 3
# function call
print(lastdigit(N))
# This code is contributed by phasing17
C#
// C# program to implement the approach
using System;
public class GFG{
// Function to calculate
// the last remaining number
static long lastdigit(int N)
{
// 1<<n is 2^n
long ans = (1 << N) - 1;
return ans;
}
// Driver code
static public void Main (){
int N = 3;
// Function call
Console.Write(lastdigit(N));
}
}
JavaScript
<script>
// JavaScript code for the above approach
// Function to calculate
// the last remaining number
function lastdigit(N)
{
// 1<<n is 2^n
let ans = (1 << N) - 1;
return ans;
}
// Driver code
let N = 3;
// Function call
document.write(lastdigit(N));
// This code is contributed by sanjoy_62.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Delete odd and even numbers at alternate step such that sum of remaining elements is minimized Given an array arr[] of N elements. At any step, we can delete a number of a different parity from the just previous step, i.e., if, at the previous step, an odd number was deleted then delete an even number in the current step or vice versa. It is allowed to start by deleting any number. Deletion i
7 min read
Maximize sum of remaining elements after every removal of the array half with greater sum Given an array arr[] consisting of N integers, the task is to maximize the resultant sum obtained after adding remaining elements after every removal of the array half with maximum sum. Array can be divided into two non-empty halves left[] and right[] where left[] contains the elements from the indi
15+ min read
Minimum deletions from front or back required to remove maximum and minimum from Array Given an array arr[] consisting of integers. The task is to find minimum deletions required to remove the initial minimum and maximum element from arr[].NOTE: Deletion can be performed either from the front or back of the array. Examples: Input: arr[] = {5, 7, 2, 4, 3}Output: 3Explanation: Initial m
6 min read
Last remaining element after repeated removal of odd indexed elements Given a positive integer N, the task is to print the last remaining element from a sequence [1, N] after repeatedly removing all the odd-indexed elements from the sequence. Examples: Input: N = 4Output: 4Explanation: Input: N = 9Output: 8 Naive Approach: The naive approach to solve the above problem
3 min read
Find the last remaining element after repeated removal of odd and even indexed elements alternately Given a positive integer N, the task is to print the last remaining element from a sequence [1, N] after repeatedly performing the following operations in the given order alternately: Remove all the odd-indexed elements from the sequence.Remove all the even-indexed elements from the sequence.Example
12 min read