Open In App

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 = 4
Output: 4
Explanation: 



Input: N = 9
Output: 8

 

Naive Approach: The naive approach to solve the above problem is to store the sequence. Iterate the sequence and remove the odd-indexed elements from it. Repeat the above steps till only one element remains.

Time Complexity: O(N logN)
Auxiliary Space: O(1)

Efficient Approach: Consider the below observation:

From the above observations, it can be deduced that:

last remaining element (L) = log2 N

Below is the implementation of the above approach.




#include <iostream>
#include<math.h>
using namespace std;
int lastBlock(int N)
{
 
    // finding power of 2 less than or equal N
    int largestPower = log2(N);
 
    // returning the last remaining number
    return pow(2,largestPower);
}
 
// Driver code
int main()
{
 
    int N = 10;
 
    cout << lastBlock(N);
    return 0;
}
 
// This code is contributed by maddler.




// Java program for the above approach
import java.io.*;
 
class GFG {
 
  // Function to find last remaining element
  public static int lastBlock(int N) {
 
    // finding power of 2 less than or equal N
    int largestPower = (int)(Math.log(N) / Math.log(2));
 
    // returning the last remaining number
    return (int)(Math.pow(2, largestPower));
 
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 10;
 
    // Function Call
    System.out.println(lastBlock(N));
  }
}
 
// This code is contributed by code_hunt.




# Python program for above approach.
import math
 
# Function to find last remaining element
def lastBlock(N):
 
    # finding power of 2 less than or equal N
    largestPower = (int)(math.log(N, 2))
     
    # returning the last remaining number
    return (int)(math.pow(2, largestPower))
 
 
# Driver Code
N = 10
 
# Function Call
print(lastBlock(N))




// C# program for the above approach
using System;
 
class GFG {
 
  // Function to find last remaining element
  public static int lastBlock(int N) {
 
    // finding power of 2 less than or equal N
    int largestPower = (int)(Math.Log(N) / Math.Log(2));
 
    // returning the last remaining number
    return (int)(Math.Pow(2, largestPower));
 
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int N = 10;
 
    // Function Call
    Console.Write(lastBlock(N));
  }
}
 
// This code is contributed by shivanisinghss2110




<script>
 
        // JavaScript program for the above approach;
 
        // Function to find last remaining element
        function lastBlock(N) {
 
            // finding power of 2 less than or equal N
            largestPower = Math.floor(Math.log2(N))
 
            // returning the last remaining number
            return Math.floor(Math.pow(2, largestPower))
 
        }
        // Driver Code
        let N = 10
 
        // Function Call
        document.write(lastBlock(N));
 
 
   // This code is contributed by Potta Lokesh
    </script>

Output
8

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :