Techno Blender
Digitally Yours.

Sort the elements by minimum number of operations

0 19


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given two positive integer arrays X[] and Y[] of size N, Where all elements of X[] are distinct. Considering all the elements of X[] are lying side by side initially on a line, the task is to find the minimum number of operations required such that elements in X[] becomes in increasing order where in one operation you can increase each element of X[] by their respective values index in Y[] (first element of X[] element can be incremented by the first element of Y[] or position of the second element of X[] can be incremented by the second element of Y[] or so on….).

Examples:

Input: N = 3, X[] = {3, 2, 1}, Y[] = {1, 1, 1}
Output: 6
Explanation: According to the problem statement:

  • X[1] = 3, It’s position can be incremented by Y[1] = 1.
  • X[2] = 2, It’s position can be incremented by Y[2] = 1.
  • X[3] = 1, It’s position can be incremented by Y[3] = 1.
  • Operations:
    • In two operations, X[2] = 2, incremented its position by Y[2] = 1 in each operation from position 1 to 2 and then 2 to 3 by using operations one by one.
    • In four operations, X[1] = 3, incremented its position by Y[1] = 1 in each operation from position 0 to 1, 1 to 2, 2 to 3 and then 3 to 4 in last operation. Each operation is performed one by one.
    • Now, It can be verified that all the elements of X[] are in sorted order of their values, for this case Minimum number of operations are=2 + 4 = 6, Which is minimum possible.     

Teset case 1

  

Input: N = 4, X[] = {2, 1, 4, 3}, Y[] = {4, 1, 2, 4}
Output: 5
Explanation: According to the problem statement:

  •  X[1] = 2, It’s position can be incremented by Y[1] = 4.
  •  X[2] = 1, It’s position can be incremented by Y[2] = 1.
  •  X[3] = 4, It’s position can be incremented by Y[3] = 2.
  •  X[4] = 3, It’s position can be incremented by Y[4] = 4.
  • Operations:
    • In one operation, X[4] = 3, incremented its position by Y[4] = 4, in each operation from position 3 to 7.
    • In one operation, X[1] = 2, incremented its position by Y[1] = 4 in each operation from position 0 to 3.
    • In three operations, X[3] = 4, incremented its position by Y[3] = 2 in each operation from position 2 to 4, 4 to 6 and then 6 to 8 in each operation one by one.
    • Therefore, Total minimum number of operations for this case are= 1 + 1 + 3 = 5. It can be verified that all the elements of X[] are in sorted order of their values after performing above operations.

Test case 2

Approach: Implement the idea below to solve the problem

The problem is observation based and can be solve by using Greedy Technique. The basic idea of the problem is that, we have to choose optimal element to increment it’s position in ach operation.        

Steps were taken to solve the problem:

  • Create two arrays positions[] and temp_length[] of length N for storing positions and temporary lengths of elements of X[].
  • Create integer variable operations and initialize it equal to 0.
  • Initialize positions[] with positions[X[i] – 1] = i,   by using a loop from i=0 to less than N.
  • Initialize temp_length[] with temp_lengthX[i] – 1] = Y[i],  by using a loop from i=0 to less than N.
  • Iterate from i = 1 to less than N using loops and follow the below-mentioned steps under the scope of the loop:
    • While(position[i] ≤ position[i-1]), till then position[i] += temp_length[i] and operations++.  
  • Return operations.  

Below is the code to implement the approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

        throws java.lang.Exception

    {

  

        

        int N = 4;

  

        

        int X[] = { 2, 1, 4, 3 };

  

        

        int Y[] = { 4, 1, 2, 4 };

  

        

        System.out.println(min_operations(N, X, Y));

    }

  

    

    

    static int min_operations(int N, int[] X, int[] Y)

    {

  

        

        

        int[] position = new int[N];

  

        

        int[] temp_length = new int[N];

  

        

        

        int operations = 0;

  

        

        for (int i = 0; i < N; i++) {

            position[X[i] - 1] = i;

        }

  

        

        

        for (int i = 0; i < N; i++) {

            temp_length[X[i] - 1] = Y[i];

        }

  

        

        

        for (int i = 1; i < N; i++) {

  

            while (position[i] <= position[i - 1]) {

                position[i] += temp_length[i];

                operations++;

            }

        }

  

        

        return operations;

    }

}

Time Complexity: O(N)
Auxiliary Space: O(N)


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given two positive integer arrays X[] and Y[] of size N, Where all elements of X[] are distinct. Considering all the elements of X[] are lying side by side initially on a line, the task is to find the minimum number of operations required such that elements in X[] becomes in increasing order where in one operation you can increase each element of X[] by their respective values index in Y[] (first element of X[] element can be incremented by the first element of Y[] or position of the second element of X[] can be incremented by the second element of Y[] or so on….).

Examples:

Input: N = 3, X[] = {3, 2, 1}, Y[] = {1, 1, 1}
Output: 6
Explanation: According to the problem statement:

  • X[1] = 3, It’s position can be incremented by Y[1] = 1.
  • X[2] = 2, It’s position can be incremented by Y[2] = 1.
  • X[3] = 1, It’s position can be incremented by Y[3] = 1.
  • Operations:
    • In two operations, X[2] = 2, incremented its position by Y[2] = 1 in each operation from position 1 to 2 and then 2 to 3 by using operations one by one.
    • In four operations, X[1] = 3, incremented its position by Y[1] = 1 in each operation from position 0 to 1, 1 to 2, 2 to 3 and then 3 to 4 in last operation. Each operation is performed one by one.
    • Now, It can be verified that all the elements of X[] are in sorted order of their values, for this case Minimum number of operations are=2 + 4 = 6, Which is minimum possible.     

Teset case 1

  

Input: N = 4, X[] = {2, 1, 4, 3}, Y[] = {4, 1, 2, 4}
Output: 5
Explanation: According to the problem statement:

  •  X[1] = 2, It’s position can be incremented by Y[1] = 4.
  •  X[2] = 1, It’s position can be incremented by Y[2] = 1.
  •  X[3] = 4, It’s position can be incremented by Y[3] = 2.
  •  X[4] = 3, It’s position can be incremented by Y[4] = 4.
  • Operations:
    • In one operation, X[4] = 3, incremented its position by Y[4] = 4, in each operation from position 3 to 7.
    • In one operation, X[1] = 2, incremented its position by Y[1] = 4 in each operation from position 0 to 3.
    • In three operations, X[3] = 4, incremented its position by Y[3] = 2 in each operation from position 2 to 4, 4 to 6 and then 6 to 8 in each operation one by one.
    • Therefore, Total minimum number of operations for this case are= 1 + 1 + 3 = 5. It can be verified that all the elements of X[] are in sorted order of their values after performing above operations.

Test case 2

Approach: Implement the idea below to solve the problem

The problem is observation based and can be solve by using Greedy Technique. The basic idea of the problem is that, we have to choose optimal element to increment it’s position in ach operation.        

Steps were taken to solve the problem:

  • Create two arrays positions[] and temp_length[] of length N for storing positions and temporary lengths of elements of X[].
  • Create integer variable operations and initialize it equal to 0.
  • Initialize positions[] with positions[X[i] – 1] = i,   by using a loop from i=0 to less than N.
  • Initialize temp_length[] with temp_lengthX[i] – 1] = Y[i],  by using a loop from i=0 to less than N.
  • Iterate from i = 1 to less than N using loops and follow the below-mentioned steps under the scope of the loop:
    • While(position[i] ≤ position[i-1]), till then position[i] += temp_length[i] and operations++.  
  • Return operations.  

Below is the code to implement the approach:

Java

  

import java.io.*;

import java.lang.*;

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

        throws java.lang.Exception

    {

  

        

        int N = 4;

  

        

        int X[] = { 2, 1, 4, 3 };

  

        

        int Y[] = { 4, 1, 2, 4 };

  

        

        System.out.println(min_operations(N, X, Y));

    }

  

    

    

    static int min_operations(int N, int[] X, int[] Y)

    {

  

        

        

        int[] position = new int[N];

  

        

        int[] temp_length = new int[N];

  

        

        

        int operations = 0;

  

        

        for (int i = 0; i < N; i++) {

            position[X[i] - 1] = i;

        }

  

        

        

        for (int i = 0; i < N; i++) {

            temp_length[X[i] - 1] = Y[i];

        }

  

        

        

        for (int i = 1; i < N; i++) {

  

            while (position[i] <= position[i - 1]) {

                position[i] += temp_length[i];

                operations++;

            }

        }

  

        

        return operations;

    }

}

Time Complexity: O(N)
Auxiliary Space: O(N)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment