Skip to content
Related Articles

Related Articles

Improve Article
Find the final number obtained after performing the given operation
  • Last Updated : 05 May, 2021

Given an array of positive distinct integers arr[], the task is to find the final number obtained by performing the following operation on the elements of the array: 
Operation: Take two unequal numbers and replace the larger number with their difference until all numbers become equal.
Examples: 
 

Input: arr[] = {5, 2, 3} 
Output:
5 – 3 = 2, arr[] = {2, 2, 3} 
3 – 2 = 1, arr[] = {2, 2, 1} 
2 – 1 = 1, arr[] = {2, 1, 1} 
2 – 1 = 1, arr[] = {1, 1, 1}
Input: arr[] = {3, 9, 6, 36} 
Output:
 

 

Naive approach: Since final answer will always be distinct, one can just sort the array and replace the largest term with the difference of the two largest elements and repeat the process until all the numbers become equal.
Efficient approach: From Euclidean’s algorithm, it is known that gcd(a, b) = gcd(a – b, b). This can be extended to gcd(A1, A2, A3, …, An) = gcd(A1 – A2, A2, A3, …, An)
Also, let’s say that after applying the given operation, the final number obtained be K. Hence, from the extended algorithm, it can be said that gcd(A1, A2, A3, …, An) = gcd(K, K, …, n times). Since gcd(K, K, …, n times) = K, the solution of the given problem can be found 
by finding the gcd of all the elements of the array.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the final number
// obtained after performing the
// given operation
int finalNum(int arr[], int n)
{
 
    // Find the gcd of the array elements
    int result = 0;
    for (int i = 0; i < n; i++) {
        result = __gcd(result, arr[i]);
    }
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 9, 6, 36 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << finalNum(arr, n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
 
// Function to return the final number
// obtained after performing the
// given operation
static int finalNum(int arr[], int n)
{
 
    // Find the gcd of the array elements
    int result = 0;
    for (int i = 0; i < n; i++)
    {
        result = __gcd(result, arr[i]);
    }
    return result;
}
 
static int __gcd(int a, int b)
{
    return b == 0? a:__gcd(b, a % b);    
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 9, 6, 36 };
    int n = arr.length;
 
    System.out.print(finalNum(arr, n));
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 implementation of the approach
from math import gcd as __gcd
 
# Function to return the final number
# obtained after performing the
# given operation
def finalNum(arr, n):
 
    # Find the gcd of the array elements
    result = arr[0]
    for i in arr:
        result = __gcd(result, i)
    return result
 
# Driver code
arr = [3, 9, 6, 36]
n = len(arr)
 
print(finalNum(arr, n))
 
# This code is contributed by Mohit Kumar

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the readonly number
// obtained after performing the
// given operation
static int finalNum(int []arr, int n)
{
 
    // Find the gcd of the array elements
    int result = 0;
    for (int i = 0; i < n; i++)
    {
        result = __gcd(result, arr[i]);
    }
    return result;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 9, 6, 36 };
    int n = arr.Length;
 
    Console.Write(finalNum(arr, n));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
// javascript implementation of the approach   
// Function to return the final number
    // obtained after performing the
    // given operation
    function finalNum(arr , n) {
 
        // Find the gcd of the array elements
        var result = 0;
        for (i = 0; i < n; i++) {
            result = __gcd(result, arr[i]);
        }
        return result;
    }
 
    function __gcd(a , b) {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Driver code
     
        var arr = [ 3, 9, 6, 36 ];
        var n = arr.length;
 
        document.write(finalNum(arr, n));
 
// This code contributed by aashish1995
</script>
Output: 
3

 

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 industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :