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 :
- Condition 1 : The array must have number of elements other than 1, 0, -1 less than or equal to 1 because if it has more than 1 such elements there will be no element present in the array whose product will be equal to the largest of those two (or more) elements. Suppose that number of such elements be 2 and their values are 5, 6 so there is no element equal to 5*6 = 30 in the array.
- Condition 2 : If the array has an other number say x (other than 0, 1 and -1) and -1 is also present, then also answer is false. Because presence of -1 makes it required that both x and -x should be present, but this violates condition 1.
- Condition 3 : if there are more than one “-1” and no one in the array then also answer will be no because the product of two “-1” is equal to 1.
Below is the implementation of above conditions.
C++
#include<bits/stdc++.h>
using namespace std;
bool checkArray( int arr[] , int n)
{
int zero = 0, one = 0,
minusone = 0, other=0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
zero++;
else if (arr[i] == 1)
one++;
else if (arr[i] == -1)
minusone++;
else
other++;
}
if (other > 1)
return false ;
else if (other != 0 &&
minusone != 0)
return false ;
else if (minusone > 1 &&
one == 0)
return false ;
return true ;
}
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
class GFG
{
static boolean checkArray( int arr[] ,
int n)
{
int zero = 0 , one = 0 ,
minusone = 0 , other= 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == 0 )
zero++;
else if (arr[i] == 1 )
one++;
else if (arr[i] == - 1 )
minusone++;
else
other++;
}
if (other > 1 )
return false ;
else if (other != 0 &&
minusone != 0 )
return false ;
else if (minusone > 1 &&
one == 0 )
return false ;
return true ;
}
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" );
}
}
|
Python3
def checkArray(arr, n):
zero = 0 ; one = 0 ;
minusone = 0 ; other = 0
for i in range ( 0 , n):
if (arr[i] = = 0 ):
zero + = 1
elif (arr[i] = = 1 ):
one + = 1
elif (arr[i] = = - 1 ):
minusone + = 1
else :
other + = 1
if (other > 1 ):
return false
elif (other ! = 0 and
minusone ! = 0 ):
return false
elif (minusone > 1 and
one = = 0 ):
return false
return True
arr = [ 0 , 1 , 1 , 10 ]
n = len (arr)
if (checkArray(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static Boolean checkArray( int []arr ,
int n)
{
int zero = 0, one = 0,
minusone = 0, other=0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 0)
zero++;
else if (arr[i] == 1)
one++;
else if (arr[i] == -1)
minusone++;
else
other++;
}
if (other > 1)
return false ;
else if (other != 0 &&
minusone != 0)
return false ;
else if (minusone > 1 &&
one == 0)
return false ;
return true ;
}
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" );
}
}
|
PHP
<?php
function checkArray( $arr , $n )
{
$zero = 0; $one = 0;
$minusone = 0; $other =0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 0)
$zero ++;
else if ( $arr [ $i ] == 1)
$one ++;
else if ( $arr [ $i ] == -1)
$minusone ++;
else
$other ++;
}
if ( $other > 1)
return false;
else if ( $other != 0 &&
$minusone != 0)
return false;
else if ( $minusone > 1 &&
$one == 0)
return false;
return true;
}
{
$arr = array (0, 1, 1, 10);
$n = sizeof( $arr ) / sizeof( $arr [0]);
if (checkArray( $arr , $n ))
echo "Yes" ;
else
echo "No" ;
return 0;
}
?>
|
Javascript
<script>
function checkArray(arr,n)
{
let zero = 0, one = 0,
minusone = 0, other=0;
for (let i = 0; i < n; i++)
{
if (arr[i] == 0)
zero++;
else if (arr[i] == 1)
one++;
else if (arr[i] == -1)
minusone++;
else
other++;
}
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" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...