Given two arrays a[] and b[] of length N consisting of unique elements, the task is to find a number K (K > 0) such that the first array can be converted into second array by adding K to a selected range [L, R] in the array. If no such number K exists, print NA
Examples:
Input: a[] = {3, 7, 1, 4, 0, 2, 2}, b[] = {3, 7, 3, 6, 2, 2, 2}
Output: 2
Explanation:
Array a[] can be converted into Array b[] by adding K = 2 to range [2, 4]Input: a[] = {3, 7, 1, 4, 0, 1, 2}, b[] = {3, 7, 3, 6, 2, 2}
Output: NA
Approach:
- Create a temporary array c[] which contains the difference of the array elements, i.e.,
ci = bi - ai
- Then create a vector pair for all non zero elements of array c[n] with there index. Therefore the vector pair will be as:
vector<c[i], o> where c[i] is a non zero value in c[] and i is the index of c[i]
- If the index value differs by 1 and the difference value is same, then K = difference value and [L, R] = the index range.
- Hence the array a[n] can be converted into b[n] by adding K to [a[L], a[R]].
For Example:
- Given
a[n] = [3, 7, 1, 4, 0, 2, 2] b[n] = [3, 7, 3, 6, 2, 2, 2]
- So, upon creating temporary array c[] and vector pair:
c[n] = [0, 0, 2, 2, 2, 0, 0] vector pair = {{2, 2}, {2, 3}, {2, 4}} - As all the index values (2, 3, 4) in the vector pair differ by 1, so they are consecutive.
- And the value of the difference is the same (2).
- Hence, we can simply add that difference value in the first array a[n] in the given indexes [2, 4] to convert it into second array b[n].
- Hence the required K value will be 2
Below is the implementation of the above approach:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to Check if it is possible to // convert a given array to another array // by adding elements to first array bool checkconv(int a[], int b[], int n) { int c[n], flag = 0; // Create a temporary array c[] // which contains the difference // of the array elements for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } // Create a vector pair for all non zero // elements of array c[n] with there index vector<pair<int, int> > idxs; for (int i = 0; i < n; i++) { if (c[i] != 0) idxs.push_back(make_pair(i, c[i])); } // Check If the index value differs by 1 // and the difference value is same for (int i = 0; i < idxs.size() - 1; i++) { if (idxs[i + 1].first - idxs[i].first != 1 || idxs[i + 1].second != idxs[i].second) { flag = 1; break; } } return !flag; } // Function to calculate the value of K int diffofarrays(int a[], int b[], int n) { int c[n], ans = 0; for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } for (int i = 0; i < n; i++) { if (c[i] != 0) { ans = c[i]; break; } } return ans; } // Driver code int main() { int A[] = { 3, 7, 1, 4, 0, 2, 2 }; int B[] = { 3, 7, 3, 6, 2, 2, 2 }; int arr_size = sizeof(A) / sizeof(A[0]); if (checkconv(A, B, arr_size)) { cout << diffofarrays(A, B, arr_size) << endl; } else cout << "NA" << endl; return 0; } |
Java
// Java implementation of above approach import java.util.*; class GFG { static class pair { int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to Check if it is possible to // convert a given array to another array // by adding elements to first array static boolean checkconv(int a[], int b[], int n) { int []c = new int[n]; int flag = 0; // Create a temporary array c[] // which contains the difference // of the array elements for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } // Create a vector pair for all non zero // elements of array c[n] with there index Vector<pair > idxs = new Vector<pair>(); for (int i = 0; i < n; i++) { if (c[i] != 0) idxs.add(new pair(i, c[i])); } // Check If the index value differs by 1 // and the difference value is same for (int i = 0; i < idxs.size() - 1; i++) { if (idxs.get(i + 1).first - idxs.get(i).first != 1 || idxs.get(i + 1).second != idxs.get(i).second) { flag = 1; break; } } return flag == 1 ? false:true; } // Function to calculate the value of K static int diffofarrays(int a[], int b[], int n) { int []c = new int[n]; int ans = 0; for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } for (int i = 0; i < n; i++) { if (c[i] != 0) { ans = c[i]; break; } } return ans; } // Driver code public static void main(String[] args) { int A[] = { 3, 7, 1, 4, 0, 2, 2 }; int B[] = { 3, 7, 3, 6, 2, 2, 2 }; int arr_size = A.length; if (checkconv(A, B, arr_size)) { System.out.print(diffofarrays(A, B, arr_size) +"\n"); } else System.out.print("NA" +"\n"); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of above approach # Function to Check if it is possible to # convert a given array to another array # by adding elements to first array def checkconv(a, b, n) : c = [0]*n; flag = 0; # Create a temporary array c[] # which contains the difference # of the array elements for i in range(n) : c[i] = b[i] - a[i]; # Create a vector pair for all non zero # elements of array c[n] with there index idxs = []; for i in range(n) : if (c[i] != 0) : idxs.append((i, c[i])); # Check If the index value differs by 1 # and the difference value is same for i in range(len(idxs) - 1) : if (idxs[i + 1][0] - idxs[i][0] != 1 or idxs[i + 1][1] != idxs[i][1]) : flag = 1; break; return not flag; # Function to calculate the value of K def diffofarrays(a, b, n) : c = [0] * n; ans = 0; for i in range(n) : c[i] = b[i] - a[i]; for i in range(n) : if (c[i] != 0) : ans = c[i]; break; return ans; # Driver code if __name__ == "__main__" : A = [ 3, 7, 1, 4, 0, 2, 2 ]; B = [ 3, 7, 3, 6, 2, 2, 2 ]; arr_size = len(A); if (checkconv(A, B, arr_size)) : print(diffofarrays(A, B, arr_size)); else : print("NA"); # This code is contributed by AnkitRai01 |
C#
// C# implementation of above approach using System; using System.Collections.Generic; class GFG { class pair { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function to Check if it is possible to // convert a given array to another array // by adding elements to first array static bool checkconv(int []a, int []b, int n) { int []c = new int[n]; int flag = 0; // Create a temporary array c[] // which contains the difference // of the array elements for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } // Create a vector pair for all non zero // elements of array c[n] with there index List<pair > idxs = new List<pair>(); for (int i = 0; i < n; i++) { if (c[i] != 0) idxs.Add(new pair(i, c[i])); } // Check If the index value differs by 1 // and the difference value is same for (int i = 0; i < idxs.Count - 1; i++) { if (idxs[i + 1].first - idxs[i].first != 1 || idxs[i + 1].second != idxs[i].second) { flag = 1; break; } } return flag == 1 ? false:true; } // Function to calculate the value of K static int diffofarrays(int []a, int []b, int n) { int []c = new int[n]; int ans = 0; for (int i = 0; i < n; i++) { c[i] = b[i] - a[i]; } for (int i = 0; i < n; i++) { if (c[i] != 0) { ans = c[i]; break; } } return ans; } // Driver code public static void Main(String[] args) { int []A = { 3, 7, 1, 4, 0, 2, 2 }; int []B = { 3, 7, 3, 6, 2, 2, 2 }; int arr_size = A.Length; if (checkconv(A, B, arr_size)) { Console.Write(diffofarrays(A, B, arr_size) +"\n"); } else Console.Write("NA" +"\n"); } } // This code is contributed by 29AjayKumar |
2
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.
Recommended Posts:
- Find Array formed by adding each element of given array with largest element in new array to its left
- Find the Initial Array from given array after range sum queries
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Count distinct elements after adding each element of First Array with Second Array
- Maximize the median of the given array after adding K elements to the same array
- Check if all array elements can be converted to K using given operations
- Sort an Array alphabetically when each number is converted into words
- Find element that maximizes LCM of an Array in the range 1 to M
- Find array elements with frequencies in range [l , r]
- Find if a crest is present in the index range [L, R] of the given array
- Find even occurring elements in an array of limited range
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Find duplicates in a given array when elements are not limited to a range
- Count the maximum number of elements that can be selected from the array
- Find the missing number in a sorted array of limited range
- Find frequency of each element in a limited range array in less than O(n) time
- Array range queries to find the maximum Fibonacci number with updates
- Array Range Queries to find the Maximum Armstrong number with updates
- Binary Array Range Queries to find the minimum distance between two Zeros
- Check if Array elements can be maximized upto M by adding all elements from another array
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.

