📌  相关文章
📜  数组中最近的一对,使得一个数是另一个数的倍数

📅  最后修改于: 2021-09-07 02:34:05             🧑  作者: Mango

给定一个大小为N的整数数组arr[] ,任务是在给定数组中找到最接近的一对,使得一个元素是另一个的倍数。如果不存在这样的对,则打印-1

注意:最近对意味着任何两个元素的索引之间的差异必须是最小的。

例子:

方法:想法是生成给定数组的所有可能对,如果一个元素是另一个元素的倍数,则检查数组中是否存在任何元素对,并更新当前对所需的最小距离。经过上述操作打印后,这对之间的距离最小,一个是另一个的倍数。如果不存在这样的对,则打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
  
// Function to find the minimum
// distance pair where one is the
// multiple of the other
void findPair(int a[], int n)
{
  
  // Initialize the variables
  int min_dist = INT_MAX;
  int index_a = -1, index_b = -1;
 
  // Iterate for all the elements
  for (int i = 0; i < n; i++)
  {
 
    // Loop to make pairs
    for (int j = i + 1; j < n; j++)
    {
 
      // Check for minimum distance
      if (j - i < min_dist)
      {
 
        // Check if one is a
        // multiple of other
        if (a[i] % a[j] == 0 || a[j] % a[i] == 0)
        {
 
          // Update the distance
          min_dist = j - i;
 
          // Store indexes
          index_a = i;
          index_b = j;
        }
      }
    }
  }
 
  // If no such pair exists
  if (index_a == -1)
  {
    cout << ("-1");
  }
 
  // Print the answer
  else
  {
    cout << "(" <<  a[index_a]
       <<  ", " <<  a[index_b]  << ")";
  }
}
 
// Driver Code
int main()
{
  // Given array arr[]
  int a[] = { 2, 3, 4, 5, 6 };
  int n = sizeof(a)/sizeof(int);
 
  // Function Call
  findPair(a, n);
}
 
// This code is contributed by rock_cool


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
    // Function to find the minimum
    // distance pair where one is the
    // multiple of the other
    public static void
    findPair(int a[], int n)
    {
 
        // Initialize the variables
        int min_dist = Integer.MAX_VALUE;
        int index_a = -1, index_b = -1;
 
        // Iterate for all the elements
        for (int i = 0; i < n; i++) {
 
            // Loop to make pairs
            for (int j = i + 1; j < n; j++) {
 
                // Check for minimum distance
                if (j - i < min_dist) {
 
                    // Check if one is a
                    // multiple of other
                    if (a[i] % a[j] == 0
                        || a[j] % a[i] == 0) {
 
                        // Update the distance
                        min_dist = j - i;
 
                        // Store indexes
                        index_a = i;
                        index_b = j;
                    }
                }
            }
        }
 
        // If no such pair exists
        if (index_a == -1) {
            System.out.println("-1");
        }
 
        // Print the answer
        else {
            System.out.print(
                "(" + a[index_a]
                + ", "
                + a[index_b] + ")");
        }
    }
 
    // Driver Code
    public static void
        main(String[] args)
    {
        // Given array arr[]
        int a[] = { 2, 3, 4, 5, 6 };
        int n = a.length;
 
        // Function Call
        findPair(a, n);
    }
}


Python3
# Python3 program for the above approach
import sys
 
# Function to find the minimum
# distance pair where one is the
# multiple of the other
def findPair(a, n):
 
    # Initialize the variables
    min_dist = sys.maxsize
    index_a = -1
    index_b = -1
 
    # Iterate for all the elements
    for i in range(n):
         
        # Loop to make pairs
        for j in range(i + 1, n):
         
            # Check for minimum distance
            if (j - i < min_dist):
 
                # Check if one is a
                # multiple of other
                if ((a[i] % a[j] == 0) or
                    (a[j] % a[i] == 0)):
         
                    # Update the distance
                    min_dist = j - i
 
                    # Store indexes
                    index_a = i
                    index_b = j
 
    # If no such pair exists
    if (index_a == -1):
        print("-1")
 
    # Print the answer
    else:
        print("(", a[index_a],
             ", ", a[index_b], ")")
 
# Driver Code
 
# Given array arr[]
a = [ 2, 3, 4, 5, 6 ]
 
n = len(a)
 
# Function call
findPair(a, n)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
  
// Function to find the minimum
// distance pair where one is the
// multiple of the other
public static void findPair(int []a, int n)
{
     
    // Initialize the variables
    int min_dist = int.MaxValue;
    int index_a = -1, index_b = -1;
 
    // Iterate for all the elements
    for(int i = 0; i < n; i++)
    {
         
        // Loop to make pairs
        for(int j = i + 1; j < n; j++)
        {
 
            // Check for minimum distance
            if (j - i < min_dist)
            {
                 
                // Check if one is a
                // multiple of other
                if (a[i] % a[j] == 0 ||
                    a[j] % a[i] == 0)
                {
                     
                    // Update the distance
                    min_dist = j - i;
 
                    // Store indexes
                    index_a = i;
                    index_b = j;
                }
            }
        }
    }
 
    // If no such pair exists
    if (index_a == -1)
    {
        Console.WriteLine("-1");
    }
 
    // Print the answer
    else
    {
        Console.Write("(" + a[index_a] +
                     ", " + a[index_b] + ")");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []a = { 2, 3, 4, 5, 6 };
     
    int n = a.Length;
 
    // Function Call
    findPair(a, n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
(2, 4)

时间复杂度: O(N 2 )
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live