Given two binary arrays A[] and B[], of length N and M respectively, the task is to find the number of rectangles of area K consisting of 1‘s in the matrix C[][] generated by multiplying the two arrays such that, C[i][j] = A[i] * B[j] ( 1 < i < n, 1 < j < m).
Examples:
Input: N= 3, M = 3, A[] = {1, 1, 0}, B[] = {0, 1, 1}, K = 2
Output: 4
Explanation: C[][] = {{0, 1, 1}, {0, 1, 1}, {0, 0, 0}}0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0Therefore, there are 4 possible rectangles of area 2 from the matrix.
Input: N = 4, M = 2, A[] = {0, 0, 1, 1}, B[] = {1, 0, 1}, K = 2
Output: 2
Explanation: C[][] = {{0, 0, 0}, {0, 0, 0}, {1, 0, 1}, {1, 0, 1}}
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 1 0 1Therefore, there are 2 possible rectangles of area 2 in the matrix.
Naive Approach: The simplest approach to solve the problem is to generate the required matrix by multiplying the two arrays and for every possible rectangle of area K, check if it consists of only 1’s or not.
Time Complexity: O(N * M * K)
Auxiliary Space: O(N * M)
Efficient Approach: To optimize the above approach, the following observations need to be made instead of generating the matrix:
- The area of a rectangle is equal to the product of its length and breadth.
- Using this property, visualize the rectangle as a submatrix which contains only 1s. Therefore, this submatrix is the result of the product of two subarrays of length a, b where a * b = K.
- Since the submatrix contains only 1‘s, it is obvious that these two subarrays also contain only 1‘s in them.
Therefore, the problem reduces to finding the subarrays consisting of only 1‘s of all possible lengths which are proper divisors of K, from the arrays A[] and B[]. Follow the steps below to solve the problem:
- Precalculate the count of possible subarrays.
- Iterate through all the divisors of K and for each possible pair (p, q) where p * q = K, check if there exist subarrays of length p, q in A[], and B[].
- Increase the count of possible such subarrays accordingly and finally, print the obtained count.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the subarrays of // all possible lengths made up of only 1s vector<int> findSubarrays(vector<int>& a) { int n = a.size(); // Stores the frequency // of the subarrays vector<int> freq(n + 1); int count = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { // Check if the previous // value was also 0 if (count == 0) continue; // If the previous value was 1 else { int value = count; for (int j = 1; j <= count; j++) { // Find the subarrays of // each size from 1 to count freq[j] += value; value--; } count = 0; } } else count++; } // If A[] is of the form ....111 if (count > 0) { int value = count; for (int j = 1; j <= count; j++) { freq[j] += value; value--; } } return freq; } // Function to find the count // of all possible rectangles void countRectangles(vector<int>& a, vector<int>& b, int K) { // Size of each of the arrays int n = a.size(); int m = b.size(); // Stores the count of subarrays // of each size consisting of // only 1s from array A[] vector<int> subA = findSubarrays(a); // Stores the count of subarrays // of each size consisting of // only 1s from array B[] vector<int> subB = findSubarrays(b); int total = 0; // Iterating over all subarrays // consisting of only 1s in A[] for (int i = 1; i < subA.size(); i++) { // If i is a factor of K, then // there is a subarray of size K/i in B[] if (K % i == 0 and (K / i) <= m) { total = total + subA[i] * subB[K / i]; } } cout << total; } // Driver Code int main() { vector<int> a = { 0, 0, 1, 1 }; vector<int> b = { 1, 0, 1 }; int K = 2; countRectangles(a, b, K); return 0; } |
Java
// Java Program to implement // the above approach class GFG{ // Function to find the subarrays of // all possible lengths made up of only 1s static int[] findSubarrays(int[] a) { int n = a.length; // Stores the frequency // of the subarrays int[] freq = new int[n + 1]; int count = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { // Check if the previous // value was also 0 if (count == 0) continue; // If the previous value was 1 else { int value = count; for (int j = 1; j <= count; j++) { // Find the subarrays of // each size from 1 to count freq[j] += value; value--; } count = 0; } } else count++; } // If A[] is of the form ....111 if (count > 0) { int value = count; for (int j = 1; j <= count; j++) { freq[j] += value; value--; } } return freq; } // Function to find the count // of all possible rectangles static void countRectangles(int[] a, int[] b, int K) { // Size of each of the arrays int n = a.length; int m = b.length; // Stores the count of subarrays // of each size consisting of // only 1s from array A[] int[] subA = findSubarrays(a); // Stores the count of subarrays // of each size consisting of // only 1s from array B[] int[] subB = findSubarrays(b); int total = 0; // Iterating over all subarrays // consisting of only 1s in A[] for (int i = 1; i < subA.length; i++) { // If i is a factor of K, then // there is a subarray of size K/i in B[] if (K % i == 0 && (K / i) <= m) { total = total + subA[i] * subB[K / i]; } } System.out.print(total); } // Driver Code public static void main(String[] args) { int[] a = {0, 0, 1, 1}; int[] b = {1, 0, 1}; int K = 2; countRectangles(a, b, K); } } // This code is contributed by shikhasingrajput |
Python3
# Python3 program to implement # the above approach # Function to find the subarrays of # all possible lengths made up of only 1s def findSubarrays(a): n = len(a) # Stores the frequency # of the subarrays freq = [0] * (n + 1) count = 0 for i in range(n): if (a[i] == 0): # Check if the previous # value was also 0 if (count == 0): continue # If the previous value was 1 else: value = count for j in range(1, count + 1): # Find the subarrays of # each size from 1 to count freq[j] += value value -= 1 count = 0 else: count += 1 # If A[] is of the form ....111 if (count > 0): value = count for j in range(1, count + 1): freq[j] += value value -= 1 return freq # Function to find the count # of all possible rectangles def countRectangles(a, b, K): # Size of each of the arrays n = len(a) m = len(b) # Stores the count of subarrays # of each size consisting of # only 1s from array A[] subA = [] subA = findSubarrays(a) # Stores the count of subarrays # of each size consisting of # only 1s from array B[] subB = [] subB = findSubarrays(b) total = 0 # Iterating over all subarrays # consisting of only 1s in A[] for i in range(1, len(subA)): # If i is a factor of K, then # there is a subarray of size K/i in B[] if (K % i == 0 and (K // i) <= m): total = total + subA[i] * subB[K // i] print(total) # Driver Code a = [ 0, 0, 1, 1 ] b = [ 1, 0, 1 ] K = 2 countRectangles(a, b, K) # This code is contributed by code_hunt |
C#
// C# Program to implement // the above approach using System; class GFG{ // Function to find the subarrays of // all possible lengths made up of only 1s static int[] findSubarrays(int[] a) { int n = a.Length; // Stores the frequency // of the subarrays int[] freq = new int[n + 1]; int count = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { // Check if the previous // value was also 0 if (count == 0) continue; // If the previous value was 1 else { int value = count; for (int j = 1; j <= count; j++) { // Find the subarrays of // each size from 1 to count freq[j] += value; value--; } count = 0; } } else count++; } // If []A is of the form ....111 if (count > 0) { int value = count; for (int j = 1; j <= count; j++) { freq[j] += value; value--; } } return freq; } // Function to find the count // of all possible rectangles static void countRectangles(int[] a, int[] b, int K) { // Size of each of the arrays int n = a.Length; int m = b.Length; // Stores the count of subarrays // of each size consisting of // only 1s from array []A int[] subA = findSubarrays(a); // Stores the count of subarrays // of each size consisting of // only 1s from array []B int[] subB = findSubarrays(b); int total = 0; // Iterating over all subarrays // consisting of only 1s in []A for (int i = 1; i < subA.Length; i++) { // If i is a factor of K, then // there is a subarray of size K/i in []B if (K % i == 0 && (K / i) <= m) { total = total + subA[i] * subB[K / i]; } } Console.Write(total); } // Driver Code public static void Main(String[] args) { int[] a = {0, 0, 1, 1}; int[] b = {1, 0, 1}; int K = 2; countRectangles(a, b, K); } } // This code is contributed by shikhasingrajput |
2
Time Complexity: O(D) * O(N + M), where D is the number of divisors of K.
Auxiliary Space: O(N + M)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count N-digit numbers made up of X or Y whose sum of digits is also made up of X or Y
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Minimum area of square holding two identical rectangles
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Total area of two overlapping rectangles
- Count subarrays consisting of only 0's and only 1's in a binary array
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Find the n-th number made of even digits only
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Nth number made up of odd digits only
- Minimum number closest to N made up of odd digits only
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Count of distinct rectangles inscribed in an equilateral triangle
- Count Distinct Rectangles in N*N Chessboard
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count of smaller rectangles that can be placed inside a bigger rectangle
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Maximum given sized rectangles that can be cut out of a sheet of paper
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

