Queries to check whether all the elements can be made positive by flipping signs exactly K times | To store the count of negative integers ; Check if zero exists ; Function to find the count of negative integers and check if zero exists in the array ; Function that returns true if array elements can be made positive by changing signs exactly k times ; Driver code ; Pre - processing ; Perform queries
let cnt_neg = 0 ; let exists_zero = false ; function preProcess ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) cnt_neg ++ ; if ( arr [ i ] == 0 ) exists_zero = true ; } } function isPossible ( k ) { if ( ! exists_zero ) { if ( k >= cnt_neg && ( k - cnt_neg ) % 2 == 0 ) return true ; else return false ; } else { if ( k >= cnt_neg ) return true ; else return false ; } } let arr = [ - 1 , 2 , - 3 , 4 , 5 ] ; let n = arr . length ; preProcess ( arr , n ) ; let queries = [ 1 , 2 , 3 , 4 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { if ( isPossible ( queries [ i ] ) ) document . write ( " " ) ; else document . write ( " " ) ; }