📜  从除数列表中找到A和B

📅  最后修改于: 2021-04-24 18:06:00             🧑  作者: Mango

给定数组arr [] ,该数组包含两个整数AB (以及AB1 )的所有除数。任务是从给定的数组中找到AB。
注意:如果数字是AB的除数,那么它将在数组中出现两次。

例子:

方法:对于给定的数组,由于所有元素都是AB的除数,因此必须将数组中的最大元素强制为AB。为了简单起见,将其作为A。现在,有两种情况适用于元素为B的情况:

  1. B可以是最大元素,而不是A的除数。
  2. 或者B可以是小于A的最大元素,其频率为2

为了找到AB的值,对数组进行排序。将最大元素打印为A ,然后将不是A的除数或频率为2最大元素设为B。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print A and B all of whose
// divisors are present in the given array
void printNumbers(int arr[], int n)
{
  
    // Sort the array
    sort(arr, arr + n);
  
    // A is the largest element from the array
    int A = arr[n - 1], B = -1;
  
    // Iterate from the second largest element
    for (int i = n - 2; i >= 0; i--) {
  
        // If current element is not a divisor
        // of A then it must be B
        if (A % arr[i] != 0) {
            B = arr[i];
            break;
        }
  
        // If current element occurs more than once
        if (i - 1 >= 0 && arr[i] == arr[i - 1]) {
            B = arr[i];
            break;
        }
    }
  
    // Print A and B
    cout << "A = " << A << ", B = " << B;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 4, 8, 16, 1, 2, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printNumbers(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GfG {
  
    // Function to print A and B all of whose
    // divisors are present in the given array
    static void printNumbers(int arr[], int n)
    {
  
        // Sort the array
        Arrays.sort(arr);
  
        // A is the largest element from the array
        int A = arr[n - 1], B = -1;
  
        // Iterate from the second largest element
        for (int i = n - 2; i >= 0; i--) {
  
            // If current element is not a divisor
            // of A then it must be B
            if (A % arr[i] != 0) {
                B = arr[i];
                break;
            }
  
            // If current element occurs more than once
            if (i - 1 >= 0 && arr[i] == arr[i - 1]) {
                B = arr[i];
                break;
            }
        }
  
        // Print A and B
        System.out.print("A = " + A + ", B = " + B);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 4, 8, 16, 1, 2, 4 };
        int n = arr.length;
        printNumbers(arr, n);
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to print A and B all of whose 
# divisors are present in the given array 
def printNumbers(arr, n): 
  
    # Sort the array 
    arr.sort() 
  
    # A is the largest element from the array 
    A, B = arr[n - 1], -1
  
    # Iterate from the second largest element 
    for i in range(n - 2, -1, -1): 
  
        # If current element is not a divisor 
        # of A then it must be B 
        if A % arr[i] != 0: 
            B = arr[i] 
            break
  
        # If current element occurs more than once 
        if i - 1 >= 0 and arr[i] == arr[i - 1]: 
            B = arr[i] 
            break
  
    # Print A and B 
    print("A =", A, ", B =", B) 
  
# Driver code 
if __name__ == "__main__":
  
    arr = [1, 2, 4, 8, 16, 1, 2, 4] 
    n = len(arr) 
    printNumbers(arr, n)
  
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System.Collections;
using System;
  
class GfG 
{
  
    // Function to print A and B all of whose
    // divisors are present in the given array
    static void printNumbers(int []arr, int n)
    {
  
        // Sort the array
        Array.Sort(arr);
  
        // A is the largest element from the array
        int A = arr[n - 1], B = -1;
  
        // Iterate from the second largest element
        for (int i = n - 2; i >= 0; i--) 
        {
  
            // If current element is not a divisor
            // of A then it must be B
            if (A % arr[i] != 0) 
            {
                B = arr[i];
                break;
            }
  
            // If current element occurs more than once
            if (i - 1 >= 0 && arr[i] == arr[i - 1]) 
            {
                B = arr[i];
                break;
            }
        }
  
        // Print A and B
        Console.WriteLine("A = " + A + ", B = " + B);
    }
  
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 2, 4, 8, 16, 1, 2, 4 };
        int n = arr.Length;
        printNumbers(arr, n);
    }
}
  
// This code is contributed by mits


PHP
= 0; $i--) 
    {
  
        // If current element is not a divisor
        // of A then it must be B
        if ($A % $arr[$i] != 0)
        {
            $B = $arr[$i];
            break;
        }
  
        // If current element occurs more than once
        if ($i - 1 >= 0 && $arr[$i] == $arr[$i - 1]) 
        {
            $B = $arr[$i];
            break;
        }
    }
  
    // Print A and B
    echo("A = " . $A . ", B = " . $B);
}
  
// Driver code
$arr = array( 1, 2, 4, 8, 16, 1, 2, 4 );
$n = sizeof($arr);
printNumbers($arr, $n);
  
// This code is contributed by Code_Mech.


输出:
A = 16, B = 4