Find the last element after repeatedly removing every second element from either end alternately
Last Updated :
03 May, 2022
Given an array arr[] consisting of N integers, the task is to find the last remaining array element after removing every second element, starting from the first and last alternately.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Output: 8
Explanation: Elements in the list are: 1 2 3 4 5 6 7 8 9 10
Starting from first array element, removing every second element from arr[] = {1, 2. 3, 4, 5, 6, 7, 8, 9, 10} modifies arr[] to {2, 4, 6, 8, 10}.
Starting from last array element, removing every second element from arr[] = {2, 4, 6, 8, 10} modifies arr[] to {4, 8}.
Starting from first array element, removing every second element from arr[] = {4, 8} modifies arr[] to {8}.
Therefore, the last remaining element in the array is 8.
Input: arr[] = {2, 3, 5, 6}
Output: 3
Naive Approach: The simplest approach to solve this problem is to remove every second element in the array starting from the first and last indices alternately until the size of the array reduces to 1. Print the last array element remaining after performing the given operations.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by an observation in the below illustration for N = 20:
The following observations can be made from the above illustration:
- Every time N/2 numbers are eliminated.
- If the turn direction is from left to right (?), the first element of the sequence changes to (currentFirstElement + 2step – 1).
- If the turn direction is from right to left (?) and then remaining numbers in the list are odd, the first element of the sequence changes to (currentFirstElement + 2step – 1).
- After getting the final value of currentFirstElement from the above steps, print the value as the remaining element in the sequence.
Steps:
- Initialize a variable, say leftTurn, to check whether the element is to be deleted from the left or right.
- Initialize variables remainElements as N, steps as 1, and head as 1, to store the remaining element in the sequence, a number of steps performed, and the first element of the sequence.
- Iterate until remainElements is greater than 1:
- if the value of leftTurn is true, then update the head as (head + 2step – 1). Otherwise, if the remainElement is odd, then update the head as (head + 2step – 1).
- Update the value of remainElement as remainElement/2.
- Update the steps as 2 * steps.
- Toggle the leftTurn to remove elements from the right.
- After completing the above steps, print the value at the index (head – 1) as the remaining element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printLastElement( int arr[], int N)
{
bool leftTurn = true ;
int remainElements = N;
int step = 1;
int head = 1;
while (remainElements > 1) {
if (leftTurn) {
head = head + step;
}
else {
if (remainElements % 2 == 1)
head = head + step;
}
remainElements = remainElements / 2;
step = step * 2;
leftTurn = !leftTurn;
}
cout << arr[head - 1];
}
int main()
{
int arr[] = { 2, 3, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
printLastElement(arr, N);
return 0;
}
|
Java
import java.lang.*;
class GFG{
public static void printLastElement( int arr[], int N)
{
boolean leftTurn = true ;
int remainElements = N;
int step = 1 ;
int head = 1 ;
while (remainElements > 1 )
{
if (leftTurn)
{
head = head + step;
}
else
{
if (remainElements % 2 == 1 )
head = head + step;
}
remainElements = remainElements / 2 ;
step = step * 2 ;
leftTurn = !leftTurn;
}
System.out.print( arr[head - 1 ]);
}
public static void main(String args[])
{
int [] arr = { 2 , 3 , 5 , 6 };
int N = arr.length;
printLastElement(arr, N);
}
}
|
Python3
def printLastElement(arr, N):
leftTurn = True
remainElements = N
step = 1
head = 1
while (remainElements > 1 ):
if (leftTurn):
head = head + step
else :
if (remainElements % 2 = = 1 ):
head = head + step
remainElements = remainElements / / 2
step = step * 2
leftTurn = not leftTurn
print (arr[head - 1 ])
if __name__ = = "__main__" :
arr = [ 2 , 3 , 5 , 6 ]
N = len (arr)
printLastElement(arr, N)
|
Javascript
<script>
function printLastElement(arr, N)
{
var leftTurn = true ;
var remainElements = N;
var step = 1;
var head = 1;
while (remainElements > 1)
{
if (leftTurn)
{
head = head + step;
}
else
{
if (remainElements % 2 == 1)
head = head + step;
}
remainElements = remainElements / 2;
step = step * 2;
leftTurn = !leftTurn;
}
document.write(arr[head - 1]);
}
var arr = [ 2, 3, 5, 6 ];
var N = arr.length;
printLastElement(arr, N);
</script>
|
C#
using System;
public class GFG{
public static void printLastElement( int [] arr, int N)
{
bool leftTurn = true ;
int remainElements = N;
int step = 1;
int head = 1;
while (remainElements > 1)
{
if (leftTurn)
{
head = head + step;
}
else
{
if (remainElements % 2 == 1)
head = head + step;
}
remainElements = remainElements / 2;
step = step * 2;
leftTurn = !leftTurn;
}
Console.Write( arr[head - 1]);
}
static public void Main (){
int [] arr = { 2, 3, 5, 6 };
int N = arr.Length;
printLastElement(arr, N);
}
}
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...