Minimize last remaining element of Array by selecting pairs such that arr[i] >= arr[j] and replace arr[i] with arr[i] – arr[j]
Last Updated :
20 Feb, 2023
Given an array arr[] of N positive integers, the task is to find the smallest possible value of the last remaining element in the array after performing the following operations any number of times:
- Select a pair of indices (i, j) such that arr[i] >= arr[j] and replace arr[i] with arr[i] – arr[j].
- If an array element arr[i] <= 0, remove it from the array.
Example:
Input: arr[] = {2, 4, 8, 32}
Output: 2
Explanation: In first 4 operations, select (i, j) as (3, 2). Hence, the array after 4 operations will become arr[] = {2, 4, 8, 0}. Here, arr[3] can be removed as arr[3] = 0. Similarly, perform the operation twice for (i, j) = (2, 1). The array after operations is arr[] = {2, 4}. Now perform the given operation twice for (i, j) as (1, 0). The final array will be arr[] = {2}. Therefore, the last remaining element is 2, which is the minimum possible.
Input: arr[] = {5, 13, 8, 10}
Output: 1
Approach: The given problem can be solved using the following observations:
- According to the Basic Euclidean Algorithm to find the GCD of two integers (x, y), it can be derived that GCD(x, y) = GCD(x, y – x), if y > x, otherwise, the values of x and y can be simply swapped. This relation will be true till the value of y – x reduces to 0.
- Hence, it can be concluded that the minimum reachable value of the pair (x, y) by subtracting the larger value from the smaller value is GCD(x, y).
Therefore, using the above observation, the required answer will be the GCD of all elements of the given array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minValue( int arr[], int n)
{
int ans;
ans = arr[0];
for ( int i = 1; i < n; i++) {
ans = __gcd(ans, arr[i]);
}
return ans;
}
int main()
{
int arr[] = { 5, 13, 8, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minValue(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int minValue( int arr[], int n)
{
int ans;
ans = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
ans = gcd(ans, arr[i]);
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 5 , 13 , 8 , 10 };
int N = arr.length;
System.out.println(minValue(arr, N));
}
}
|
Python3
def __gcd (a, b):
if ( not b):
return a;
return __gcd(b, a % b);
def minValue (arr, n):
ans = None
ans = arr[ 0 ];
for i in range ( 1 , n):
ans = __gcd(ans, arr[i]);
return ans;
arr = [ 5 , 13 , 8 , 10 ];
N = len (arr)
print (minValue(arr, N));
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int minValue( int []arr, int n)
{
int ans;
ans = arr[0];
for ( int i = 1; i < n; i++) {
ans = gcd(ans, arr[i]);
}
return ans;
}
public static void Main () {
int []arr = { 5, 13, 8, 10 };
int N = arr.Length;
Console.Write(minValue(arr, N));
}
}
|
Javascript
<script>
const __gcd = (a, b) => {
if (!b) {
return a;
}
return __gcd(b, a % b);
}
const minValue = (arr, n) => {
let ans;
ans = arr[0];
for (let i = 1; i < n; i++) {
ans = __gcd(ans, arr[i]);
}
return ans;
}
let arr = [5, 13, 8, 10];
let N = arr.length;
document.write(minValue(arr, N));
</script>
|
Time Complexity: O(N*log(max(arr))) where N is the number of elements in the array and max(arr) is the maximum element in the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...