Smallest possible integer to be added to N-1 elements in array A such that elements of array B are present in A
Last Updated :
15 Nov, 2021
Given two arrays A[] of length N and B[] of length N-1, the task is to find the smallest positive integer X that is added to every element of A[] except one element, so that all the elements of array B[] are present in array A[]. Assume that finding X is always possible.
Examples:
Input: A[] = {1, 4, 3, 8}, B[] = {15, 8, 11}
Output: 7
Explanation: Adding 7 to every elements of the array A except 3 will give all the elements of array B
Input: A[] = {4, 8}, B[] = {10}
Output: 2
Explanation: Adding 2 to 8 gives 10.
Approach: The idea is to use the greedy approach.
- Sort the arrays A[] and B[]
- On observation, it can be seen that, the value of the X can be either B[0] – A[0] or B[0] – A[1].
- So either A[0] or A[1] is not taken into account and X is added to the rest of the elements.
- Check if both values are valid or not by traversing the array and if both X values are found to be valid then choose the minimum one and print it.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findVal( int A[], int B[], int N)
{
unordered_set< int > s;
for ( int i = 0; i < N; i++) {
s.insert(A[i]);
}
sort(A, A + N);
sort(B, B + N - 1);
int X = B[0] - A[1];
if (X <= 0)
X = B[0] - A[0];
else {
for ( int i = 0; i < N - 1; i++) {
if (s.count(B[i] - X) == 0) {
X = B[0] - A[0];
break ;
}
}
}
cout << X << endl;
}
int main()
{
int A[] = { 1, 4, 3, 8 };
int B[] = { 15, 8, 11 };
int N = sizeof (A) / sizeof (A[0]);
findVal(A, B, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void findVal( int []A, int []B, int N)
{
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++) {
s.add(A[i]);
}
Arrays.sort(A);
Arrays.sort(B);
int X = B[ 0 ] - A[ 1 ];
if (X <= 0 )
X = B[ 0 ] - A[ 0 ];
else {
for ( int i = 0 ; i < N - 1 ; i++) {
if (s.contains(B[i] - X) == false ) {
X = B[ 0 ] - A[ 0 ];
break ;
}
}
}
System.out.println(X);
}
public static void main(String args[])
{
int []A = { 1 , 4 , 3 , 8 };
int []B = { 15 , 8 , 11 };
int N = A.length;
findVal(A, B, N);
}
}
|
Python3
def findVal(A, B, N):
s = set ()
for i in range (N):
s.add(A[i])
A.sort()
B.sort()
X = B[ 0 ] - A[ 1 ]
if (X < = 0 ):
X = B[ 0 ] - A[ 0 ]
else :
for i in range (N - 1 ):
if (B[i] - X not in s):
X = B[ 0 ] - A[ 0 ]
break
print (X)
if __name__ = = '__main__' :
A = [ 1 , 4 , 3 , 8 ]
B = [ 15 , 8 , 11 ]
N = len (A)
findVal(A, B, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findVal( int []A, int []B, int N)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < N; i++) {
s.Add(A[i]);
}
Array.Sort(A);
Array.Sort(B);
int X = B[0] - A[1];
if (X <= 0)
X = B[0] - A[0];
else {
for ( int i = 0; i < N - 1; i++) {
if (s.Contains(B[i] - X) == false ) {
X = B[0] - A[0];
break ;
}
}
}
Console.Write(X);
}
public static void Main()
{
int []A = { 1, 4, 3, 8 };
int []B = { 15, 8, 11 };
int N = A.Length;
findVal(A, B, N);
}
}
|
Javascript
<script>
function findVal(A, B, N)
{
let s = new Set();
for (let i = 0; i < N; i++)
{
s.add(A[i]);
}
A.sort( function (a, b) { return a - b });
B.sort( function (a, b) { return a - b })
let X = B[0] - A[1];
if (X <= 0)
X = B[0] - A[0];
else {
for (let i = 0; i < N - 1; i++) {
if (s.has(B[i] - X) == false ) {
X = B[0] - A[0];
break ;
}
}
}
document.write(X);
}
let A = [1, 4, 3, 8];
let B = [15, 8, 11];
let N = A.length;
findVal(A, B, N);
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...