Open In App

Check if product of every pair exists in an array

Given a array of n integers, we need to check whether for every pair of numbers a[i] & a[j] there exists a a[k] such that a[k] = a[i]*a[j] where k can be equal to i or j too.

Examples : 



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

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

An array will satisfy the problem conditions if the array follows all the below mentioned conditions : 

Below is the implementation of above conditions.






// C++ program to find if
// product of every pair
// is present in array.
#include<bits/stdc++.h>
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 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 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# 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
// PHP 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[]
function 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 = 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
{
    $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.
?>




<script>
//javascript 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[]
function checkArray(arr,n)
{
    // variable to store number
    // of zeroes, ones, minus
    // one and other numbers.
    let zero = 0, one = 0,
        minusone = 0, other=0;
    for (let 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;
}
 
    let arr = [0, 1, 1, 10];
    let n = arr.length;
    if (checkArray(arr, n))
    document.write("Yes");
    else
    document.write("No");
 
// This code is contributed by vaibhavrabadiya117.
</script>

Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(1)

 


Article Tags :