Find the array element having equal sum of Prime Numbers on its left and right | Function to find an index in the array having sum of prime numbers to its left and right equal ; Stores the maximum value present in the array ; Stores all positive elements which are <= max_value ; If 1 is present ; Remove 1 ; Sieve of Eratosthenes to store all prime numbers which are <= max_value in the Map ; Erase non - prime numbers ; Stores the sum of prime numbers from left ; Stores the sum of prime numbers to the left of each index ; Stores the sum of prime numbers to the left of the current index ; Add current value to the prime sum if the current value is prime ; Stores the sum of prime numbers from right ; Stores the sum of prime numbers to the right of each index ; Stores the sum of prime numbers to the right of the current index ; Add current value to the prime sum if the current value is prime ; Traverse through the two arrays to find the index ; Compare the values present at the current index ; Return the index where both the values are same ; No index is found . ; Given array arr [ ] ; Size of Array ; Function Call
function find_index ( arr , N ) { let max_value = Number . MIN_VALUE ; for ( let i = 0 ; i < N ; i ++ ) { max_value = Math . max ( max_value , arr [ i ] ) ; } let store = new Map ( ) ; for ( let i = 1 ; i <= max_value ; i ++ ) { if ( ! store . has ( i ) ) store . set ( i , 0 ) ; store . set ( i , store . get ( i ) + 1 ) ; } if ( store . has ( 1 ) ) { store . delete ( 1 ) ; } for ( let i = 2 ; i <= Math . sqrt ( max_value ) ; i ++ ) { let multiple = 2 ; while ( ( i * multiple ) <= max_value ) { if ( store . has ( i * multiple ) ) { store . delete ( i * multiple ) ; } multiple ++ ; } } let prime_sum_from_left = 0 ; let first_array = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { first_array [ i ] = prime_sum_from_left ; if ( store . has ( arr [ i ] ) ) { prime_sum_from_left += arr [ i ] ; } } let prime_sum_from_right = 0 ; let second_array = new Array ( N ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { second_array [ i ] = prime_sum_from_right ; if ( store . has ( arr [ i ] ) ) { prime_sum_from_right += arr [ i ] ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( first_array [ i ] == second_array [ i ] ) { return i ; } } return - 1 ; } let arr = [ 11 , 4 , 7 , 6 , 13 , 1 , 5 ] ; let N = arr . length ; document . write ( find_index ( arr , N ) ) ;