Find distinct integers for a triplet with given product | C # implementation of the approach ; Function to find the required triplets ; To store the factors ; Find factors in Math . Sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Print the valid triplet ; Triplet found ; Triplet not found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int x ) { List < int > fact = new List < int > ( ) ; HashSet < int > factors = new HashSet < int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . Add ( i ) ; if ( x / i != i ) fact . Add ( x / i ) ; factors . Add ( i ) ; factors . Add ( x / i ) ; } } bool found = false ; int k = fact . Count ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact [ i ] ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { Console . Write ( a + " ▁ " + b + " ▁ " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int x = 105 ; findTriplets ( x ) ; } }