Given an array **arr[]** consisting of** N** integers, the task is to to modify the array such that **arr[index] = index** using minimum number of operations of the following type:

- Choose any index
**i**and any integer**X**, and add**X**to all the elements in the range**[0, i]**. - Choose any index
**i**and any integer**X**, and change**arr[j]**to**arr[j] % X**where**0 ≤ j ≤ i**.

For each operation performed, print the following:

- For 1st operation: print
**1 i X** - For 2nd operation: print
**2 i X**

**Note:** Maximum **N + 1 **operations can be applied.

**Examples:**

Input:arr[] = {7, 6, 3}, N = 3

Output:

1 2 5

1 1 2

1 0 1

2 2 3

Explanation:

1st operation: Adding 5 to all the elements till index 2 modifies array to {12, 11, 8}.

2nd operation: Adding 2 to all the elements till index 1 modifies array to {14, 13, 8}.

3rd operation: Adding 1 to all the elements till index 0 modifies array to {15, 13, 8}.

4th operation: Adding 3 to all the elements till index 2 modifies array to {0, 1, 2}.

So after 4 operations, the required array is obtained.

Input:arr[] = {3, 4, 5, 6}, N = 4

Output:

1 3 5

1 2 4

1 1 4

1 0 4

2 3 4

**Approach:** This problem can be solved using Greedy Approach. Below are the steps:

- Apply
**N**operations of**type 1**where the**i**operation is to add^{th}**X =**upto index*( N + i – (arr[i] % N) )***i**by traversing the array in the reverse order. For every**i**operation, print^{th}**“1 i X”**. - After the above
**N**operations array will be of the form**arr[i] % N = i**for**0 ≤ i < N**. - One more operation has to be done which is to perform modulo of each array element with
**N**i.e., the operation**“2 (N-1) N”**. - After performing the above operations, for each index
**i**,**arr[i] = i**.

Below is the implementation of the above approach:

## C++

`// CPP program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function which makes the given ` `// array increasing using given ` `// operations ` `void` `makeIncreasing(` `int` `arr[], ` `int` `N) ` `{ ` ` ` ` ` `// The ith operation will be ` ` ` `// 1 i N + i - arr[i] % N ` ` ` `for` `(` `int` `x = N - 1; ` ` ` `x >= 0; x--) { ` ` ` ` ` `int` `val = arr[x]; ` ` ` ` ` `// Find the value to be added ` ` ` `// in each operation ` ` ` `int` `add = N - val % n + x; ` ` ` ` ` `// Print the operation ` ` ` `cout << ` `"1 "` `<< x ` ` ` `<< ` `" "` `<< add << endl; ` ` ` ` ` `// Performing the operation ` ` ` `for` `(` `int` `y = x; y >= 0; y--) { ` ` ` `arr[y] += add; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Last modulo with N operation ` ` ` `int` `mod = N; ` ` ` ` ` `cout << ` `"2 "` `<< N - 1 ` ` ` `<< ` `" "` `<< mod << endl; ` ` ` `for` `(` `int` `x = N - 1; ` ` ` `x >= 0; x--) { ` ` ` `arr[x] %= mod; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array arr[] ` ` ` `int` `arr[] = { 7, 6, 3 }; ` ` ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function Call ` ` ` `makeIncreasing(arr, N); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 2 5 1 1 2 1 0 1 2 2 3

**Time Complexity:** *O(N ^{2})*

**Auxiliary Space:**

*O(1)*

## Recommended Posts:

- Minimum operations required to make all the array elements equal
- Find the number of operations required to make all array elements Equal
- Find the minimum number of operations required to make all array elements equal
- Minimum changes required to make all element in an array equal
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Minimum number of operations required to make two strings equal
- Minimum no. of operations required to make all Array Elements Zero
- Minimize swaps to rearrange array such that parity of index and corresponding element is same
- Minimize increment/decrement of Array elements to make each modulo K equal
- Minimum operations required to make all Array elements divisible by K
- Minimum operations required to make all the elements distinct in an array
- Minimum increment or decrement operations required to make the array sorted
- Minimum operations to make all elements equal using the second array
- Find if it is possible to make all elements of an array equal by the given operations
- Minimum number of given operations required to reduce the array to 0 element
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Cost to make all array elements equal using given operations
- Minimum number of increment-other operations to make all array elements equal.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.