Pair with given product | Set 1 ( Find if any pair exists ) | C # program if there exists a pair for given product ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element into it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static bool isProduct ( int [ ] arr , int n , int x ) { HashSet < int > hset = new HashSet < int > ( ) ; if ( n < 2 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) { return true ; } else { continue ; } } if ( x % arr [ i ] == 0 ) { if ( hset . Contains ( x / arr [ i ] ) ) { return true ; } hset . Add ( arr [ i ] ) ; } } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . Length ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } x = 190 ; if ( isProduct ( arr , arr . Length , x ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }