Check if the array has an element which is equal to product of remaining elements
Given an array of N elements, the task is to check if the array has an element which is equal to the product of all the remaining elements.
Examples:
Input: arr[] = {1, 2, 12, 3, 2}
Output: YES
12 is the product of all the remaining elements
i.e. 1 * 2 * 3 * 2 = 12
Input: arr[] = {1, 2, 3}
Output: NO
Method-1:
- First, take the product of all the element of the array.
- Now traverse the whole array again.
- For any element a[i] check if it is equal to the product of all elements divided by that element.
- Print Yes if at least one such element is found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool CheckArray( int arr[], int n)
{
int prod = 1;
for ( int i = 0; i < n; ++i)
prod *= arr[i];
for ( int i = 0; i < n; ++i)
if (arr[i] == prod / arr[i])
return true ;
return false ;
}
int main()
{
int arr[] = { 1, 2, 12, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
if (CheckArray(arr, n))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean CheckArray( int arr[], int n)
{
int prod = 1 ;
for ( int i = 0 ; i < n; ++i)
prod *= arr[i];
for ( int i = 0 ; i < n; ++i)
if (arr[i] == prod / arr[i])
return true ;
return false ;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 12 , 3 , 2 };
int n =arr.length;
if (CheckArray(arr, n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def CheckArray(arr, n):
prod = 1
for i in range ( 0 , n, 1 ):
prod * = arr[i]
for i in range ( 0 , n, 1 ):
if (arr[i] = = prod / arr[i]):
return True
return False
if __name__ = = '__main__' :
arr = [ 1 , 2 , 12 , 3 , 2 ]
n = len (arr)
if (CheckArray(arr, n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
class GFG
{
static bool CheckArray( int [] arr, int n)
{
int prod = 1;
for ( int i = 0; i < n; ++i)
prod *= arr[i];
for ( int i = 0; i < n; ++i)
if (arr[i] == prod / arr[i])
return true ;
return false ;
}
public static void Main ()
{
int [] arr = new int [] { 1, 2, 12, 3, 2 };
int n = arr.Length;
if (CheckArray(arr, n))
System.Console.WriteLine( "YES" );
else
System.Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function CheckArray( $arr , $n )
{
$prod = 1;
for ( $i = 0; $i < $n ; ++ $i )
$prod *= $arr [ $i ];
for ( $i = 0; $i < $n ; ++ $i )
if ( $arr [ $i ] == $prod / $arr [ $i ])
return true;
return false;
}
$arr = array (1, 2, 12, 3, 2);
$n = sizeof( $arr );
if (CheckArray( $arr , $n ))
echo "YES" ;
else
echo "NO" ;
|
Javascript
<script>
function CheckArray(arr,n)
{
let prod = 1;
for (let i = 0; i < n; ++i)
prod *= arr[i];
for (let i = 0; i < n; ++i)
if (arr[i] == prod / arr[i])
return true ;
return false ;
}
let arr = [ 1, 2, 12, 3, 2 ];
let n =arr.length;
if (CheckArray(arr, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Method-2:
The approach is to find the product of all the elements of the array and check if it is a perfect square or not. If it is a perfect square then check if the square root of the product exists in the array or not. If exists then print Yes else print No.
According to the problem statement, a * b = N
where b is the product of all the remaining elements of the array except a i.e arr[i]
And it is also mentioned that find the index such that a = b.
So, it simply means that a*a = N i.e. N is a perfect square. and a is its square root.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool CheckArray( int arr[], int n)
{
int prod = 1;
unordered_set< int > freq;
for ( int i = 0; i < n; ++i) {
freq.insert(arr[i]);
prod *= arr[i];
}
int root = sqrt (prod);
if (root * root == prod)
if (freq.find(root) != freq.end())
return true ;
return false ;
}
int main()
{
int arr[] = { 1, 2, 12, 3, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
if (CheckArray(arr, n))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static boolean CheckArray( int arr[], int n) {
int prod = 1 ;
ArrayList<Integer> freq = new ArrayList<>();
for ( int i = 0 ; i < n; ++i) {
freq.add(arr[i]);
prod *= arr[i];
}
int root = ( int ) Math.sqrt(prod);
if (root * root == prod)
{
if (freq.contains(root) & freq.lastIndexOf(root) != (freq.size())) {
return true ;
}
}
return false ;
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 12 , 3 , 2 };
int n = arr.length;
if (CheckArray(arr, n)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
import math
def CheckArray( arr, n):
prod = 1
freq = []
for i in range (n) :
freq.append(arr[i])
prod * = arr[i]
root = math.sqrt(prod)
if (root * root = = prod):
if root in freq:
return True
return False
if __name__ = = "__main__" :
arr = [ 1 , 2 , 12 , 3 , 2 ]
n = len (arr)
if (CheckArray(arr, n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections;
class GFG
{
static bool CheckArray( int []arr, int n)
{
int prod = 1;
ArrayList freq = new ArrayList();
for ( int i = 0; i < n; ++i)
{
freq.Add(arr[i]);
prod *= arr[i];
}
int root = ( int ) Math.Sqrt(prod);
if (root * root == prod)
{
if (freq.Contains(root) & freq.LastIndexOf(root) != (freq.Count))
{
return true ;
}
}
return false ;
}
public static void Main()
{
int []arr = {1, 2, 12, 3, 2};
int n = arr.Length;
if (CheckArray(arr, n))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
PHP
<?php
function CheckArray( $arr , $n )
{
$prod = 1;
$freq = array ();
for ( $i = 0; $i < $n ; ++ $i )
{
array_push ( $freq , $arr [ $i ]);
$prod *= $arr [ $i ];
}
$freq = array_unique ( $freq );
$root = (int)(sqrt( $prod ));
if ( $root * $root == $prod )
if (in_array( $root , $freq ))
return true;
return false;
}
$arr = array ( 1, 2, 12, 3, 2 );
$n = count ( $arr );
if (CheckArray( $arr , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function CheckArray(arr,n)
{
let prod = 1;
let freq = [];
for (let i = 0; i < n; ++i) {
freq.push(arr[i]);
prod *= arr[i];
}
let root = Math.floor(Math.sqrt(prod));
if (root * root == prod)
{
if (freq.includes(root) &
freq.lastIndexOf(root) != (freq.length))
{
return true ;
}
}
return false ;
}
let arr=[1, 2, 12, 3, 2];
let n = arr.length;
if (CheckArray(arr, n)) {
document.write( "YES" );
} else {
document.write( "NO" );
}
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...