Find last element after deleting every second element in array of n integers

Given a circular array of size n containing integers from 1 to n. Find the last element that would remain in the list after erasing every second element starting from the first element.

Input: 5
Output: 3
Explanation
Element in circular array are:
1 2 3 4 5
Starting from first element i.e, '1'
delete every second element like this,
1 0 3 4 5
1 0 3 0 5
0 0 3 0 5
0 0 3 0 0
For demonstration purpose erased element
would be treated as '0'. 
Thus at the end of list, the last element
remains is 3.

Input: 10
Output: 5

The Naive approach is to remove every second element from the array until the size of array becomes equals to ‘1’. Time complexity of this approach is O(n2) which would not be feasible for large value of ‘n’.

The Efficient approach is to use recursion. Let’s consider n to be even. In one traversal, numbers 2, 4, 6 … N will be removed and we start again from 1. Thus exactly n/2 numbers are removed, and we start as if from 1 in an array of N/2 containing only odd digits 1, 3, 5, … n/2.
Thus by this intuation, their recursive formula can be written as,



If n is even:
  solve(n) = 2 * solve(n/2) - 1
else
  solve(n) = 2 * solve((n-1) / 2) + 1

Base condition would occur when n = 1, then
answer would be 1.

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program return last number
// after removing every second
// element from circular array
#include<iostream>
using namespace std;
  
// Utility function to return last
// number after removing element
int removeAlternate(int n) {
    if (n == 1)
        return 1;
  
    if (n % 2 == 0)
        return 2 * removeAlternate(n / 2) - 1;
    else
        return 2 * removeAlternate(((n - 1) / 2)) + 1;
}
  
// Driver code
int main() {
    int n = 5;
    cout << removeAlternate(n) << "\n";
  
    n = 10;
    cout << removeAlternate(n) << "\n";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program return
// last number after 
// removing every second
// element from circular
// array
import java.util.*;
  
class Circular{
      
    // Utility function to
    // return last number 
    // number after removing
    // element
    public static int removeAlternate(int n)
    {
        if (n == 1)
            return 1;
  
        if (n % 2 == 0)
            return 2 * removeAlternate(n / 2) - 1;
        else
            return 2 * removeAlternate(((n - 1) / 2)) + 1;
    }
      
    public static void main(String[] args)
    {
        int n = 5;
        System.out.print(removeAlternate(n));
        n = 10;
        System.out.print("\n"+removeAlternate(n));
    }
}
  
// This code is contributed by rishabh_jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program return last number
# after removing every second
# element from circular array
  
# Utility function to return last
# number after removing element
def removeAlternate(n):
    if (n == 1):
        return 1
  
    if (n % 2 == 0):
        return 2 * removeAlternate(n / 2) - 1
    else:
        return 2 * removeAlternate(((n - 1) / 2)) + 1
  
# Driver code
n = 5
print(removeAlternate(n))
n = 10
print(removeAlternate(n))
   
# This code is contribited by Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program return last number after 
// removing every second element from 
// circular array
using System;
  
class Circular {
      
    // Utility function to return last number 
    // number after removing element
    public static int removeAlternate(int n)
    {
        if (n == 1)
            return 1;
  
        if (n % 2 == 0)
            return 2 * removeAlternate(n / 2) - 1;
        else
            return 2 * removeAlternate(((n - 1) / 2)) + 1;
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 5;
        Console.WriteLine(removeAlternate(n));
          
        n = 10;
        Console.WriteLine(removeAlternate(n));
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program return last number
// after removing every second
// element from circular array
  
// Utility function to return last
// number after removing element
function removeAlternate($n)
{
    if ($n == 1)
        return 1;
  
    if ($n % 2 == 0)
        return 2 * removeAlternate($n / 2) - 1;
    else
        return 2 * removeAlternate((($n - 1) / 
                                      2)) + 1;
}
  
    // Driver code
    $n = 5;
    echo removeAlternate($n) , "\n";
    $n = 10;
    echo removeAlternate($n) , "\n";
      
// This code is contributed by ajit
?>

chevron_right



Output

3
5

Time complexity: O(log(n))
Auxiliary space: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.