Prefix Suffix Operations

 You are given two arrays 

A and B, each of size N. You can perform the following types of operations on array A.

  • Type 1: Select any prefix of A and increment all its elements by 1.
  • Type 2: Select any suffix of A and increment all its elements by 1.

Your task is to transform the array A into array B using the minimum number of operations. If it is impossible to do so, output 1.

For an array A having N elements:

  • prefix of the array A is a subarray starting at index 1.
  • suffix of the array A is a subarray ending at index N.

Input Format

  • First line will contain T, number of test cases. Then the test cases follow.
  • The first line of each test case contains a single integer N, the size of arrays A and B.
  • The next line contains N space-separated integers, where the ith integer denotes Ai.
  • The next line contains N space-separated integers, where the ith integer denotes Bi.

Output Format

For each test case, print a single line containing one integer ― minimum operations required to convert A into B. Print 1 if it is impossible to convert A into B.

Constraints

  • 1T104
  • 1N105
  • 1014Ai,Bi1014
  • Sum of N over all test cases does not exceed 2105.

Sample Input 1 

3
5
2 3 5 1 2
4 3 6 2 3
4
0 0 0 0
1 2 2 1
3
1 2 3
1 2 2

Sample Output 1 

3
2
-1

Explanation

Test Case 1: Given A=[2,3,5,1,2]. It can be converted to array B=[4,3,6,2,3] in 3 operations:

  • Operation 1: Perform a type 1 operation. Choose the prefix of length 1 and increment the elements by 1. Thus, the updated array is A=[3,3,5,1,2].
  • Operation 2: Perform a type 1 operation. Choose the prefix of length 1 and increment the elements by 1. Thus, the updated array is A=[4,3,5,1,2].
  • Operation 3: Perform a type 2 operation. Choose the suffix of length 3 and increment the elements by 1. Thus, the updated array is A=[4,3,6,2,3].

It can be proven that we cannot convert the array A into array B in less than 3 operations.

Test Case 2: Given A=[0,0,0,0]. It can be converted to array B=[1,2,2,1] in 2 operations:

  • Operation 1: Perform a type 1 operation. Choose the prefix of length 3 and increment the elements by 1. Thus, the updated array is A=[1,1,1,0].
  • Operation 2: Perform a type 2 operation. Choose the suffix of length 3 and increment the elements by 1. Thus, the updated array is A=[1,2,2,1].

It can be proven that we cannot convert the array A into array B in less than 2 operations.

Test Case 3: Given A=[1,2,3]. The value of A[3] will always be greater than B[3]. Hence, it is impossible to convert A into B using any number of operations

Comments

Popular posts from this blog

Flipkart Runway: Season 3 | 2025 | Internship

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Top-order failure leaves India in the doldrums

Software Testing Week 5 : Assignment 5 | 2022