Find K such that array A can be converted into array B by adding K to a selected range [L, R]
Last Updated :
14 Mar, 2023
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 the second array by adding K to a selected range [L, R] in the array. If no such number K exists, print N
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 between the array elements, i.e.,
ci = bi - ai
- Then create a vector pair for all non-zero elements of the array c[n] with their 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 the 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:
a[n] = [3, 7, 1, 4, 0, 2, 2]
b[n] = [3, 7, 3, 6, 2, 2, 2]
- So, upon creating the temporary array c[] and a 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 different value in the first array a[n] in the given indexes [2, 4] to convert it into a 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 their 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.*;
public 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 their 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 their 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 their 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
JavaScript
<script>
// Javascript 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
function checkconv(a, b, n) {
let c = new Array(n);
let flag = 0;
// Create a temporary array c[]
// which contains the difference
// of the array elements
for (let 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 their index
let idxs = new Array();
for (let i = 0; i < n; i++) {
if (c[i] != 0)
idxs.push([i, c[i]]);
}
// Check If the index value differs by 1
// and the difference value is same
for (let i = 0; i < idxs.length - 1; i++) {
if (idxs[i + 1][0] - idxs[i][0] != 1
|| idxs[i + 1][1] != idxs[i][1]) {
flag = 1;
break;
}
}
return !flag;
}
// Function to calculate the value of K
function diffofarrays(a, b, n) {
let c = new Array(n);
let ans = 0;
for (let i = 0; i < n; i++) {
c[i] = b[i] - a[i];
}
for (let i = 0; i < n; i++) {
if (c[i] != 0) {
ans = c[i];
break;
}
}
return ans;
}
// Driver code
let A = [3, 7, 1, 4, 0, 2, 2];
let B = [3, 7, 3, 6, 2, 2, 2];
let arr_size = A.length;
if (checkconv(A, B, arr_size)) {
document.write(diffofarrays(A, B, arr_size) + "<br>");
}
else
document.write("NA" + "<br>");
</script>
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the size of the given array.
Similar Reads
Minimize elements to be added to a given array such that it contains another given array as its subsequence
Given an array A[] consisting of N distinct integers and another array B[] consisting of M integers, the task is to find the minimum number of elements to be added to the array B[] such that the array A[] becomes the subsequence of the array B[]. Examples: Input: N = 5, M = 6, A[] = {1, 2, 3, 4, 5},
15+ min read
Count of index range [L, R] in Array such that removing all its instances sorts the Array
Given an array arr[] of length N, the task is to find the number of Good Ranges in the array arr[]. A Good Range is defined as the range of left and right indices, i.e, [L. R] in the array arr[] such that by removing all the numbers in the range [L, R] of the array arr[] and the appearances of those
8 min read
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
Given an array A[] consisting of N distinct integers and another array B[] consisting of M integers, the task is to find the minimum number of elements to be added to the array B[] such that the array A[] becomes the subsequence of the array B[]. Examples: Input: N = 5, M = 6, A[] = {1, 2, 3, 4, 5},
9 min read
Count pairs (i, j) from arrays arr[] & brr[] such that arr[i] - brr[j] = arr[j] - brr[i]
Given two arrays arr[] and brr[] consisting of N integers, the task is to count the number of pairs (i, j) from both the array such that (arr[i] â brr[j]) and (arr[j] â brr[i]) are equal. Examples: Input: A[] = {1, 2, 3, 2, 1}, B[] = {1, 2, 3, 2, 1} Output: 2 Explanation: The pairs satisfying the co
7 min read
Find all pairs (a, b) in an array such that a % b = k
Given an array with distinct elements, the task is to find the pairs in the array such that a % b = k, where k is a given integer. You may assume that a and b are in small range Examples : Input : arr[] = {2, 3, 5, 4, 7} k = 3Output : (7, 4), (3, 4), (3, 5), (3, 7)7 % 4 = 33 % 4 = 33 % 5 = 33 % 7 =
15 min read
Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index
Given two arrays A[] and B[], both consisting of a permutation of first N natural numbers, the task is to count the minimum number of times the last array element is required to be shifted to any arbitrary position in the array A[] to make both the arrays A[] and B[] equal. Examples: Input: A[] = {1
6 min read
Maximize sum by choosing a subsegment from array [l, r] and convert arr[i] to (Mâarr[i]) at most once
Given an array, arr[] consisting of N positive integers and a positive integer M, the task is to maximize the sum of the array after performing at most one operation. In one operation, choose a subsegment from the array [l, r] and convert arr[i] to M - arr[i] where l?i?r. Examples: Input: arr[] = {2
6 min read
Check if elements of given array can be rearranged such that (arr[i] + i*K) % N = i for all values of i in range [0, N-1]
Given an array arr[] consisting of N positive integers and an integer K, the task is to check if the array elements can be rearranged such that (arr[i] + i*K) % N = i for all values of i in the range [0, N-1]. Examples: Input: arr[] = {2, 1, 0}, K = 5Output: YesExplanation: The given array can be re
11 min read
Maximum range subarray for each index in Array such that A[i] = min(A[L], A[L+1], ⦠A[R])
Given an array arr[] of N distinct integers, the task is to calculate for each index i (1â¤iâ¤N) a range [L, R] such that arr[i] = min(arr[L], arr[L+1], ⦠arr[R]), where Lâ¤iâ¤R and R-L is maximized. Examples: Input: N = 3, arr[] = {1, 3, 2}Output: 1 32 22 3Explanation: 1 is minimum in the range [1, 3]3
11 min read
Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space
Given an array arr[] of size n where every element is in the range from 0 to n-1. Rearrange the given array so that arr[i] becomes arr[arr[i]]. This should be done with O(1) extra spaceExamples: Input: arr[] = [3, 2, 0, 1]Output: arr[] = [1, 0, 3, 2]Explanation: arr[arr[0]] is 1 so arr[0] in output
5 min read