Open In App

Find Maximized difference between two elements for every index of given Arrays

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:

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:

Below is the implementation of the above approach.  




#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximized value
// of a possible pair
void maxValueGoodPair(int A[],int B[],int N)
{
  long X = 0, Y = 0, max = INT_MIN;
 
  // Traversing over array elements
  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;
  }
}
 
// Driver code
int main() {
  int A[] = { 5, 2, 10 };
  int B[] = { 6, 8, 89 };
  int N =sizeof(A) / sizeof(A[0]);
 
  // Function call
  maxValueGoodPair(A, B, N);
 
  return 0;
}
 
// This code is contributed by satwik4409.




// Java code to implement the above approach
 
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to find the maximized value
    // of a possible pair
    public static void maxValueGoodPair(int A[],
                                        int B[],
                                        int N)
    {
        long X = 0, Y = 0, max = Long.MIN_VALUE;
 
        // Traversing over array elements
        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);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A[] = { 5, 2, 10 };
        int B[] = { 6, 8, 89 };
        int N = A.length;
 
        // Function call
        maxValueGoodPair(A, B, N);
    }
}




# Python code to implement the above approach
import sys
 
# function to find the maximized value of a possible pair
def maxValueGoodPair(A, B, N):
    X = 0
    Y = 0
    maxx = -sys.maxsize - 1
 
    # Traversing over array elements
    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)
# Function call
maxValueGoodPair(A, B, N)
 
# This code is contributed by lokeshmvs21.




// C# implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find the maximized value
    // of a possible pair
    public static void maxValueGoodPair(int[] A,
                                        int[] B,
                                        int N)
    {
        int X = 0, Y = 0, max = Int32.MinValue;
  
        // Traversing over array elements
        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);
        }
    }
  
  // Driver Code
  public static void Main()
  {
        int[] A = { 5, 2, 10 };
        int[] B = { 6, 8, 89 };
        int N = A.Length;
  
        // Function call
        maxValueGoodPair(A, B, N);
  }
}




<script>
    // JavaScript code to implement the approach
  
    // Function to find the maximized value
    // of a possible pair
    const maxValueGoodPair = (A,B,N) => {
      let X = 0, Y = 0, max =  Number. MIN_SAFE_INTEGER;
 
        // Traversing over array elements
        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);
        }
    }
  
    // Driver Code
    let A1 = [ 5, 2, 10 ];
    let B1 = [ 6, 8, 89 ];
    let N1 =A1.length;
 
    // Function call
    maxValueGoodPair(A1, B1, N1);
  
// This code is contributed by Rohit Pradhan
  
</script>

Output
5 5
2 8
11 88

Time Complexity: O(N * M) where M is the maximum value in the array B
Auxiliary Space: O(1)


Article Tags :