# Minimum number of operations to convert array A to array B by adding an integer into a subarray

Given two arrays **A[]** and **B[]** of length **N**, the task is to find the minimum number of operations in which the array **A** can be converted into array **B** where each operation consists of adding an integer K into a subarray from L to R.

**Examples:**

Input:A[] = {3, 7, 1, 4, 1, 2}, B[] = {3, 7, 3, 6, 3, 2}

Output:1

Explanation:

In the above given example only one operation is required to convert from A to B: L = 3, R = 5 and K = 2

Array after the following operation:

Index 0:A[0] = 3, B[0] = 3

Index 1:A[1] = 7, B[1] = 7

Index 2:A[2] = 1 + 2 = 3, B[2] = 3

Index 3:A[3] = 4 + 2 = 6, B[3] = 6

Index 4:A[4] = 1 + 2 = 3, B[4] = 3

Index 5:A[5] = 2, B[5] = 2

Input:A[] = {1, 1, 1, 1, 1}, B[] = {1, 2, 1, 3, 1}

Output:2

Explanation:

In the above given example only one operation is required to convert from A to B –

Operation 1:Add 1 to L = 2 to R = 2

Operation 2:Add 2 to L = 4 to R = 4

**Approach:** The idea is to count the consecutive elements, in array A, having equal difference with the corresponding element in the array B.

- Find the difference of the corresponding element from the array A and B:
Difference = A[i] - B[i]

- If the difference of the corresponding elements is equal to 0, then the continue for checking the next index.
- Otherwise, Increase the index until the difference between consecutive elements is not equal to the previous difference of the consecutive elements
- Increment the count by 1, If all the index are iterated those are having a same difference.
- At the end, return the count as the minimum number of operations.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum number of operations in ` `// which the array A can be converted ` `// to another array B ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `// number of operations in which ` `// array A can be converted to array B ` `void` `checkArray(` `int` `a[], ` `int` `b[], ` `int` `n) ` `{ ` ` ` `int` `operations = 0; ` ` ` `int` `i = 0; ` ` ` ` ` `// Loop to iterate over the array ` ` ` `while` `(i < n) { ` ` ` ` ` `// if both elements are equal ` ` ` `// then move to next element ` ` ` `if` `(a[i] - b[i] == 0) { ` ` ` `i++; ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Calculate the difference ` ` ` `// between two elements ` ` ` `int` `diff = a[i] - b[i]; ` ` ` `i++; ` ` ` ` ` `// loop while the next pair of ` ` ` `// elements have same difference ` ` ` `while` `(i < n && ` ` ` `a[i] - b[i] == diff) { ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Increase the number of ` ` ` `// operations by 1 ` ` ` `operations++; ` ` ` `} ` ` ` ` ` `// Print the number of ` ` ` `// operations required ` ` ` `cout << operations << ` `"\n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 3, 7, 1, 4, 1, 2 }; ` ` ` `int` `b[] = { 3, 7, 3, 6, 3, 2 }; ` ` ` `int` `size = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` ` ` `checkArray(a, b, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// minimum number of operations in ` `// which the array A can be converted ` `// to another array B ` `class` `GFG { ` ` ` ` ` `// Function to find the minimum ` ` ` `// number of operations in which ` ` ` `// array A can be converted to array B ` ` ` `static` `void` `checkArray(` `int` `a[], ` `int` `b[], ` `int` `n) ` ` ` `{ ` ` ` `int` `operations = ` `0` `; ` ` ` `int` `i = ` `0` `; ` ` ` ` ` `// Loop to iterate over the array ` ` ` `while` `(i < n) { ` ` ` ` ` `// if both elements are equal ` ` ` `// then move to next element ` ` ` `if` `(a[i] - b[i] == ` `0` `) { ` ` ` `i++; ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Calculate the difference ` ` ` `// between two elements ` ` ` `int` `diff = a[i] - b[i]; ` ` ` `i++; ` ` ` ` ` `// loop while the next pair of ` ` ` `// elements have same difference ` ` ` `while` `(i < n && ` ` ` `a[i] - b[i] == diff) { ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Increase the number of ` ` ` `// operations by 1 ` ` ` `operations++; ` ` ` `} ` ` ` ` ` `// Print the number of ` ` ` `// operations required ` ` ` `System.out.println(operations); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `a[] = { ` `3` `, ` `7` `, ` `1` `, ` `4` `, ` `1` `, ` `2` `}; ` ` ` `int` `b[] = { ` `3` `, ` `7` `, ` `3` `, ` `6` `, ` `3` `, ` `2` `}; ` ` ` `int` `size = a.length; ` ` ` ` ` `checkArray(a, b, size); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// minimum number of operations in ` `// which the array A can be converted ` `// to another array B ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find the minimum ` ` ` `// number of operations in which ` ` ` `// array A can be converted to array B ` ` ` `static` `void` `checkArray(` `int` `[]a, ` `int` `[]b, ` `int` `n) ` ` ` `{ ` ` ` `int` `operations = 0; ` ` ` `int` `i = 0; ` ` ` ` ` `// Loop to iterate over the array ` ` ` `while` `(i < n) { ` ` ` ` ` `// if both elements are equal ` ` ` `// then move to next element ` ` ` `if` `(a[i] - b[i] == 0) { ` ` ` `i++; ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Calculate the difference ` ` ` `// between two elements ` ` ` `int` `diff = a[i] - b[i]; ` ` ` `i++; ` ` ` ` ` `// loop while the next pair of ` ` ` `// elements have same difference ` ` ` `while` `(i < n && ` ` ` `a[i] - b[i] == diff) { ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Increase the number of ` ` ` `// operations by 1 ` ` ` `operations++; ` ` ` `} ` ` ` ` ` `// Print the number of ` ` ` `// operations required ` ` ` `Console.WriteLine(operations); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main (` `string` `[] args) ` ` ` `{ ` ` ` `int` `[]a = { 3, 7, 1, 4, 1, 2 }; ` ` ` `int` `[]b = { 3, 7, 3, 6, 3, 2 }; ` ` ` `int` `size = a.Length; ` ` ` ` ` `checkArray(a, b, size); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum number of operations in ` `# which the array A can be converted ` `# to another array B ` ` ` `# Function to find the minimum ` `# number of operations in which ` `# array A can be converted to array B ` `def` `checkArray(a, b, n) : ` ` ` ` ` `operations ` `=` `0` `; ` ` ` `i ` `=` `0` `; ` ` ` ` ` `# Loop to iterate over the array ` ` ` `while` `(i < n) : ` ` ` ` ` `# if both elements are equal ` ` ` `# then move to next element ` ` ` `if` `(a[i] ` `-` `b[i] ` `=` `=` `0` `) : ` ` ` `i ` `+` `=` `1` `; ` ` ` `continue` `; ` ` ` ` ` `# Calculate the difference ` ` ` `# between two elements ` ` ` `diff ` `=` `a[i] ` `-` `b[i]; ` ` ` `i ` `+` `=` `1` `; ` ` ` ` ` `# loop while the next pair of ` ` ` `# elements have same difference ` ` ` `while` `(i < n ` `and` `a[i] ` `-` `b[i] ` `=` `=` `diff) : ` ` ` `i ` `+` `=` `1` `; ` ` ` ` ` `# Increase the number of ` ` ` `# operations by 1 ` ` ` `operations ` `+` `=` `1` `; ` ` ` ` ` `# Print the number of ` ` ` `# operations required ` ` ` `print` `(operations); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `3` `, ` `7` `, ` `1` `, ` `4` `, ` `1` `, ` `2` `]; ` ` ` `b ` `=` `[ ` `3` `, ` `7` `, ` `3` `, ` `6` `, ` `3` `, ` `2` `]; ` ` ` `size ` `=` `len` `(a); ` ` ` ` ` `checkArray(a, b, size); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Performance Analysis:**

**Time Complexity:**As in the above approach, There is only one loop which takes O(N) time in worst case. Hence the Time Complexity will be**O(N)**.**Auxiliary Space Complexity:**As in the above approach, There is no extra space used. Hence the auxiliary space complexity will be**O(1)**.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Convert to Strictly increasing integer array with minimum changes
- Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M
- Minimum number of operations on an array to make all elements 0
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N
- Find minimum number of merge operations to make an array palindrome
- Minimum number of increment-other operations to make all array elements equal.
- Find the minimum number of operations required to make all array elements equal
- Minimum number operations required to convert n to m | Set-2
- Minimum prime number operations to convert A to B
- Convert given array to Arithmetic Progression by adding an element
- Convert a String to Integer Array in C/C++
- Convert a number m to n using minimum number of given operations
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Minimum operations to make XOR of array zero

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.