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: 1
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: 3
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.
C++
#include <algorithm>
#include <iostream>
int finalNumber( int arr[], int n)
{
std::sort(arr,
arr + n);
while (arr[n - 1] != arr[0]) {
int diff = arr[n - 1] - arr[0];
arr[n - 1] = diff;
std::sort(arr,
arr + n);
}
return arr[0];
}
int main()
{
int arr[] = { 3, 9, 6, 36 };
int n = sizeof (arr)
/ sizeof (arr[0]);
std::cout << finalNumber(arr, n) << std::endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int finalNumber( int [] arr) {
int n = arr.length;
Arrays.sort(arr);
while (arr[n - 1 ] != arr[ 0 ]) {
int diff = arr[n - 1 ] - arr[ 0 ];
arr[n - 1 ] = diff;
Arrays.sort(arr);
}
return arr[ 0 ];
}
public static void main(String[] args) {
int [] arr2 = { 3 , 9 , 6 , 36 };
System.out.println(finalNumber(arr2));
}
}
|
Python3
def final_number(arr):
n = len (arr)
arr.sort()
while arr[n - 1 ] ! = arr[ 0 ]:
diff = arr[n - 1 ] - arr[ 0 ]
arr[n - 1 ] = diff
arr.sort()
return arr[ 0 ]
if __name__ = = '__main__' :
arr = [ 3 , 9 , 6 , 36 ]
print (final_number(arr))
|
Javascript
function finalNumber(arr) {
const n = arr.length;
arr.sort((a, b) => a - b);
while (arr[n - 1] !== arr[0]) {
const diff = arr[n - 1] - arr[0];
arr[n - 1] = diff;
arr.sort((a, b) => a - b);
}
return arr[0];
}
const arr = [3, 9, 6, 36];
console.log(finalNumber(arr));
|
C#
using System;
using System.Linq;
class Program
{
static int FinalNumber( int [] arr, int n)
{
Array.Sort(arr);
while (arr[n - 1] != arr[0])
{
int diff = arr[n - 1] - arr[0];
arr[n - 1] = diff;
Array.Sort(arr);
}
return arr[0];
}
static void Main( string [] args)
{
int [] arr = { 3, 9, 6, 36 };
int n = arr.Length;
Console.WriteLine(FinalNumber(arr, n));
}
}
|
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++
#include <bits/stdc++.h>
using namespace std;
int finalNum( int arr[], int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
result = __gcd(result, arr[i]);
}
return result;
}
int main()
{
int arr[] = { 3, 9, 6, 36 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << finalNum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int finalNum( int arr[], int n)
{
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);
}
public static void main(String[] args)
{
int arr[] = { 3 , 9 , 6 , 36 };
int n = arr.length;
System.out.print(finalNum(arr, n));
}
}
|
Python3
from math import gcd as __gcd
def finalNum(arr, n):
result = arr[ 0 ]
for i in arr:
result = __gcd(result, i)
return result
arr = [ 3 , 9 , 6 , 36 ]
n = len (arr)
print (finalNum(arr, n))
|
C#
using System;
class GFG
{
static int finalNum( int []arr, int n)
{
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);
}
public static void Main(String[] args)
{
int []arr = { 3, 9, 6, 36 };
int n = arr.Length;
Console.Write(finalNum(arr, n));
}
}
|
Javascript
<script>
function finalNum(arr , n) {
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);
}
var arr = [ 3, 9, 6, 36 ];
var n = arr.length;
document.write(finalNum(arr, n));
</script>
|
Time Complexity: O(N*logN), as we are using a loop to traverse N times so it will cost us O(N) time and _gcd function will take logN time.
Auxiliary Space: O(1), as we are not using any extra space.
Contact Us