Number of 0 s and 1 s at prime positions in the given array | Function that returns true if n is prime ; Check from 2 to n ; Function to find the count of 0 s and 1 s at prime indices ; To store the count of 0 s and 1 s ; If current 0 is at prime position ; If current 1 is at prime position ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function countPrimePosition ( arr , n ) { let c0 = 0 , c1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } document . write ( " " + c0 + " " ) ; document . write ( " " + c1 ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 ] ; let n = arr . length ; countPrimePosition ( arr , n ) ;