Open In App

Minimize operations required to make each element of Array equal to it’s index value

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

  1. Choose any index i and any integer X, and add X to all the elements in the range [0, i].
  2. 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: 

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: 

  1. Apply N operations of type 1 where the ith operation is to add X = ( N + i – (arr[i] % N) ) upto index i by traversing the array in the reverse order. For every ith operation, print “1 i X”.
  2. After the above N operations array will be of the form arr[i] % N = i for 0 ? i < N.
  3. 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”.
  4. After performing the above operations, for each index i, arr[i] = i.

Below is the implementation of the above approach:




// 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);
}




// Java Program for the above approach
import java.util.*;
 
class GFG
{
    // Function which makes the given
    // array increasing using given
    // operations
    static 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
            System.out.println("1"
                               + " " + x + " " + add);
 
            // Performing the operation
            for (int y = x; y >= 0; y--)
            {
                arr[y] += add;
            }
        }
 
        // Last modulo with N operation
        int mod = N;
 
        System.out.println("2"
                           + " " + (N - 1) + " " + mod);
 
        for (int x = N - 1; x >= 0; x--)
        {
            arr[x] %= mod;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given array arr[]
        int arr[] = { 7, 6, 3 };
        int N = arr.length;
        // Function Call
        makeIncreasing(arr, N);
    }
}




# python Program for the above problem
# Function which makes the given
# array increasing using given
# operations
def makeIncreasing(arr, N):
     
    # The ith operation will be
    # 1 i N + i - arr[i] % N
    for x in range( N - 1 -1, -1):
        val = arr[x]
         
        # Find the value to be added
        # in each operation
        add = N - val % N + x
         
        # Print the operation
        print("1" + " " + str(x) + " " + str(add))
         
        # Performing the operation
        for y in range(x, -1, -1):
            arr[y] += add
     
    # Last modulo with N operation
    mod = N;
    print("2" + " " + str(N - 1) + " " + str(mod))
    for i in range( N - 1, -1, -1):
        arr[i] = arr[i] % mod
 
# Driver code
 
# Given array arr
arr = [ 7, 6, 3 ]
 
N = len(arr)
 
# Function Call
makeIncreasing(arr, N)




// C# Program for the above approach
using System;
 
class GFG
{
    // Function which makes the given
    // array increasing using given
    // operations
    static 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
            Console.WriteLine("1"
                              + " " + x + " " + add);
 
            // Performing the operation
            for (int y = x; y >= 0; y--)
            {
                arr[y] += add;
            }
        }
 
        // Last modulo with N operation
        int mod = N;
 
        Console.WriteLine("2"
                          + " " + (N - 1) + " " + mod);
 
        for (int x = N - 1; x >= 0; x--) {
            arr[x] %= mod;
        }
    }
 
    // Driver code
    public static void Main()
    {
        // Given array arr[]
        int[] arr = new int[] { 7, 6, 3 };
 
        int N = arr.Length;
 
        // Function Call
        makeIncreasing(arr, N);
    }
}




<script>
 
      // JavaScript Program for the above approach
       
      // Function which makes the given
      // array increasing using given
      // operations
      function makeIncreasing(arr, N)
      {
        // The ith operation will be
        // 1 i N + i - arr[i] % N
        for (var x = N - 1; x >= 0; x--) {
          var val = arr[x];
 
          // Find the value to be added
          // in each operation
          var add = N - (val % N) + x;
 
          // Print the operation
          document.write("1" + " " + x + " "
          + add + "<br>");
 
          // Performing the operation
          for (var y = x; y >= 0; y--) {
            arr[y] += add;
          }
        }
 
        // Last modulo with N operation
        var mod = N;
 
        document.write("2" + " " + (N - 1) + " "
        + mod + "<br>");
 
        for (var x = N - 1; x >= 0; x--) {
          arr[x] %= mod;
        }
      }
 
      // Driver code
       
      // Given array arr[]
      var arr = [7, 6, 3];
      var N = arr.length;
 
      // Function Call
      makeIncreasing(arr, N);
       
</script>

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

Time Complexity: O(N2) 
Auxiliary Space: O(1)


Article Tags :