Given an array A[], you may perform at most 40 operations to transform it. In each operation, choose a subarray arr[l … r], find the smallest positive value x in that subarray, and update every element in the range to arr[i] % x.
Find any sequence of such operations (up to 40) that makes all elements of the array equal.
Examples:
Input: A[] = [2, 2, 2, 3, 3, 3]
Output: [[1, 3], [4, 6]]
Explanation: Choosing subarray A[1 … 3] gives the smallest positive value x = 2, so the first three elements become A[i] % 2 = 0.
Choosing subarray A[4 … 6] gives x = 3, so the remaining elements also become 0. All elements become equal after two operations.
Input: A[] = [1, 6]
Output: [[1, 2]]
Explanation: Selecting the subarray A[1 … 2] gives x = 1, so both elements become 0.
A single operation is enough.
[Approach] Using Euclidean Algorithm - O(n) Time and O(1) Space
The idea comes from the Euclidean algorithm. Replacing A[i] with A[i] % x is like a gcd step, which quickly reduces larger numbers. By always taking x as the smallest positive number in the array, all larger elements shrink rapidly.
In the Euclidean algorithm, the number of modulo steps required to reach the gcd is at most logarithmic in the maximum number. Since the array values are ≤ 1e9, at most about 30–35 modulo steps are enough to reduce all numbers to the same value. Adding a few extra steps for safety, 40 operations are sufficient to guarantee convergence.
Repeating this for the whole array applies gcd-style reductions, and within these steps, all elements become equal.
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> solve(int N, vector<int> A) {
vector<vector<int>> ops;
// perform at most 40 operations
for (int t = 0; t < 40; t++) {
// check if all elements are equal
bool ok = true;
for (int i = 1; i < N; i++) {
if (A[i] != A[0]) {
ok = false;
break;
}
}
if (ok) break;
// find smallest positive element
int x = INT_MAX;
for (int i = 0; i < N; i++) {
if (A[i] > 0) x = min(x, A[i]);
}
if (x == INT_MAX) break;
// find segment where x occurs
int l = 0, r = N-1;
// apply modulo only on that segment
for (int i = l; i <= r; i++) {
A[i] = A[i] % x;
}
ops.push_back({l + 1, r + 1});
}
return ops;
}
int main() {
int N = 6;
vector<int> A = {2, 2, 2, 3, 3, 3};
vector<vector<int>> res = solve(N, A);
for (auto &p : res) {
cout << p[0] << " " << p[1] << "\n";
}
}
Java
import java.util.*;
class GFG {
static ArrayList<ArrayList<Integer>> solve(int N, int[] A) {
ArrayList<ArrayList<Integer>> ops = new ArrayList<>();
// perform at most 40 operations
for (int t = 0; t < 40; t++) {
// check if all elements are equal
boolean ok = true;
for (int i = 1; i < N; i++) {
if (A[i] != A[0]) {
ok = false;
break;
}
}
if (ok) break;
// find smallest positive element
int x = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
if (A[i] > 0) x = Math.min(x, A[i]);
}
if (x == Integer.MAX_VALUE) break;
// full range
int l = 0, r = N - 1;
// apply modulo
for (int i = l; i <= r; i++) {
A[i] = A[i] % x;
}
ArrayList<Integer> range = new ArrayList<>();
range.add(l + 1);
range.add(r + 1);
ops.add(range);
}
return ops;
}
public static void main(String[] args) {
int N = 6;
int[] A = {2, 2, 2, 3, 3, 3};
ArrayList<ArrayList<Integer>> res = solve(N, A);
for (ArrayList<Integer> v : res) {
System.out.println(v.get(0) + " " + v.get(1));
}
}
}
Python
def solve(N, A):
ops = []
# perform at most 40 operations
for _ in range(40):
# check if all elements are equal
if all(A[i] == A[0] for i in range(N)):
break
# find smallest positive
x = min((v for v in A if v > 0), default=None)
if x is None:
break
l, r = 0, N - 1
# apply modulo
for i in range(l, r + 1):
A[i] = A[i] % x
ops.append([l + 1, r + 1])
return ops
if __name__ == '__main__':
N = 6
A = [2, 2, 2, 3, 3, 3]
res = solve(N, A)
for l, r in res:
print(l, r)
C#
using System;
using System.Collections.Generic;
class GFG {
static List<List<int>> solve(int N, int[] A) {
List<List<int>> ops = new List<List<int>>();
for (int t = 0; t < 40; t++) {
// check if all elements are equal
bool equal = true;
for (int i = 1; i < N; i++) {
if (A[i] != A[0])
{
equal = false;
break;
}
}
if (equal) break;
// find smallest positive element
int minPos = int.MaxValue;
for (int i = 0; i < N; i++) {
if (A[i] > 0) minPos = Math.Min(minPos, A[i]);
}
if (minPos == int.MaxValue) break;
// apply modulo to entire array
for (int i = 0; i < N; i++) A[i] %= minPos;
// store operation (1-based indices)
ops.Add(new List<int> { 1, N });
}
return ops;
}
static void Main() {
int[] A = { 2, 2, 2, 3, 3, 3 };
int N = A.Length;
List<List<int>> res = solve(N, A);
foreach (var op in res)
{
Console.WriteLine(op[0] + " " + op[1]);
}
}
}
JavaScript
function solve(N, A) {
let ops = [];
// perform at most 40 operations
for (let t = 0; t < 40; t++) {
// check if all equal
let ok = true;
for (let i = 1; i < N; i++) {
if (A[i] !== A[0]) {
ok = false;
break;
}
}
if (ok) break;
// find smallest positive
let x = Infinity;
for (let i = 0; i < N; i++) {
if (A[i] > 0) x = Math.min(x, A[i]);
}
if (x === Infinity) break;
let l = 0, r = N - 1;
// apply modulo
for (let i = l; i <= r; i++) {
A[i] = A[i] % x;
}
ops.push([l + 1, r + 1]);
}
return ops;
}
// Driver Code
let N = 6;
let A = [2, 2, 2, 3, 3, 3];
let res = solve(N, A);
for (let p of res) {
console.log(p[0], p[1]);
}
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem