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: arr[] = {2, 3, 4, 5, 6}
Output: 2 4
Explanation:
The only possible pairs are (2, 4), (2, 6), (3, 6) out of which the pair which have minimum distance between them is (2, 4).
Input: arr[] = { 2, 3, 6, 4, 5 }
Output: 3 6
Explanation:
The only possible pairs are (2, 4), (2, 6), (3, 6) out of which the pair which have minimum distance between them is (3, 6).
Approach: The idea is to generate all possible pairs of the given array and check if there exists any pair of elements in the array if one element is the multiple of other and update the required minimum distance with the current pair. After the above operation print, the pair have the minimum distance among them and one is a multiple of the other. If no such pair exists then print -1.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void findPair( int a[], int n)
{
int min_dist = INT_MAX;
int index_a = -1, index_b = -1;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((a[i] % a[j] == 0 || a[j] % a[i] == 0) and j-i<min_dist)
{
min_dist = j - i;
index_a = i;
index_b = j;
}
}
}
if (index_a == -1)
{
cout << ( "-1" );
}
else
{
cout << "(" << a[index_a]
<< ", " << a[index_b] << ")" ;
}
}
int main()
{
int a[] = { 2, 3, 4, 5, 6 };
int n = sizeof (a)/ sizeof ( int );
findPair(a, n);
}
|
Java
import java.util.*;
class GFG {
public static void
findPair( int a[], int n)
{
int min_dist = Integer.MAX_VALUE;
int index_a = - 1 , index_b = - 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if ((a[i] % a[j] == 0
|| a[j] % a[i] == 0 ) && j-i<min_dist) {
min_dist = j - i;
index_a = i;
index_b = j;
}
}
}
if (index_a == - 1 ) {
System.out.println( "-1" );
}
else {
System.out.print(
"(" + a[index_a]
+ ", "
+ a[index_b] + ")" );
}
}
public static void
main(String[] args)
{
int a[] = { 2 , 3 , 4 , 5 , 6 };
int n = a.length;
findPair(a, n);
}
}
|
Python3
import sys
def findPair(a, n):
min_dist = sys.maxsize
index_a = - 1
index_b = - 1
for i in range (n):
for j in range (i + 1 , n):
if (((a[i] % a[j] = = 0 ) or
(a[j] % a[i] = = 0 )) and j - i<min_dist):
min_dist = j - i
index_a = i
index_b = j
if (index_a = = - 1 ):
print ( "-1" )
else :
print ( "(" , a[index_a],
", " , a[index_b], ")" )
a = [ 2 , 3 , 4 , 5 , 6 ]
n = len (a)
findPair(a, n)
|
C#
using System;
class GFG{
public static void findPair( int []a, int n)
{
int min_dist = int .MaxValue;
int index_a = -1, index_b = -1;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
{
if ((a[i] % a[j] == 0 ||
a[j] % a[i] == 0) && j-i<min_dist)
{
min_dist = j - i;
index_a = i;
index_b = j;
}
}
}
if (index_a == -1)
{
Console.WriteLine( "-1" );
}
else
{
Console.Write( "(" + a[index_a] +
", " + a[index_b] + ")" );
}
}
public static void Main(String[] args)
{
int []a = { 2, 3, 4, 5, 6 };
int n = a.Length;
findPair(a, n);
}
}
|
Javascript
<script>
function findPair(a, n)
{
let min_dist = Number.MAX_VALUE;
let index_a = -1, index_b = -1;
for (let i = 0; i < n; i++)
{
for (let j = i + 1; j < n; j++)
{
if ((a[i] % a[j] == 0 ||
a[j] % a[i] == 0) && j-i<min_dist)
{
min_dist = j - i;
index_a = i;
index_b = j;
}
}
}
if (index_a == -1)
{
document.write( "-1" );
}
else
{
document.write( "(" + a[index_a] +
", " + a[index_b] + ")" );
}
}
let a = [ 2, 3, 4, 5, 6 ];
let n = a.length;
findPair(a, n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
01 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...