Query to find length of the longest subarray consisting only of 1 s | Function to calculate the longest subarray consisting of 1 s only ; Stores the maximum length of subarray containing 1 s only ; Traverse the array ; If current element is '1' ; Increment length ; Otherwise ; Reset length ; Update maximum subarray length ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Flip the character ; Size of array ; Given array ; Given queries ; Number of queries
function longestsubarray ( a , N ) { var maxlength = 0 , sum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . max ( maxlength , sum ) ; } return maxlength ; } function solveQueries ( arr , n , Q , k ) { var cntQuery = Q . length ; for ( var i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { document . write ( longestsubarray ( arr , n ) + " " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; } } } var N = 10 ; var arr = [ 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 ] ; var Q = [ [ 1 ] , [ 2 , 3 ] , [ 1 ] ] ; var K = 3 ; solveQueries ( arr , N , Q , K ) ;