Count of Rectangles with area K made up of only 1 s from given Binary Arrays | Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If A [ ] is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array A [ ] ; Stores the count of subarrays of each size consisting of only 1 s from array B [ ] ; Iterating over all subarrays consisting of only 1 s in A [ ] ; If i is a factor of K , then there is a subarray of size K / i in B [ ] ; Driver Code
function findSubarrays ( a ) { let n = a . length ; let freq = new Array ( n + 1 ) . fill ( 0 ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { if ( count == 0 ) continue ; else { let value = count ; for ( let j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } count = 0 ; } } else count ++ ; } if ( count > 0 ) { let value = count ; for ( let j = 1 ; j <= count ; j ++ ) { freq [ j ] += value ; value -- ; } } return freq ; } function countRectangles ( a , b , K ) { let n = a . length ; let m = b . length ; let subA = findSubarrays ( a ) ; let subB = findSubarrays ( b ) ; let total = 0 ; for ( let i = 1 ; i < subA . length ; i ++ ) { if ( K % i == 0 && ( K / i ) <= m ) { total = total + subA [ i ] * subB [ K / i ] ; } } document . write ( total ) ; } let a = [ 0 , 0 , 1 , 1 ] ; let b = [ 1 , 0 , 1 ] ; let K = 2 ; countRectangles ( a , b , K ) ;