Skip to content
Related Articles

Related Articles

Minimize last remaining element of Array by selecting pairs such that arr[i] >= arr[j] and replace arr[i] with arr[i] – arr[j]

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Feb, 2022
View Discussion
Improve Article
Save Article

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




// C++ Program os the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to minimize the last
// remaining element of array
int minValue(int arr[], int n)
{
    // Stores the required value
    int ans;
 
    // Initialize answer
    ans = arr[0];
 
    // Loop to traverse arr[]
    for (int i = 1; i < n; i++) {
        // Calculate the GCD
        ans = __gcd(ans, arr[i]);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 13, 8, 10 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minValue(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Recursive function to return gcd of a and b
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to minimize the last
  // remaining element of array
  static int minValue(int arr[], int n)
  {
    // Stores the required value
    int ans;
 
    // Initialize answer
    ans = arr[0];
 
    // Loop to traverse arr[]
    for (int i = 1; i < n; i++) {
      // Calculate the GCD
      ans = gcd(ans, arr[i]);
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int arr[] = { 5, 13, 8, 10 };
    int N = arr.length;
    System.out.println(minValue(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python Program os the above approach
def __gcd (a, b):
    if (not b):
        return a;
    return __gcd(b, a % b);
 
# Function to minimize the last
# remaining element of array
def minValue (arr, n):
 
    # Stores the required value
    ans = None
 
    # Initialize answer
    ans = arr[0];
 
    # Loop to traverse arr[]
    for i in range(1, n):
        # Calculate the GCD
        ans = __gcd(ans, arr[i]);
     
 
    # Return Answer
    return ans;
 
# Driver Code
arr = [5, 13, 8, 10];
N = len(arr)
print(minValue(arr, N));
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
 
class GFG {
 
  // Recursive function to return gcd of a and b
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to minimize the last
  // remaining element of array
  static int minValue(int []arr, int n)
  {
    // Stores the required value
    int ans;
 
    // Initialize answer
    ans = arr[0];
 
    // Loop to traverse arr[]
    for (int i = 1; i < n; i++) {
      // Calculate the GCD
      ans = gcd(ans, arr[i]);
    }
 
    // Return Answer
    return ans;
  }
 
  // Driver Code
  public static void Main () {
    int []arr = { 5, 13, 8, 10 };
    int N = arr.Length;
    Console.Write(minValue(arr, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
    // JavaScript Program os the above approach
    const __gcd = (a, b) => {
        if (!b) {
            return a;
        }
 
        return __gcd(b, a % b);
    }
 
    // Function to minimize the last
    // remaining element of array
    const minValue = (arr, n) => {
     
        // Stores the required value
        let ans;
 
        // Initialize answer
        ans = arr[0];
 
        // Loop to traverse arr[]
        for (let i = 1; i < n; i++) {
            // Calculate the GCD
            ans = __gcd(ans, arr[i]);
        }
 
        // Return Answer
        return ans;
    }
 
    // Driver Code
 
    let arr = [5, 13, 8, 10];
    let N = arr.length;
    document.write(minValue(arr, N));
 
// This code is contributed by rakeshsahni
 
</script>


Output

1

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!