Number of operations such that size of the Array becomes 1
Last Updated :
27 Jul, 2023
Given an array A[] of N positive integers. The task is to find the number of operations such that the size of the array becomes 1 at the end of operations. The operation to be performed is:
- In the current array, let X be the maximum element and Y be the minimum element. Let Z=X%Y. If Z is 0, remove X from the array, else replace X with Z.
- Note that after each operation the size of the array either remains the same or gets reduced by 1.
Examples:
Input: N = 3, A[] = {2, 3, 6}
Output: 3
Explanation: Following are the course of operations –
1st operation -> X = 6, Y = 2. Z = X%Y = 0. Hence, we delete X=6 (i.e. 3rd element) from the array. A[] becomes {2, 3}.
2nd operation -> X = 3, Y = 2. Z =X%Y = 1. Hence, we replace X with Z. A[] becomes {2, 1}.
3rd operation -> X = 2, Y = 1. Z = X%Y = 0. Hence, we delete X=2(i.e. first element) from the array. A[] becomes {1}.
It can be seen that length of array has become 1 after 3 operations.
Input: N = 6, A[] = {1232, 452, 23491, 34099, 57341, 21488}
Output: 12
Approach: To solve the problem follow the below idea:
The idea is to use the set data structure to store the unique elements in the given array and repeatedly find the largest and smallest elements of the set using the end() and begin() functions, respectively. Then calculate the modulus of the largest and smallest elements, and insert the result into the set if the value of Z is not zero.
Below are the steps for the above approach:
- Declare a set S.
- Insert all the elements of array A[] into the S.
- Initialize a variable (say “ans”) with 0, that stores the total number of operations.
- Perform steps 5 to 9, till the size of set S becomes 1.
- Store the element from the beginning and end of the set in variables Y and X respectively.
- Find Z=X%Y.
- If Z is non-zero, insert it into the set.
- Erase X from the set.
- Increment “ans” in each operation.
- Return the ans.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int totalOperations( int N, int A[])
{
set< int > S;
for ( int i = 0; i < N; i++) {
S.insert(A[i]);
}
int ans = 0;
while (S.size() > 1) {
auto End = S.end();
auto Start = S.begin();
End--;
int X = *End;
int Y = *Start;
int Z = X % Y;
if (Z) {
S.insert(Z);
}
S.erase(S.find(X));
ans++;
}
return ans;
}
int32_t main()
{
int N = 3;
int A[] = { 2, 3, 6 };
int answer = totalOperations(N, A);
cout << answer << endl;
return 0;
}
|
Java
import java.util.*;
public class Main
{
static long totalOperations( int N, int [] A)
{
Set<Integer> S = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++) {
S.add(A[i]);
}
long ans = 0 ;
while (S.size() > 1 ) {
int X = Collections.max(S);
int Y = Collections.min(S);
int Z = X % Y;
if (Z != 0 ) {
S.add(Z);
}
S.remove(X);
ans++;
}
return ans;
}
public static void main(String[] args) {
int N = 3 ;
int [] A = { 2 , 3 , 6 };
long answer = totalOperations(N, A);
System.out.println(answer);
}
}
|
Python3
import math
def totalOperations(N, A):
S = set ()
for i in range (N):
S.add(A[i])
ans = 0
while len (S) > 1 :
X = max (S)
S.remove(X)
Y = min (S)
Z = X % Y
if Z ! = 0 :
S.add(Z)
ans + = 1
return ans
if __name__ = = '__main__' :
N = 3
A = [ 2 , 3 , 6 ]
answer = totalOperations(N, A)
print (answer)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static long totalOperations( int N, int [] A)
{
HashSet< int > S = new HashSet< int >();
for ( int i = 0; i < N; i++) {
S.Add(A[i]);
}
long ans = 0;
while (S.Count() > 1) {
int X = S.Max();
int Y = S.Min();
int Z = X % Y;
if (Z != 0) {
S.Add(Z);
}
S.Remove(X);
ans++;
}
return ans;
}
static public void Main()
{
int N = 3;
int [] A = { 2, 3, 6 };
long answer = totalOperations(N, A);
Console.WriteLine(answer);
}
}
|
Javascript
function totalOperations(N, A) {
let S = new Set();
for (let i = 0; i < N; i++) {
S.add(A[i]);
}
let ans = 0;
while (S.size > 1) {
let End = [...S].pop();
let Start = [...S][0];
let X = End;
let Y = Start;
let Z = X % Y;
if (Z) {
S.add(Z);
}
S. delete (X);
ans++;
}
return ans;
}
let N = 3;
let A = [2, 3, 6];
let answer = totalOperations(N, A);
console.log(answer);
|
Time Complexity: O(M+N*logN), where M is the number of operations performed
Auxiliary Space : O(N)
Share your thoughts in the comments
Please Login to comment...