# Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements

• Last Updated : 01 Apr, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 .
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 < 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

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to print the last remaining``// array element after after performing``// given operations``void` `canArrayBeReduced(``int` `arr[], ``int` `N)``{``    ``// If size of the array is 1``    ``if` `(N == 1) {``        ``cout << arr;``        ``return``;``    ``}` `    ``// Check for the condition``    ``if` `(arr < arr[N - 1]) {``        ``cout << arr[N - 1];``    ``}` `    ``// If condition is not satisfied``    ``else``        ``cout << ``"Not Possible"``;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 6, 5, 2, 4, 1, 3, 7 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function Call``    ``canArrayBeReduced(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``class` `GFG``{` `  ``// Function to print the last remaining``  ``// array element after after performing``  ``// given operations``  ``static` `void` `canArrayBeReduced(``int``[] arr, ``int` `N)``  ``{` `    ``// If size of the array is 1``    ``if` `(N == ``1``)``    ``{``      ``System.out.print(arr[``0``]);``      ``return``;``    ``}` `    ``// Check for the condition``    ``if` `(arr[``0``] < arr[N - ``1``])``    ``{``      ``System.out.print(arr[N - ``1``]);``    ``}` `    ``// If condition is not satisfied``    ``else``      ``System.out.print(``"Not Possible"``);``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``int``[] arr = { ``6``, ``5``, ``2``, ``4``, ``1``, ``3``, ``7` `};``    ``int` `N = arr.length;` `    ``// Function Call``    ``canArrayBeReduced(arr, N);``  ``}``}` `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python 3 program for the above approach` `# Function to print the last remaining``# array element after after performing``# given operations``def` `canArrayBeReduced(arr,  N):` `    ``# If size of the array is 1``    ``if` `(N ``=``=` `1``):``        ``print``(arr[``0``])``        ``return` `    ``# Check for the condition``    ``if` `(arr[``0``] < arr[N ``-` `1``]):``        ``print``(arr[N ``-` `1``])` `    ``# If condition is not satisfied``    ``else``:``        ``print``(``"Not Possible"``)`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``6``, ``5``, ``2``, ``4``, ``1``, ``3``, ``7``]``    ``N ``=` `len``(arr)` `    ``# Function Call``    ``canArrayBeReduced(arr, N)` `    ``# This code is contributed by chitranayal.`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG {` `  ``// Function to print the last remaining``  ``// array element after after performing``  ``// given operations``  ``static` `void` `canArrayBeReduced(``int``[] arr, ``int` `N)``  ``{` `    ``// If size of the array is 1``    ``if` `(N == 1)``    ``{``      ``Console.Write(arr);``      ``return``;``    ``}` `    ``// Check for the condition``    ``if` `(arr < arr[N - 1])``    ``{``      ``Console.Write(arr[N - 1]);``    ``}` `    ``// If condition is not satisfied``    ``else``      ``Console.Write(``"Not Possible"``);``  ``}` `  ``// Driver Code``  ``static` `public` `void` `Main()``  ``{``    ``int``[] arr = { 6, 5, 2, 4, 1, 3, 7 };``    ``int` `N = arr.Length;` `    ``// Function Call``    ``canArrayBeReduced(arr, N);``  ``}``}` `// This code is contributed by Dharanendra L V.`

## Javascript

 ``
Output:
`7`

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

My Personal Notes arrow_drop_up