Minimize increment-decrement operation on adjacent elements to convert Array A to B
Last Updated :
26 Dec, 2022
Given two arrays A[] and B[] consisting of N positive integers, the task is to find the minimum number of increment and decrements of adjacent array elements of the array A[] required to convert it to the array B[]. If it is not possible, then print “-1”.
Examples:
Input: A[] = {1, 2}, B[] = {2, 1}
Output: 1
Explanation:
Perform the following operations on the array A[]:
- Consider the adjacent pairs (arr[0], arr[1]) and after incrementing and decrementing the pairs modifies the array A[] to {2, 1}.
After the above operation, the array A[] = {2, 1} is equal to B and the minimum number of operation required is 1.
Input: A[] = {1, 0, 0}, B[] = {2, 3, 1}
Output: -1
Approach: The given problem can be solved by using the Greedy Approach. Below are the steps:
- It can be observed that if the sum of the arrays A[] and B[] are not equal, then there is no valid sequence of operations exists. In this case, the answer will be -1.
- Otherwise, traverse the given array A[] and perform the following steps according to the following cases:
- Case where A[i] > B[i]:
- In this case keep a track of the extra values(i.e, A[i] – B[i]) Iterate using a pointer j from [i – 1, 0] and keep adding the extra values to the indices until A[j] < B[j] until the extra values gets exhausted (A[i] – B[i] becomes 0) or the end of the array is reached. Similarly, traverse to the right [i + 1, N – 1] and keep adding the extra values.
- Keep a track of the number of moves in a variable and to transfer 1 from index i to index j, the minimum number of operation need is |i – j|.
- Case where A[i] <= B[i]. In this case, iterate to the next value of i because these indices will be considered while iterating during the above-discussed case.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumMoves( int A[], int B[], int N)
{
int ans = 0;
int sum_A = 0, sum_B = 0;
for ( int i = 0; i < N; i++) {
sum_A += A[i];
}
for ( int i = 0; i < N; i++) {
sum_B += B[i];
}
if (sum_A != sum_B) {
return -1;
}
int i = 0;
while (i < N) {
if (A[i] > B[i]) {
int temp = A[i] - B[i];
int j = i - 1;
while (j >= 0 && temp > 0) {
if (B[j] > A[j]) {
int cnt = min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * abs (j - i));
}
j--;
}
if (temp > 0) {
int j = i + 1;
while (j < N && temp > 0) {
if (B[j] > A[j]) {
int cnt = min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * abs (j - i));
}
j++;
}
}
}
i++;
}
return ans;
}
int main()
{
int A[] = { 1, 5, 7 };
int B[] = { 13, 0, 0 };
int N = sizeof (A) / sizeof ( int );
cout << minimumMoves(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minimumMoves( int A[], int B[], int N)
{
int ans = 0 ;
int sum_A = 0 , sum_B = 0 ;
for ( int i = 0 ; i < N; i++) {
sum_A += A[i];
}
for ( int i = 0 ; i < N; i++) {
sum_B += B[i];
}
if (sum_A != sum_B) {
return - 1 ;
}
int i = 0 ;
while (i < N) {
if (A[i] > B[i]) {
int temp = A[i] - B[i];
int j = i - 1 ;
while (j >= 0 && temp > 0 ) {
if (B[j] > A[j]) {
int cnt
= Math.min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.abs(j - i));
}
j--;
}
if (temp > 0 ) {
j = i + 1 ;
while (j < N && temp > 0 ) {
if (B[j] > A[j]) {
int cnt = Math.min(
temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.abs(j - i));
}
j++;
}
}
}
i++;
}
return ans;
}
public static void main(String[] args)
{
int A[] = { 1 , 5 , 7 };
int B[] = { 13 , 0 , 0 };
int N = A.length;
System.out.println(minimumMoves(A, B, N));
}
}
|
Python3
def minimumMoves(A, B, N):
ans = 0
sum_A = 0
sum_B = 0
for i in range (N):
sum_A + = A[i]
for i in range (N):
sum_B + = B[i]
if (sum_A ! = sum_B):
return - 1
i = 0
while (i < N):
if (A[i] > B[i]):
temp = A[i] - B[i]
j = i - 1
while (j > = 0 and temp > 0 ):
if (B[j] > A[j]):
cnt = min (temp, (B[j] - A[j]))
A[j] + = cnt
temp - = cnt
ans + = (cnt * abs (j - i))
j - = 1
if (temp > 0 ):
j = i + 1
while (j < N and temp > 0 ):
if (B[j] > A[j]):
cnt = min (temp, (B[j] - A[j]))
A[j] + = cnt
temp - = cnt
ans + = (cnt * abs (j - i))
j + = 1
i + = 1
return ans
if __name__ = = '__main__' :
A = [ 1 , 5 , 7 ]
B = [ 13 , 0 , 0 ]
N = len (A)
print (minimumMoves(A, B, N))
|
C#
using System;
public class GFG
{
static int minimumMoves( int []A, int []B, int N)
{
int ans = 0;
int sum_A = 0, sum_B = 0;
for ( int i = 0; i < N; i++) {
sum_A += A[i];
}
for ( int i = 0; i < N; i++) {
sum_B += B[i];
}
if (sum_A != sum_B) {
return -1;
}
int k = 0;
while (k < N) {
if (A[k] > B[k]) {
int temp = A[k] - B[k];
int j = k - 1;
while (j >= 0 && temp > 0) {
if (B[j] > A[j]) {
int cnt
= Math.Min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.Abs(j - k));
}
j--;
}
if (temp > 0) {
j = k + 1;
while (j < N && temp > 0) {
if (B[j] > A[j]) {
int cnt = Math.Min(
temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.Abs(j - k));
}
j++;
}
}
}
k++;
}
return ans;
}
public static void Main( string [] args)
{
int []A = { 1, 5, 7 };
int []B = { 13, 0, 0 };
int N = A.Length;
Console.WriteLine(minimumMoves(A, B, N));
}
}
|
Javascript
<script>
function minimumMoves(A, B, N)
{
var ans = 0;
var i;
var sum_A = 0, sum_B = 0;
for (i = 0; i < N; i++) {
sum_A += A[i];
}
for (i = 0; i < N; i++) {
sum_B += B[i];
}
if (sum_A != sum_B) {
return -1;
}
var i = 0;
while (i < N) {
if (A[i] > B[i]) {
var temp = A[i] - B[i];
var j = i - 1;
while (j >= 0 && temp > 0) {
if (B[j] > A[j]) {
var cnt = Math.min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.abs(j - i));
}
j--;
}
if (temp > 0) {
var j = i + 1;
while (j < N && temp > 0) {
if (B[j] > A[j]) {
var cnt = Math.min(temp, (B[j] - A[j]));
A[j] += cnt;
temp -= cnt;
ans += (cnt * Math.abs(j - i));
}
j++;
}
}
}
i++;
}
return ans;
}
var A = [1, 5, 7];
var B = [13, 0, 0];
var N = A.length;
document.write(minimumMoves(A, B, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...