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++ 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 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 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# 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 |
<script> // Javascript program to implement // the above approach // Function to find the subarrays of
// all possible lengths made up of only 1s
function findSubarrays(a)
{
let n = a.length;
// Stores the frequency
// of the subarrays
let freq = new Array(n+1).fill(0);
let count = 0;
for (let 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
{
let value = count;
for (let 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)
{
let value = count;
for (let j = 1; j <= count; j++)
{
freq[j] += value;
value--;
}
}
return freq;
}
// Function to find the count
// of all possible rectangles
function countRectangles(a, b, K)
{
// Size of each of the arrays
let n = a.length;
let m = b.length;
// Stores the count of subarrays
// of each size consisting of
// only 1s from array A[]
let subA = findSubarrays(a);
// Stores the count of subarrays
// of each size consisting of
// only 1s from array B[]
let subB = findSubarrays(b);
let total = 0;
// Iterating over all subarrays
// consisting of only 1s in A[]
for (let 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];
}
}
document.write(total);
}
// Driver Code let a = [0, 0, 1, 1];
let b = [1, 0, 1];
let K = 2;
countRectangles(a, b, K);
</script> |
2
Time Complexity: O(D) * O(N + M), where D is the number of divisors of K.
Auxiliary Space: O(N + M)