Remaining array element after repeated removal of the smallest element from pairs with absolute difference of 2 or 0
Given an array arr[] consisting of N positive integers, the task is to check if it is possible to reduce the size of the array to 1 by repeatedly removing the smallest element from a pair having absolute difference of 2 or 0 between them. If it is not possible to reduce, then print “-1”. Otherwise, print the last remaining element in the array.
Examples:
Input: arr[] = {2, 4, 6, 8, 0, 8}
Output: 8
Explanation:
arr[] = {2, 4, 6, 8, 0, 8}, Remove 0 from the pair (2, 0).
arr[] = {2, 4, 6, 8, 8}. Remove 2 from the pair (2, 4).
arr[] = {4, 6, 8, 8}, Remove 4 from the pair (4, 6).
arr[] = {6, 8, 8}. Remove 6 from the pair (6, 8).
arr[] = {8, 8}. Remove 8.
arr[] = {8}
Input: arr[] = {1, 7, 3, 3}
Output: -1
Explanation:
arr[] = {1, 7, 3, 3}. Remove 1 from the pair (1, 3).
arr[] = {7, 3, 3}. Remove 3 from the pair (3, 3).
arr[] = {7, 3}. No more removals possible.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findLastElement( int arr[], int N)
{
sort(arr, arr + N);
int i = 0;
for (i = 1; i < N; i++) {
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2) {
cout << "-1" << endl;
return ;
}
}
cout << arr[N - 1] << endl;
}
int main()
{
int arr[] = { 2, 4, 6, 8, 0, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
findLastElement(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findLastElement( int arr[], int N)
{
Arrays.sort(arr);
int i = 0 ;
for (i = 1 ; i < N; i++) {
if (arr[i] - arr[i - 1 ] != 0
&& arr[i] - arr[i - 1 ] != 2 )
{
System.out.println( "-1" );
return ;
}
}
System.out.println( arr[N - 1 ]);
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 6 , 8 , 0 , 8 };
int N = arr.length;
findLastElement(arr, N);
}
}
|
Python3
def findLastElement(arr, N):
arr.sort();
i = 0 ;
for i in range ( 1 , N):
if (arr[i] - arr[i - 1 ] ! = 0 \
and arr[i] - arr[i - 1 ] ! = 2 ):
print ( "-1" );
return ;
print (arr[N - 1 ]);
if __name__ = = '__main__' :
arr = [ 2 , 4 , 6 , 8 , 0 , 8 ];
N = len (arr);
findLastElement(arr, N);
|
C#
using System;
public class GFG
{
static void findLastElement( int []arr, int N)
{
Array.Sort(arr);
int i = 0;
for (i = 1; i < N; i++)
{
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2)
{
Console.WriteLine( "-1" );
return ;
}
}
Console.WriteLine(arr[N - 1]);
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 6, 8, 0, 8 };
int N = arr.Length;
findLastElement(arr, N);
}
}
|
Javascript
<script>
function findLastElement(arr, N)
{
arr.sort();
let i = 0;
for (i = 1; i < N; i++)
{
if (arr[i] - arr[i - 1] != 0
&& arr[i] - arr[i - 1] != 2)
{
document.write( "-1" + "<br>" );
return ;
}
}
document.write(arr[N - 1] + "<br>" );
}
let arr = [ 2, 4, 6, 8, 0, 8 ];
let N = arr.length;
findLastElement(arr, N);
</script>
|
Time Complexity: O(N*logN), as we are using a inbuilt sort function.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...