Maximize product of two closest numbers of other array for every element in given array
Last Updated :
23 Jul, 2025
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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem