Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | Java implementation to count the number of unordered triplets such that the numbers are in increasing order and the product of them is less than or equal to integer X ; Function to count the number of triplets ; Iterate through all the triplets ; Rearrange the numbers in ascending order ; Check if the necessary conditions satisfy ; Increment count ; Return the answer ; Driver code
import java . util . * ; class GFG { static int countTriplets ( int a [ ] , int n , int x ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { Vector < Integer > temp = new Vector < > ( ) ; temp . add ( a [ i ] ) ; temp . add ( a [ j ] ) ; temp . add ( a [ k ] ) ; Collections . sort ( temp ) ; if ( temp . get ( 0 ) < temp . get ( 1 ) && temp . get ( 1 ) < temp . get ( 2 ) && temp . get ( 0 ) * temp . get ( 1 ) * temp . get ( 2 ) <= x ) answer ++ ; } } } return answer ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 5 , 7 } ; int N = A . length ; int X = 42 ; System . out . println ( countTriplets ( A , N , X ) ) ; } }