Reduce the array to a single element with the given operation

02 Jul, 2022
Given an integer N and an array arr containing integers from 1 to N in a sorted fashion. The task is to reduce the array to a single element by performing the following operation: All the elements in the odd positions will be removed after a single operation. This operation will be performed until only a single element is left int the array and it prints that element at the end.
Examples:  
Input: N = 3 Output: 2 Initially the array will be arr[] = {1, 2, 3} After the 1st operation, ‘1’ and ‘3’ will be removed and the array becomes arr[] = {2} So 2 is the only element left at the end.
Input: N = 6 Output: 4 arr[] = {1, 2, 3, 4, 5, 6} After the first iteration, the array becomes {2, 4, 6} After the second iteration, the array becomes {4} So 4 is the last element.  
Approach: For this kind of problem:  
Write multiple test cases and the respective output.
Analyze the output for the given input and the relation between them.
Once we find the relation we will try to express it in the form of a mathematical expression if possible.
Write the code/algorithm for the above expression.
So let’s create a table for the given input N and its respective output. 
Analyzed Relation: The output is at 2i. Using the above table, we can create the output table for the range of inputs. 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach #include<bits/stdc++.h>using namespace std; // Function to return the final elementlong getFinalElement(long n){    long finalNum;    for (finalNum = 2; finalNum * 2 <= n; finalNum *= 2)            ;    return finalNum;}  // Driver codeint main(){       int N = 12;    cout << getFinalElement(N) ;       return 0;}// This code is contributed by Ryuga
// Java implementation of the approachclass OddPosition {     // Function to return the final element    public static long getFinalElement(long n)    {        long finalNum;        for (finalNum = 2; finalNum * 2 <= n; finalNum *= 2)            ;        return finalNum;    }     // Driver code    public static void main(String[] args)    {        int N = 12;        System.out.println(getFinalElement(N));    }}
# Python 3 implementation of the approach # Function to return the final elementdef getFinalElement(n):     finalNum = 2    while finalNum * 2 <= n:        finalNum *= 2    return finalNum # Driver codeif __name__ =="__main__":     N = 12    print( getFinalElement(N)) # This code is contributed# by ChitraNayal
// C# implementation of the approachusing System; public class GFG{         // Function to return the final element    public static long getFinalElement(long n)    {        long finalNum;        for (finalNum = 2; finalNum * 2 <= n; finalNum *= 2)            ;        return finalNum;    }     // Driver code    static public void Main (){        int N = 12;        Console.WriteLine(getFinalElement(N));    }}
<?php//PHP implementation of the approach // Function to return the final elementfunction  getFinalElement($n){    $finalNum=0;    for ($finalNum = 2; ($finalNum * 2) <= $n; $finalNum *= 2) ;             return $finalNum;}  // Driver code    $N = 12;    echo  getFinalElement($N) ;          // This code is contributed by akt_mit?>
<script>// Javascript implementation of the approach         // Function to return the final element    function getFinalElement(n)    {        let finalNum;        for (finalNum = 2; finalNum * 2 <= n; finalNum *= 2)            ;        return finalNum;    }         // Driver code    let N = 12;    document.write(getFinalElement(N));     // This code is contributed by avanitrachhadiya2155</script>     
8
 
Time Complexity: O(logN), since every time the finalNum value is becoming twice its current value.Auxiliary Space: O(1), since no extra space has been taken.
Sach_Code
ankthon
ukasp
jit_t
avanitrachhadiya2155
pankajsharmagfg
subham348
Algorithms
Competitive Programming
Mathematical
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Hashing | A Complete Tutorial
Find if there is a path between two vertices in an undirected graph
How to Start Learning DSA?
Complete Roadmap To Learn DSA From Scratch
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Modulo 10^9+7 (1000000007)
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming