Given two arrays A[] and B[] consisting of N positive integers such that each array element A[i] is placed at the ith position on the number line, the task is to find the minimum number of operations required to sort the array elements arranged in the number line. In each operation any array element A[i] can move to the position (i + B[i])th on the number line. Two or more elements can come to the same number line.
Examples:
Input: A[] = {2, 1, 4, 3}, B[] = {4, 1, 2, 4}
Output: 5
Explanation:

Initially array 2, 1, 4, 3 are placed on the number line at 0, 1, 2, 3 positions respectively.
Operation 1: Move arr[0](= 2) by move[0](= 4). Now the element are arranged as {1, 4, 3, 2} at indices on the number line is {1, 2, 3, 4} respectively.
Operation 2: Move arr[3](= 3) by move[3](= 4). Now the element are arranged as {1, 4, 2, 3} at indices on the number line is {1, 2, 4, 7} respectively.
Operation 3: Move arr[2](= 4) move[2](= 2). Now the element are arranged as {1, 2, 4, 3} at indices on the number line is {1, 4, 4, 7} respectively.
Operation 4: Move arr[3](= 4) move[2](= 2). Now the element are arranged as {1, 2, 3, 4} at indices on the number line is {1, 4, 6, 7} respectively.
Operation 5: In first operation move arr[0](= 2) i.e., 2 by move[0](= 4). Now the element are arranged as {1, 4, 3, 2} at indices on the number line is {1, 4, 7, 8} respectively.
Input: A[] = {1, 2, 3, 4}, B[] = {4, 1, 2, 4}
Output: 0
Approach: The given problem can be solved using the Greedy Approach by moving the greater element one by one to its next possible index and then find the minimum of all the operations required. Follow the steps below to solve the given problem:
- Initialize a 2D vectors, say arr[] such that each ith element represents the element, corresponding moves, and the current position as {arr[i], A[i], current_position}.
- Sort the array arr[] in ascending order.
- Initialize two variables, say cnt and f, and Mark count as 0 and flag as 1 to store the number of required operations.
- Iterate until F is not equal to 1 and perform the following steps:
- Update the value of F equals 0.
- For each element in vector arr[] and If the value of arr[i][2] is at least arr[i + 1][2], then increment the count by 1, f = 1 and the current position of the (i + 1)th element i.e., (arr[i + 1][2]) by arr[i + 1][1].
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minHits( int arr[], int move[],
int n)
{
vector<vector< int > > V(n, vector< int >(3));
for ( int i = 0; i < n; i++) {
V[i] = { arr[i], move[i], i };
}
sort(V.begin(), V.end());
int cnt = 0;
int f = 1;
while (f == 1) {
f = 0;
for ( int i = 0; i < n - 1; i++) {
if (V[i][2] >= V[i + 1][2]) {
V[i + 1][2] += V[i + 1][1];
cnt++;
f = 1;
break ;
}
}
}
return cnt;
}
int main()
{
int A[] = { 2, 1, 4, 3 };
int B[] = { 4, 1, 2, 4 };
int N = sizeof (A) / sizeof (A[0]);
cout << minHits(A, B, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int minHits( int arr[], int move[],
int n)
{
int [][] V = new int [n][ 3 ];
for ( int i = 0 ; i < n; i++) {
V[i][ 0 ] = arr[i];
V[i][ 1 ] = move[i];
V[i][ 2 ] = i;
}
Arrays.sort(V, (a, b) -> a[ 0 ] - b[ 0 ]);
int cnt = 0 ;
int f = 1 ;
while (f == 1 ) {
f = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (V[i][ 2 ] >= V[i + 1 ][ 2 ]) {
V[i + 1 ][ 2 ] += V[i + 1 ][ 1 ];
cnt++;
f = 1 ;
break ;
}
}
}
return cnt;
}
public static void main(String[] args)
{
int A[] = { 2 , 1 , 4 , 3 };
int B[] = { 4 , 1 , 2 , 4 };
int N = A.length;
System.out.println(minHits(A, B, N));
}
}
|
Python3
def minHits(arr, move, n):
temp = [ 0 for i in range ( 3 )]
V = [temp for i in range (n)]
for i in range (n):
V[i] = [arr[i], move[i], i]
V.sort()
cnt = 0
f = 1
while (f = = 1 ):
f = 0
for i in range (n - 1 ):
if (V[i][ 2 ] > = V[i + 1 ][ 2 ]):
V[i + 1 ][ 2 ] + = V[i + 1 ][ 1 ]
cnt + = 1
f = 1
break
return cnt
if __name__ = = '__main__' :
A = [ 2 , 1 , 4 , 3 ]
B = [ 4 , 1 , 2 , 4 ]
N = len (A)
print (minHits(A, B, N))
|
C#
using System;
using System.Linq;
class GFG
{
public static int minHits( int [] arr, int [] move, int n)
{
int [][] V = new int [n][];
for ( int i = 0; i < n; i++)
{
V[i] = new int [] { arr[i], move[i], i };
}
Array.Sort(V, (a, b) => a[0] - b[0]);
int cnt = 0;
int f = 1;
while (f == 1)
{
f = 0;
for ( int i = 0; i < n - 1; i++)
{
if (V[i][2] >= V[i + 1][2])
{
V[i + 1][2] += V[i + 1][1];
cnt++;
f = 1;
break ;
}
}
}
return cnt;
}
public static void Main( string [] args)
{
int [] A = new int [] { 2, 1, 4, 3 };
int [] B = new int [] { 4, 1, 2, 4 };
int N = A.Length;
Console.WriteLine(minHits(A, B, N));
}
}
|
Javascript
<script>
function minHits(arr, move, n) {
let V = new Array(n).fill(0).map(() => new Array(3));
for (let i = 0; i < n; i++) {
V[i] = [arr[i], move[i], i];
}
V.sort((a, b) => a[0] - b[0]);
let cnt = 0;
let f = 1;
while (f == 1) {
f = 0;
for (let i = 0; i < n - 1; i++) {
if (V[i][2] >= V[i + 1][2]) {
V[i + 1][2] += V[i + 1][1];
cnt++;
f = 1;
break ;
}
}
}
return cnt;
}
let A = [2, 1, 4, 3];
let B = [4, 1, 2, 4];
let N = A.length;
document.write(minHits(A, B, N));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)