Sort the elements by minimum number of operations
Last Updated :
20 Mar, 2023
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:
C++
#include <bits/stdc++.h>
using namespace std;
int min_operations( int N, int X[], int Y[])
{
int position[N];
int temp_length[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;
}
int main() {
int N = 4;
int X[] = { 2, 1, 4, 3 };
int Y[] = { 4, 1, 2, 4 };
cout << min_operations(N, X, Y) << endl;
}
|
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;
}
}
|
Python3
def min_operations(N, X, Y):
position = [ 0 ] * N
temp_length = [ 0 ] * N
operations = 0
for i in range ( 0 , N):
position[X[i] - 1 ] = i
for i in range ( 0 , N):
temp_length[X[i] - 1 ] = Y[i]
for i in range ( 1 , N):
while position[i] < = position[i - 1 ]:
position[i] + = temp_length[i]
operations + = 1
return operations
if __name__ = = "__main__" :
N = 4
X = [ 2 , 1 , 4 , 3 ]
Y = [ 4 , 1 , 2 , 4 ]
print (min_operations(N, X, Y))
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 4;
int [] X = { 2, 1, 4, 3 };
int [] Y = { 4, 1, 2, 4 };
Console.WriteLine(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;
}
}
|
Javascript
function min_operations( N, X, Y)
{
let position= new Array(N);
let temp_length= new Array(N);
let operations = 0;
for (let i = 0; i < N; i++) {
position[X[i] - 1] = i;
}
for (let i = 0; i < N; i++) {
temp_length[X[i] - 1] = Y[i];
}
for (let i = 1; i < N; i++) {
while (position[i] <= position[i - 1]) {
position[i] += temp_length[i];
operations++;
}
}
return operations;
}
let N = 4;
let X = [ 2, 1, 4, 3 ];
let Y = [ 4, 1, 2, 4 ];
console.log(min_operations(N, X, Y));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...