Find Maximized difference between two elements for every index of given Arrays
Last Updated :
26 Aug, 2022
Given two arrays A[] and B[] of the same length N. the task is to find a pair (X, Y) for two numbers Ai (number at i index of array A) and Bi(number at i index of array B) such that the value of |X-Y| is maximized for each index and the pair satisfies the following conditions:
- 1 ≤ X, Y ≤ Bi
- gcd(X, Y) ≥ Ai
Note: If there are multiple possible pairs for which the value of is |X-Y| maximized, you can print any of them.
Examples:
Input: A[] = {5, 2, 10}, B[] = {6, 8, 89}
Output: {5, 5}, {2, 8}, {11, 88}
Explanation: For first element of both array (5, 5) and (6, 6)
are the only possible pairs and for both of them the value of |X-Y| is 0.
And for second element of both array (6, 8), (8, 6), (2, 8), (8, 2),
(4, 6), (6, 4), (2, 6), (6, 2), (2, 4), (4, 2) and (2, 2) are the pairs out of which |X-Y|
is maximum for (2, 8) and (8, 2).
Similarly, for third element both the arrays have (11, 88) as a possible pair.
Input: A[] = {1, 12}, B[] = {6, 39}
Output: {6, 1}, {13, 39}
Approach: To solve the problem follow the below idea:
Run a nested for loop, in which first one will iterate over elements in the array and the second one will calculate the valid pairs between current array elements of both the arrays. Compare these elements with the conditions provided, then print the pair with maximum difference.
Follow the given steps to solve the problem:
- First, iterate a loop from the first index of the array to the last index of the array to get the number for which the good pair needs to be found
- After that iterate second for loop from i = A[i] to B[i] such that, first element of pair i.e. X = i and second element of pair i.e.Y = B[i] – (B[i]%i) and find absolute difference of (X, Y) pair
- After that check whether the absolute difference of (X, Y) pair is greater than maximum difference found so far:
- If it is true then set the first element and the second element of the good pair as X and Y respectively
- And then assign max = absolute difference of (X, Y)
- And after the end of the second loop, print the (X, Y) pair as a possible pair such that the value of |X-Y| is maximized
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void maxValueGoodPair( int A[], int B[], int N)
{
long X = 0, Y = 0, max = INT_MIN;
for ( int j = 0; j < N; j++) {
long first = A[j], sec = A[j], diff = 0;
for ( int i = A[j]; i <= B[j]; i++) {
X = i;
Y = B[j] - (B[j] % i);
diff = abs (X - Y);
if (diff > max) {
first = X;
sec = Y;
max = abs (X - Y);
}
}
cout << first<< " " <<sec<<endl;
}
}
int main() {
int A[] = { 5, 2, 10 };
int B[] = { 6, 8, 89 };
int N = sizeof (A) / sizeof (A[0]);
maxValueGoodPair(A, B, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void maxValueGoodPair( int A[],
int B[],
int N)
{
long X = 0 , Y = 0 , max = Long.MIN_VALUE;
for ( int j = 0 ; j < N; j++) {
long first = A[j], sec = A[j], diff = 0 ;
for ( int i = A[j]; i <= B[j]; i++) {
X = i;
Y = B[j] - (B[j] % i);
diff = Math.abs(X - Y);
if (diff > max) {
first = X;
sec = Y;
max = Math.abs(X - Y);
}
}
System.out.println(first + " " + sec);
}
}
public static void main(String[] args)
{
int A[] = { 5 , 2 , 10 };
int B[] = { 6 , 8 , 89 };
int N = A.length;
maxValueGoodPair(A, B, N);
}
}
|
Python3
import sys
def maxValueGoodPair(A, B, N):
X = 0
Y = 0
maxx = - sys.maxsize - 1
for j in range (N):
first = A[j]
sec = A[j]
diff = 0
for i in range (A[j], B[j]):
X = i
Y = B[j] - (B[j] % i)
diff = abs (X - Y)
if (diff > maxx):
first = X
sec = Y
maxx = abs (X - Y)
print (first, end = " " )
print (sec)
A = [ 5 , 2 , 10 ]
B = [ 6 , 8 , 89 ]
N = len (A)
maxValueGoodPair(A, B, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void maxValueGoodPair( int [] A,
int [] B,
int N)
{
int X = 0, Y = 0, max = Int32.MinValue;
for ( int j = 0; j < N; j++) {
int first = A[j], sec = A[j], diff = 0;
for ( int i = A[j]; i <= B[j]; i++) {
X = i;
Y = B[j] - (B[j] % i);
diff = Math.Abs(X - Y);
if (diff > max) {
first = X;
sec = Y;
max = Math.Abs(X - Y);
}
}
Console.WriteLine(first + " " + sec);
}
}
public static void Main()
{
int [] A = { 5, 2, 10 };
int [] B = { 6, 8, 89 };
int N = A.Length;
maxValueGoodPair(A, B, N);
}
}
|
Javascript
<script>
const maxValueGoodPair = (A,B,N) => {
let X = 0, Y = 0, max = Number. MIN_SAFE_INTEGER;
for (let j = 0; j < N; j++) {
let first = A[j], sec = A[j], diff = 0;
for (let i = A[j]; i <= B[j]; i++) {
X = i;
Y = B[j] - (B[j] % i);
diff = Math.abs(X - Y);
if (diff > max) {
first = X;
sec = Y;
max = Math.abs(X - Y);
}
}
document.write(first+ " " +sec);
}
}
let A1 = [ 5, 2, 10 ];
let B1 = [ 6, 8, 89 ];
let N1 =A1.length;
maxValueGoodPair(A1, B1, N1);
</script>
|
Time Complexity: O(N * M) where M is the maximum value in the array B
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...