Query to find length of the longest subarray consisting only of 1 s | C # Program for the above approach ; 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 ; Driver code ; Size of array ; Given array ; Given queries ; Number of queries
using System ; using System . Collections . Generic ; class GFG { static int longestsubarray ( int [ ] a , int N ) { int maxlength = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . Max ( maxlength , sum ) ; } return maxlength ; } static void solveQueries ( int [ ] arr , int n , List < List < int > > Q , int k ) { int cntQuery = Q . Count ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { Console . Write ( longestsubarray ( arr , n ) + " ▁ " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; } } } static void Main ( ) { int N = 10 ; int [ ] arr = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; List < List < int > > Q = new List < List < int > > ( ) ; Q . Add ( new List < int > { 1 } ) ; Q . Add ( new List < int > { 2 , 3 } ) ; Q . Add ( new List < int > { 1 } ) ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }