Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements
Last Updated :
14 Sep, 2022
Given an array arr[] consisting of N integers, the task is to print the last remaining array element after repeatedly selecting pairs of increasing adjacent elements (arr[i], arr[i + 1]) and removing any of the elements in the pair. If it is not possible to reduce the array to a single element, then print “Not Possible”.
Examples:
Input: arr[] = {3, 1, 2, 4}
Output: 4
Explanation:
Step 1: Choose a pair (1, 2) and remove 2 from it. Now the array becomes [3, 1, 4].
Step 2: Choose pair (1, 4) and remove 1 from it. Now the array becomes [3, 4].
Step 3: Choose pair (3, 4) and remove 3 from it. Now the array becomes [4].
Hence, the remaining element is 4.
Input: arr[] = {2, 3, 1}
Output: Not Possible
Approach: The given problem can be solved by observing the fact that if the first array element is less than the last array element, then all the elements between them can be removed by performing the given operations. Therefore, simply check if arr[0] < arr[N – 1] or not. If found to be true, print either the first or last array element. Otherwise, print -1.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void canArrayBeReduced( int arr[], int N)
{
if (N == 1) {
cout << arr[0];
return ;
}
if (arr[0] < arr[N - 1]) {
cout << arr[N - 1];
}
else
cout << "Not Possible" ;
}
int main()
{
int arr[] = { 6, 5, 2, 4, 1, 3, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
canArrayBeReduced(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void canArrayBeReduced( int [] arr, int N)
{
if (N == 1 )
{
System.out.print(arr[ 0 ]);
return ;
}
if (arr[ 0 ] < arr[N - 1 ])
{
System.out.print(arr[N - 1 ]);
}
else
System.out.print( "Not Possible" );
}
public static void main(String[] args)
{
int [] arr = { 6 , 5 , 2 , 4 , 1 , 3 , 7 };
int N = arr.length;
canArrayBeReduced(arr, N);
}
}
|
Python3
def canArrayBeReduced(arr, N):
if (N = = 1 ):
print (arr[ 0 ])
return
if (arr[ 0 ] < arr[N - 1 ]):
print (arr[N - 1 ])
else :
print ( "Not Possible" )
if __name__ = = "__main__" :
arr = [ 6 , 5 , 2 , 4 , 1 , 3 , 7 ]
N = len (arr)
canArrayBeReduced(arr, N)
|
C#
using System;
public class GFG {
static void canArrayBeReduced( int [] arr, int N)
{
if (N == 1)
{
Console.Write(arr[0]);
return ;
}
if (arr[0] < arr[N - 1])
{
Console.Write(arr[N - 1]);
}
else
Console.Write( "Not Possible" );
}
static public void Main()
{
int [] arr = { 6, 5, 2, 4, 1, 3, 7 };
int N = arr.Length;
canArrayBeReduced(arr, N);
}
}
|
Javascript
<script>
function canArrayBeReduced(arr, N)
{
if (N == 1)
{
document.write(arr[0]);
return ;
}
if (arr[0] < arr[N - 1])
{
document.write(arr[N - 1]);
}
else
document.write( "Not Possible" );
}
let arr = [ 6, 5, 2, 4, 1, 3, 7 ];
let N = arr.length;
canArrayBeReduced(arr, N);
</script>
|
Time Complexity: O(1), as we are not using any loops or recursion.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...