Count smaller primes on the right of each array element | Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
function is_prime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function countSmallerPrimes ( ar , N ) { for ( let i = 0 ; i < N ; i ++ ) { let count = 0 ; for ( let j = i + 1 ; j < N ; j ++ ) { if ( ar [ j ] <= ar [ i ] && is_prime ( ar [ j ] ) ) { count ++ ; } } document . write ( count + " " ) ; } } let ar = [ 43 , 3 , 5 , 7 , 2 , 41 ] ; let N = ar . length ; countSmallerPrimes ( ar , N ) ;