Minimize increment-decrement operation on adjacent elements to convert Array A to B
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 is 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.
- Case where A[i] > B[i]:
Below is the implementation of the above approach:
C++
// C++ Program of the above approach#include <bits/stdc++.h>using namespace std;// Function to calculate the minimum// number of operations to convert// array A to array B by incrementing// and decrementing adjacent elementsint minimumMoves(int A[], int B[], int N){ // Stores the final count int ans = 0; // Stores the sum of array A // and B respectivelly 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]; } // Check of the sums are unequall if (sum_A != sum_B) { return -1; } // Pointer to iterate through array int i = 0; while (i < N) { // Case 1 where A[i] > B[i] if (A[i] > B[i]) { // Stores the extra values // for the current index int temp = A[i] - B[i]; int j = i - 1; // Iterate the array from [i-1, 0] while (j >= 0 && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = min(temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * abs(j - i)); } j--; } // Iterate the array in right // direction id A[i]-B[i] > 0 if (temp > 0) { int j = i + 1; // Iterate the array from [i+1, n-1] while (j < N && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = min(temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * abs(j - i)); } j++; } } } i++; } // Return Answer return ans;}// Driver Codeint main(){ int A[] = { 1, 5, 7 }; int B[] = { 13, 0, 0 }; int N = sizeof(A) / sizeof(int); // Function Call cout << minimumMoves(A, B, N); return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG{ // Function to calculate the minimum // number of operations to convert // array A to array B by incrementing // and decrementing adjacent elements static int minimumMoves(int A[], int B[], int N) { // Stores the final count int ans = 0; // Stores the sum of array A // and B respectivelly 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]; } // Check of the sums are unequall if (sum_A != sum_B) { return -1; } // Pointer to iterate through array int i = 0; while (i < N) { // Case 1 where A[i] > B[i] if (A[i] > B[i]) { // Stores the extra values // for the current index int temp = A[i] - B[i]; int j = i - 1; // Iterate the array from [i-1, 0] while (j >= 0 && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = Math.min(temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * Math.abs(j - i)); } j--; } // Iterate the array in right // direction id A[i]-B[i] > 0 if (temp > 0) { j = i + 1; // Iterate the array from [i+1, n-1] while (j < N && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = Math.min( temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * Math.abs(j - i)); } j++; } } } i++; } // Return Answer return ans; } // Driver Code public static void main(String[] args) { int A[] = { 1, 5, 7 }; int B[] = { 13, 0, 0 }; int N = A.length; // Function Call System.out.println(minimumMoves(A, B, N)); }}// This code is contributed by Potta Lokesh |
C#
// C# program for the above approachusing System;public class GFG{ // Function to calculate the minimum // number of operations to convert // array A to array B by incrementing // and decrementing adjacent elements static int minimumMoves(int []A, int []B, int N) { // Stores the final count int ans = 0; // Stores the sum of array A // and B respectivelly 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]; } // Check of the sums are unequall if (sum_A != sum_B) { return -1; } // Pointer to iterate through array int k = 0; while (k < N) { // Case 1 where A[i] > B[i] if (A[k] > B[k]) { // Stores the extra values // for the current index int temp = A[k] - B[k]; int j = k - 1; // Iterate the array from [i-1, 0] while (j >= 0 && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = Math.Min(temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * Math.Abs(j - k)); } j--; } // Iterate the array in right // direction id A[i]-B[i] > 0 if (temp > 0) { j = k + 1; // Iterate the array from [i+1, n-1] while (j < N && temp > 0) { if (B[j] > A[j]) { // Stores the count of // values being transfered // from A[i] to A[j] int cnt = Math.Min( temp, (B[j] - A[j])); A[j] += cnt; temp -= cnt; // Add operation count ans += (cnt * Math.Abs(j - k)); } j++; } } } k++; } // Return Answer return ans; } // Driver Code public static void Main(string[] args) { int []A = { 1, 5, 7 }; int []B = { 13, 0, 0 }; int N = A.Length; // Function Call Console.WriteLine(minimumMoves(A, B, N)); }}// This code is contributed by AnkThon |
19
Time Complexity: O(N2)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


