Minimum number of operations to convert array A to array B by adding an integer into a subarray
Last Updated :
17 Apr, 2023
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 an equal difference with the corresponding element in 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 continue checking for 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, until all the indexes are iterated having the same difference.
- In the end, return the count as the minimum number of operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkArray( int a[], int b[], int n)
{
int operations = 0;
int i = 0;
while (i < n) {
if (a[i] - b[i] == 0) {
i++;
continue ;
}
int diff = a[i] - b[i];
i++;
while (i < n &&
a[i] - b[i] == diff) {
i++;
}
operations++;
}
cout << operations << "\n" ;
}
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;
}
|
Java
class GFG {
static void checkArray( int a[], int b[], int n)
{
int operations = 0 ;
int i = 0 ;
while (i < n) {
if (a[i] - b[i] == 0 ) {
i++;
continue ;
}
int diff = a[i] - b[i];
i++;
while (i < n &&
a[i] - b[i] == diff) {
i++;
}
operations++;
}
System.out.println(operations);
}
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);
}
}
|
C#
using System;
class GFG {
static void checkArray( int []a, int []b, int n)
{
int operations = 0;
int i = 0;
while (i < n) {
if (a[i] - b[i] == 0) {
i++;
continue ;
}
int diff = a[i] - b[i];
i++;
while (i < n &&
a[i] - b[i] == diff) {
i++;
}
operations++;
}
Console.WriteLine(operations);
}
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);
}
}
|
Python3
def checkArray(a, b, n) :
operations = 0 ;
i = 0 ;
while (i < n) :
if (a[i] - b[i] = = 0 ) :
i + = 1 ;
continue ;
diff = a[i] - b[i];
i + = 1 ;
while (i < n and a[i] - b[i] = = diff) :
i + = 1 ;
operations + = 1 ;
print (operations);
if __name__ = = "__main__" :
a = [ 3 , 7 , 1 , 4 , 1 , 2 ];
b = [ 3 , 7 , 3 , 6 , 3 , 2 ];
size = len (a);
checkArray(a, b, size);
|
Javascript
<script>
function checkArray(a , b , n) {
var operations = 0;
var i = 0;
while (i < n) {
if (a[i] - b[i] == 0) {
i++;
continue ;
}
var diff = a[i] - b[i];
i++;
while (i < n && a[i] - b[i] == diff) {
i++;
}
operations++;
}
document.write(operations);
}
var a = [ 3, 7, 1, 4, 1, 2 ];
var b = [ 3, 7, 3, 6, 3, 2 ];
var size = a.length;
checkArray(a, b, size);
</script>
|
Performance Analysis:
- Time Complexity: As in the above approach, there is only one loop that takes O(N) time in the 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).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...