📌  相关文章
📜  检查每对的乘积是否存在于数组中

📅  最后修改于: 2022-05-13 01:57:52.473000             🧑  作者: Mango

检查每对的乘积是否存在于数组中

给定一个包含 n 个整数的数组,我们需要检查对于每对数字 a[i] 和 a[j] 是否存在 aa[k] 使得 a[k] = a[i]*a[j] 其中 k也可以等于 i 或 j。
例子 :

Input : arr[] = {0. 1} 
Output : Yes
Here a[0]*a[1] is equal to a[0]

Input : arr[] = {5, 6}
Output : No

如果数组满足以下所有条件,则数组将满足问题条件:
条件 1:数组必须有 1、0、-1 以外的元素个数小于或等于 1,因为如果它有超过 1 个这样的元素,则数组中将不存在乘积等于最大的元素这两个(或更多)元素。假设此类元素的数量为 2,它们的值为 5、6,因此数组中没有等于 5*6 = 30 的元素。
条件 2:如果数组有其他数字,比如 x(0、1 和 -1 除外)并且 -1 也存在,那么答案也是错误的。因为 -1 的存在要求 x 和 -x 都应该存在,但这违反了条件 1。
条件 3:如果有多个“-1”且数组中没有一个,则答案也将为否,因为两个“-1”的乘积等于 1。
下面是上述条件的实现。

C++
// C++ program to find if
// product of every pair
// is present in array.
#include
using namespace std;
 
// Returns true if product
// of every pair in arr[]
// is present in arr[]
bool checkArray(int arr[] , int n)
{
    // variable to store number
    //  of zeroes, ones, minus
    // one and other numbers.
    int zero = 0, one = 0,
        minusone = 0, other=0;
    for (int i = 0; i < n; i++)
    {
        // incrementing the
        // variable values
        if (arr[i] == 0)
            zero++;
        else if (arr[i] == 1)
            one++;
        else if (arr[i] == -1)
            minusone++;
        else
            other++;
    }
 
    // checking the conditions
    if (other > 1)
        return false;
    else if (other != 0 &&
             minusone != 0)
        return false;
    else if (minusone > 1 &&
             one == 0)
        return false;
 
    return true;
}
 
// Driver Code
int main()
{
    int arr[] = {0, 1, 1, 10};
    int n = sizeof(arr) / sizeof(arr[0]);
    if (checkArray(arr, n))
    cout << "Yes";
    else
    cout << "No";
    return 0;
}


Java
// Java program to find
// if product of every pair
// is present in array.
 
class GFG
{
    // Returns true if product
    // of every pair in arr[]
    // is present in arr[]
    static boolean checkArray(int arr[] ,
                              int n)
    {
        // variable to store number
        //  of zeroes, ones, minus
        // one and other numbers.
        int zero = 0, one = 0,
            minusone = 0, other=0;
        for (int i = 0; i < n; i++)
        {
            // incrementing the
            // variable values
            if (arr[i] == 0)
                zero++;
            else if (arr[i] == 1)
                one++;
            else if (arr[i] == -1)
                minusone++;
            else
                other++;
        }
     
        // checking the conditions
        if (other > 1)
            return false;
        else if (other != 0 &&
                 minusone != 0)
            return false;
        else if (minusone > 1 &&
                 one == 0)
            return false;
     
        return true;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = {0, 1, 1, 10};
        int n = arr.length;
        if (checkArray(arr, n))
        System.out.println("Yes");
        else
        System.out.println("No");
    }
}
 
// This code is contributed by Harsh Agarwal


Python3
# Python3 program to find
# if product of every pair
# is present in array.
 
# Returns True if product
# of every pair in arr[] is
# present in arr[]
def checkArray(arr, n):
 
    # variable to store number
    # of zeroes, ones, minus
    # one and other numbers.
    zero = 0; one = 0;
    minusone = 0; other = 0
    for i in range(0, n):
     
        # incrementing the
        # variable values
        if (arr[i] == 0):
            zero += 1
        elif (arr[i] == 1):
            one += 1
        elif (arr[i] == -1):
            minusone += 1
        else:
            other += 1
     
    # checking the conditions
    if (other > 1):
        return false
    elif (other != 0 and
          minusone != 0):
        return false
    elif (minusone > 1 and
          one == 0):
        return false
 
    return True
 
 
# Driver Code
arr = [0, 1, 1, 10]
n = len(arr)
if (checkArray(arr, n)):
    print("Yes")
else:
    print("No")
     
# This code is contributed
# by Smitha Dinesh Semwal.


C#
// C# program to find if
// product of every pair
// is present in array.
using System;
 
class GFG
{
    // Returns true if product
    // of every pair in arr[]
    // is present in arr[]
    static Boolean checkArray(int []arr ,
                              int n)
    {
        // variable to store number
        // of zeroes, ones, minus
        // one and other numbers.
        int zero = 0, one = 0,
            minusone = 0, other=0;
        for (int i = 0; i < n; i++)
        {
            // incrementing the
            // variable values
            if (arr[i] == 0)
                zero++;
            else if (arr[i] == 1)
                one++;
            else if (arr[i] == -1)
                minusone++;
            else
                other++;
        }
     
        // checking the conditions
        if (other > 1)
            return false;
        else if (other != 0 &&
                 minusone != 0)
            return false;
        else if (minusone > 1 &&
                 one == 0)
            return false;
     
        return true;
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        int []arr = {0, 1, 1, 10};
        int n = arr.Length;
        if (checkArray(arr, n))
        Console.Write("Yes");
        else
        Console.Write("No");
    }
}
 
// This code is contributed by parashar....


PHP
 1)
        return false;
    else if ($other != 0 &&
             $minusone != 0)
        return false;
    else if ($minusone > 1 &&
             $one == 0)
        return false;
 
    return true;
}
 
// Driver Code
{
    $arr = array(0, 1, 1, 10);
    $n = sizeof($arr) / sizeof($arr[0]);
    if (checkArray($arr, $n))
    echo "Yes";
    else
    echo "No";
    return 0;
}
 
//This code is contributed
// by nitin mittal.
?>


Javascript


输出 :

yes

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