Maximize product of two closest numbers of other array for every element in given array
Last Updated :
27 Jan, 2022
Given arrays arr1[] of size M and arr2[] of size N having length at least 2, the task is for every element in arr1[], maximize the product of two elements in arr2[] which are closest to the element in arr1[]. The closest elements must be present on distinct indices.
Example:
Input: arr1 = [5, 10, 17, 22, -1], arr2 = [-1, 26, 5, 20, 14, 17, -7]
Output: -5 70 340 520 7
Explanation:
The closest elements to 5 are 5 and -1, therefore the maximum product is -5
The closest elements to 10 are 5 and 14, therefore the maximum product is 70
The closest elements to 17 are 20, 17, and 14, therefore the maximum product of 20 and 17 is 340
The closest elements to 22 are 20 and 26, therefore the maximum product is 520
The closest elements to -1 are -1, 5, and -7, therefore the maximum product of -1 and -7 is 7
Input: arr1 = [3, 9, 4, -1, 22], arr2 = [-1, 1, 21, 8, -3, 20, 25]
Output: -1 8 8 3 420
Approach: The given problem can be solved using a greedy approach. The idea is to sort the array arr2 in ascending order, then for every element in arr1, find the closest element to it in arr2, using binary search. Below steps can be followed to solve the problem:
- Sort the array arr2 in ascending order
- Iterate the array arr1 and at every iteration, apply binary search on arr2 to find an index of element closest to arr1[i], say x:
- If there does not exists a previous index x-1, then return arr2[x] * arr2[x+1]
- Else If there does not exists a next index x+1, then return arr2[x] * arr2[x-1]
- Else, check which element between arr2[x-1] and arr2[x+1] is closer to arr1[i]:
- If arr2[x-1] is closer return arr2[x] * arr2[x-1]
- Else if arr2[x+1] is closer return arr2[x] * arr2[x+1]
- Else if both arr2[x-1] and arr2[x+1] are equidistant from arr1[i] then return the maximum product between arr2[x] * arr2[x+1] and arr2[x] * arr2[x+1]
Below is the implementation of the above approach.
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Binary search function to
// find element closest to arr1[i]
int binarySearch(int num,
vector<int> arr)
{
// Initialize left right and mid
int mid = -1, left = 0,
right = arr.size() - 1;
// Initialize closest index and
// smallest difference
int closestInd = -1;
int smallestDiff = INT_MAX;
while (left <= right)
{
mid = (left + right) >> 1;
if (abs(arr[mid] - num) < smallestDiff)
{
// Update smallest difference
smallestDiff = abs(arr[mid] - num);
// Update closest index
closestInd = mid;
}
else if (abs(arr[mid] - num) == smallestDiff)
{
if (arr[mid] > arr[closestInd])
closestInd = mid;
}
if (arr[mid] == num)
{
// This is the closest
// element index
return mid;
}
else if (arr[mid] < num)
{
// Closer element lies
// to the right
left = mid + 1;
}
else
{
// Closer element lies
// to the left
right = mid - 1;
}
}
return closestInd;
}
// Function to find the maximum product of
// Closest two elements in second array
// for every element in the first array
vector<int> maxProdClosest(vector<int> arr1,
vector<int> arr2)
{
// Find the length of both arrays
int M = arr1.size(), N = arr2.size();
// Initialize an array to store
// the result for every element
vector<int> ans(M);
// Sort the second array arr2
sort(arr2.begin(), arr2.end());
// Iterate the array arr1
for (int i = 0; i < M; i++)
{
// Apply binary search and
// find the index of closest
// element to arr1[i] in arr2
int ind = binarySearch(arr1[i],
arr2);
// No element at previous index
if (ind == 0)
{
ans[i] = arr2[ind] * arr2[ind + 1];
}
// No element at the next index
else if (ind == N - 1)
{
ans[i] = arr2[ind] * arr2[ind - 1];
}
// Elements at the next and
// previous indices are present
else
{
// arr2[ind - 1] is closer
// to arr1[i]
if (abs(arr2[ind - 1] - arr1[i]) < abs(arr2[ind + 1] - arr1[i]))
{
ans[i] = arr2[ind] * arr2[ind - 1];
}
else if (
// arr2[ind + 1] is
// closer to arr1[i]
abs(arr2[ind - 1] - arr1[i]) > abs(arr2[ind + 1] - arr1[i]))
{
ans[i] = arr2[ind] * arr2[ind + 1];
}
// If both arr2[ind - 1] and
// arr2[ind + 1] are
// equidistant from arr1[i]
else
{
ans[i] = max(
arr2[ind] * arr2[ind - 1],
arr2[ind] * arr2[ind + 1]);
}
}
}
// Return the resulting array
return ans;
}
// Driver function
int main()
{
// Initialize the arrays
vector<int> arr1 = {5, 10, 17, 22, -1};
vector<int> arr2 = {-1, 26, 5, 20,
14, 17, -7};
// Call the function
vector<int> res = maxProdClosest(arr1,
arr2);
// Iterate the array and
// print the result
for (int i = 0; i < res.size(); i++)
{
cout << res[i] << " ";
}
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the maximum product of
// Closest two elements in second array
// for every element in the first array
public static int[] maxProdClosest(int[] arr1,
int[] arr2)
{
// Find the length of both arrays
int M = arr1.length, N = arr2.length;
// Initialize an array to store
// the result for every element
int[] ans = new int[M];
// Sort the second array arr2
Arrays.sort(arr2);
// Iterate the array arr1
for (int i = 0; i < M; i++) {
// Apply binary search and
// find the index of closest
// element to arr1[i] in arr2
int ind = binarySearch(arr1[i],
arr2);
// No element at previous index
if (ind == 0) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// No element at the next index
else if (ind == N - 1) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
// Elements at the next and
// previous indices are present
else {
// arr2[ind - 1] is closer
// to arr1[i]
if (Math.abs(arr2[ind - 1]
- arr1[i])
< Math.abs(arr2[ind + 1]
- arr1[i])) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
else if (
// arr2[ind + 1] is
// closer to arr1[i]
Math.abs(arr2[ind - 1]
- arr1[i])
> Math.abs(arr2[ind + 1]
- arr1[i])) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// If both arr2[ind - 1] and
// arr2[ind + 1] are
// equidistant from arr1[i]
else {
ans[i] = Math.max(
arr2[ind] * arr2[ind - 1],
arr2[ind] * arr2[ind + 1]);
}
}
}
// Return the resulting array
return ans;
}
// Binary search function to
// find element closest to arr1[i]
public static int binarySearch(int num,
int[] arr)
{
// Initialize left right and mid
int mid = -1, left = 0,
right = arr.length - 1;
// Initialize closest index and
// smallest difference
int closestInd = -1;
int smallestDiff = Integer.MAX_VALUE;
while (left <= right) {
mid = (left + right) >> 1;
if (Math.abs(arr[mid] - num)
< smallestDiff) {
// Update smallest difference
smallestDiff = Math.abs(arr[mid] - num);
// Update closest index
closestInd = mid;
}
else if (Math.abs(arr[mid] - num)
== smallestDiff) {
if (arr[mid] > arr[closestInd])
closestInd = mid;
}
if (arr[mid] == num) {
// This is the closest
// element index
return mid;
}
else if (arr[mid] < num) {
// Closer element lies
// to the right
left = mid + 1;
}
else {
// Closer element lies
// to the left
right = mid - 1;
}
}
return closestInd;
}
// Driver function
public static void main(String[] args)
{
// Initialize the arrays
int[] arr1 = { 5, 10, 17, 22, -1 };
int[] arr2 = { -1, 26, 5, 20,
14, 17, -7 };
// Call the function
int[] res = maxProdClosest(arr1,
arr2);
// Iterate the array and
// print the result
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}
Python3
# python3 code for the above approach
INT_MAX = 2147483647
# Binary search function to
# find element closest to arr1[i]
def binarySearch(num, arr):
# Initialize left right and mid
mid, left, right = -1, 0, len(arr) - 1
# Initialize closest index and
# smallest difference
closestInd = -1
smallestDiff = INT_MAX
while (left <= right):
mid = (left + right) >> 1
if (abs(arr[mid] - num) < smallestDiff):
# Update smallest difference
smallestDiff = abs(arr[mid] - num)
# Update closest index
closestInd = mid
elif (abs(arr[mid] - num) == smallestDiff):
if (arr[mid] > arr[closestInd]):
closestInd = mid
if (arr[mid] == num):
# This is the closest
# element index
return mid
elif (arr[mid] < num):
# Closer element lies
# to the right
left = mid + 1
else:
# Closer element lies
# to the left
right = mid - 1
return closestInd
# Function to find the maximum product of
# Closest two elements in second array
# for every element in the first array
def maxProdClosest(arr1, arr2):
# Find the length of both arrays
M, N = len(arr1), len(arr2)
# Initialize an array to store
# the result for every element
ans = [0 for _ in range(M)]
# Sort the second array arr2
arr2.sort()
# Iterate the array arr1
for i in range(0, M):
# Apply binary search and
# find the index of closest
# element to arr1[i] in arr2
ind = binarySearch(arr1[i], arr2)
# No element at previous index
if (ind == 0):
ans[i] = arr2[ind] * arr2[ind + 1]
# No element at the next index
elif (ind == N - 1):
ans[i] = arr2[ind] * arr2[ind - 1]
# Elements at the next and
# previous indices are present
else:
# arr2[ind - 1] is closer
# to arr1[i]
if (abs(arr2[ind - 1] - arr1[i]) < abs(arr2[ind + 1] - arr1[i])):
ans[i] = arr2[ind] * arr2[ind - 1]
elif (
# arr2[ind + 1] is
# closer to arr1[i]
abs(arr2[ind - 1] - arr1[i]) > abs(arr2[ind + 1] - arr1[i])):
ans[i] = arr2[ind] * arr2[ind + 1]
# If both arr2[ind - 1] and
# arr2[ind + 1] are
# equidistant from arr1[i]
else:
ans[i] = max(
arr2[ind] * arr2[ind - 1],
arr2[ind] * arr2[ind + 1])
# Return the resulting array
return ans
# Driver function
if __name__ == "__main__":
# Initialize the arrays
arr1 = [5, 10, 17, 22, -1]
arr2 = [-1, 26, 5, 20, 14, 17, -7]
# Call the function
res = maxProdClosest(arr1, arr2)
# Iterate the array and
# print the result
for i in range(0, len(res)):
print(res[i], end=" ")
# This code is contributed by rakeshsahni
C#
// C# implementation for the above approach
using System;
class GFG {
// Function to find the maximum product of
// Closest two elements in second array
// for every element in the first array
public static int[] maxProdClosest(int[] arr1,
int[] arr2)
{
// Find the length of both arrays
int M = arr1.Length, N = arr2.Length;
// Initialize an array to store
// the result for every element
int[] ans = new int[M];
// Sort the second array arr2
Array.Sort(arr2);
// Iterate the array arr1
for (int i = 0; i < M; i++) {
// Apply binary search and
// find the index of closest
// element to arr1[i] in arr2
int ind = binarySearch(arr1[i], arr2);
// No element at previous index
if (ind == 0) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// No element at the next index
else if (ind == N - 1) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
// Elements at the next and
// previous indices are present
else {
// arr2[ind - 1] is closer
// to arr1[i]
if (Math.Abs(arr2[ind - 1] - arr1[i])
< Math.Abs(arr2[ind + 1] - arr1[i])) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
else if (
// arr2[ind + 1] is
// closer to arr1[i]
Math.Abs(arr2[ind - 1] - arr1[i])
> Math.Abs(arr2[ind + 1] - arr1[i])) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// If both arr2[ind - 1] and
// arr2[ind + 1] are
// equidistant from arr1[i]
else {
ans[i] = Math.Max(
arr2[ind] * arr2[ind - 1],
arr2[ind] * arr2[ind + 1]);
}
}
}
// Return the resulting array
return ans;
}
// Binary search function to
// find element closest to arr1[i]
public static int binarySearch(int num, int[] arr)
{
// Initialize left right and mid
int mid = -1, left = 0, right = arr.Length - 1;
// Initialize closest index and
// smallest difference
int closestInd = -1;
int smallestDiff = Int32.MaxValue;
while (left <= right) {
mid = (left + right) >> 1;
if (Math.Abs(arr[mid] - num) < smallestDiff) {
// Update smallest difference
smallestDiff = Math.Abs(arr[mid] - num);
// Update closest index
closestInd = mid;
}
else if (Math.Abs(arr[mid] - num)
== smallestDiff) {
if (arr[mid] > arr[closestInd])
closestInd = mid;
}
if (arr[mid] == num) {
// This is the closest
// element index
return mid;
}
else if (arr[mid] < num) {
// Closer element lies
// to the right
left = mid + 1;
}
else {
// Closer element lies
// to the left
right = mid - 1;
}
}
return closestInd;
}
// Driver function
public static void Main(string[] args)
{
// Initialize the arrays
int[] arr1 = { 5, 10, 17, 22, -1 };
int[] arr2 = { -1, 26, 5, 20, 14, 17, -7 };
// Call the function
int[] res = maxProdClosest(arr1, arr2);
// Iterate the array and
// print the result
for (int i = 0; i < res.Length; i++) {
Console.Write(res[i] + " ");
}
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript code for the above approach
// Binary search function to
// find element closest to arr1[i]
function binarySearch(num, arr) {
// Initialize left right and mid
let mid = -1, left = 0,
right = arr.length - 1;
// Initialize closest index and
// smallest difference
let closestInd = -1;
let smallestDiff = Number.MAX_SAFE_INTEGER;
while (left <= right) {
mid = (left + right) >> 1;
if (Math.abs(arr[mid] - num) < smallestDiff) {
// Update smallest difference
smallestDiff = Math.abs(arr[mid] - num);
// Update closest index
closestInd = mid;
}
else if (Math.abs(arr[mid] - num) == smallestDiff) {
if (arr[mid] > arr[closestInd])
closestInd = mid;
}
if (arr[mid] == num) {
// This is the closest
// element index
return mid;
}
else if (arr[mid] < num) {
// Closer element lies
// to the right
left = mid + 1;
}
else {
// Closer element lies
// to the left
right = mid - 1;
}
}
return closestInd;
}
// Function to find the maximum product of
// Closest two elements in second array
// for every element in the first array
function maxProdClosest(arr1, arr2) {
// Find the length of both arrays
let M = arr1.length, N = arr2.length;
// Initialize an array to store
// the result for every element
let ans = new Array(M);
// Sort the second array arr2
arr2.sort((a, b) => a - b);
// Iterate the array arr1
for (let i = 0; i < M; i++) {
// Apply binary search and
// find the index of closest
// element to arr1[i] in arr2
let ind = binarySearch(arr1[i],
arr2);
// No element at previous index
if (ind == 0) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// No element at the next index
else if (ind == N - 1) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
// Elements at the next and
// previous indices are present
else {
// arr2[ind - 1] is closer
// to arr1[i]
if (Math.abs(arr2[ind - 1] - arr1[i]) < Math.abs(arr2[ind + 1] - arr1[i])) {
ans[i] = arr2[ind] * arr2[ind - 1];
}
else if (
// arr2[ind + 1] is
// closer to arr1[i]
Math.abs(arr2[ind - 1] - arr1[i]) > Math.abs(arr2[ind + 1] - arr1[i])) {
ans[i] = arr2[ind] * arr2[ind + 1];
}
// If both arr2[ind - 1] and
// arr2[ind + 1] are
// equidistant from arr1[i]
else {
ans[i] = Math.max(
arr2[ind] * arr2[ind - 1],
arr2[ind] * arr2[ind + 1]);
}
}
}
// Return the resulting array
return ans;
}
// Driver function
// Initialize the arrays
let arr1 = [5, 10, 17, 22, -1];
let arr2 = [-1, 26, 5, 20, 14, 17, -7];
// Call the function
let res = maxProdClosest(arr1, arr2);
// Iterate the array and
// print the result
for (let i = 0; i < res.length; i++) {
document.write(res[i] + " ");
}
// This code is contributed by Saurabh Jaiswal
</script>
Time Complexity: O(N * log N + M * log N)
Auxiliary Space: O(1)
Similar Reads
Closest pair in an Array such that one number is multiple of the other
Given an array arr[] of integers of size N, the task is to find the closest pair in the given array such that one element is the multiple of the other. If no such pair exists then print -1. Note: Closest pair means the difference between the index of any two elements must be minimum. Examples: Input
6 min read
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: 1 ⤠X, Y ⤠Bigcd(X, Y)
6 min read
Maximize count of distinct elements possible in an Array from the given operation
Given an array A[] of size N, the task is to maximize the count of distinct elements in the array by inserting the absolute differences of the existing array elements. Examples: Input: A[] = {1, 2, 3, 5} Output: 5 Explanation: Possible absolute differences among the array elements are: (2 - 1) = 1 (
6 min read
Print X array elements closest to the Kth smallest element in the array
Given two integers K, X, and an array arr[] consisting of N distinct elements, the task is to find X elements closest to the Kth smallest element from the given array. Examples: Input: arr[] = {1, 2, 3, 4, 10}, K = 3, X = 2Output: 2 3Explanation: Kth smallest element present in the given array is 3
15+ min read
Maximize the minimum element of Array by reducing elements one by one
Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations. Examples: Input:
6 min read
Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that the number of elements less than or equal to them in arr2[] is maximum. Examples: Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4} Output: 20 Below table shows the count of element
15 min read
Find the absolute difference between the nearest powers of two given integers for every array element
Given an array arr[] consisting of N positive integers and two positive integers A and B, the task is to replace each array element with the absolute difference of the nearest powers of A and B. If there exists two nearest powers, then choose the maximum of the two. Examples: Input: arr[] = {5, 12,
8 min read
Maximum difference of prefix sum for all indices of given two Arrays
Given 2 arrays of integers a[] and s[] both of size N. The task is to find the maximum difference of prefix sum for all indices of the given arrays. Examples: Input: N = 5, a[] = {20, 20, 35, 20, 35}, s[] = {21, 31, 34, 41, 14}Output: 32Explanation: After prefix sum the arrays are a[] = {20, 40, 75,
4 min read
Maximize sum of product of same-indexed elements of equal length subarrays obtained from two given arrays
Given two arrays arr[] and brr[] of size N and M integers respectively, the task is to maximize the sum of the product of the same-indexed elements of two subarrays of an equal length with the selected subarray from the array brr[] being reversed. Examples: Input: arr[] = {-1, 3, -2, 4, 5}, brr[] =
13 min read
Closest greater or same value on left side for every element in array
Given an array arr[] of size n. For each element in the array, find the value wise closest element to its left that is greater than or equal to the current element. If no such element exists, return -1 for that position. Examples: Input : arr[] = [10, 5, 11, 6, 20, 12]Output : [-1, 10, -1, 10, -1, 2
9 min read