Count squares of unique dimensions possible from given Straight Lines parallel to the axes
Given two arrays X[] and Y[] consisting of N and M integers such that there are N lines parallel to the y-axis and M lines parallel to the x-axis, the task is to find the total number of squares having unique dimensions that can be generated from the given straight lines.
Each integer(say a) in the array X[] denotes lines having equation x = a, parallel to
y-axis.
Each integer(say b) in the array Y[] denotes lines having equation y = b, parallel to
x-axis.
Examples:
Input: X[] = {1, 3, 7}, Y[] = {1, 2, 4, 6}
Output: 2
Explanation:
3 lines are parallel to y-axis for x = 1, x = 3 and x = 7.
4 lines are parallel to x-axis for y = 2, y = 4, y = 6 and y = 1.
From the above figure, there are two possible squares of unique dimensions that are possible:
1) square ABDC (x = 1, x = 3, y = 4, y = 6), side = 2 units.
2) square BGHF (x = 3, x = 7, y = 2, y = 6), side = 4 units.
Input: X[] = {2, 6, 7, 8}, Y[] = {1, 3, 5, 7}
Output: 3
Naive Approach: The simplest approach is to check for every possible vertical dimension if there exists an equal horizontal dimension. The time complexity of this approach can be reduced with the help of the map. By using a map, we can get all the different vertical/horizontal dimensions. i>Time Complexity: O((N2)*log N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to generate all possible dimensions with the help of bitsets. Follow the steps below to solve this problem:
- Initialize bitsets for horizontal and vertical lines from the array X[] and Y[] respectively.
- Set positions of vertical and horizontal lines in the bitset.
- Maintain another bitset hdiff and vdiff that store the different possible dimensions of squares. The dimensions can be obtained by shifting the set bits in the original bitset.
- After the above steps, the unique count of squares is the count of the common element in hdiff and vdiff which is calculated by (hdiff&vdiff).count().
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int countSquares( int * hor, int * ver,
int n, int m)
{
bitset<N> hpos, vpos;
for ( int i = 0; i < n; ++i) {
hpos.set(hor[i]);
}
for ( int i = 0; i < m; ++i) {
vpos.set(ver[i]);
}
bitset<N> hdiff, vdiff;
for ( int i = 0; i < n; ++i) {
hdiff = hdiff | (hpos >> hor[i]);
}
for ( int i = 0; i < m; ++i) {
vdiff = vdiff | (vpos >> ver[i]);
}
int common = (hdiff & vdiff).count();
cout << common - 1;
}
int main()
{
int X[] = { 1, 3, 7 };
int Y[] = { 1, 2, 4, 6 };
int N = ( sizeof (X) / sizeof (X[0]));
int M = ( sizeof (Y) / sizeof (Y[0]));
countSquares(X, Y, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int N = 100 ;
static void countSquares( int [] hor, int [] ver, int n, int m)
{
var hpos = 0 ;
var vpos = 0 ;
for (var i = 0 ; i < n; i++)
hpos |= ( 1 << hor[i]);
for (var i = 0 ; i < m; i++)
vpos |= ( 1 << ver[i]);
var hdiff = 0 ;
var vdiff = 0 ;
for (var i = 0 ; i < n; i++)
hdiff = hdiff | (hpos >> hor[i]);
for (var i = 0 ; i < m; i++)
vdiff = vdiff | (vpos >> ver[i]);
var common = 0 ;
var res = hdiff & vdiff;
while (res > 0 )
{
if ((res & 1 ) == 1 )
common++;
res = ( int )(res / 2 );
}
System.out.print(common - 1 );
}
public static void main(String[] args)
{
int [] X = { 1 , 3 , 7 };
int [] Y = { 1 , 2 , 4 , 6 };
N = X.length;
var M = Y.length;
countSquares(X, Y, N, M);
}
}
|
Python3
N = 100 ;
def countSquares(hor, ver, n, m):
hpos, vpos = 0 , 0 ;
for i in range (n):
hpos | = ( 1 << hor[i])
for i in range (m):
vpos | = ( 1 << ver[i])
hdiff, vdiff = 0 , 0 ;
for i in range (n):
hdiff = hdiff | (hpos >> hor[i])
for i in range (m):
vdiff = vdiff | (vpos >> ver[i])
common = bin (hdiff & vdiff).count( "1" );
print (common - 1 );
X = [ 1 , 3 , 7 ];
Y = [ 1 , 2 , 4 , 6 ]
N = len (X)
M = len (Y)
countSquares(X, Y, N, M);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int N = 100;
static void countSquares( int [] hor, int [] ver, int n, int m)
{
var hpos = 0;
var vpos = 0;
for ( var i = 0; i < n; i++)
hpos |= (1 << hor[i]);
for ( var i = 0; i < m; i++)
vpos |= (1 << ver[i]);
var hdiff = 0;
var vdiff = 0;
for ( var i = 0; i < n; i++)
hdiff = hdiff | (hpos >> hor[i]);
for ( var i = 0; i < m; i++)
vdiff = vdiff | (vpos >> ver[i]);
var common = 0;
var res = hdiff & vdiff;
while (res > 0)
{
if ((res & 1) == 1)
common++;
res = ( int )(res / 2);
}
Console.Write(common - 1);
}
public static void Main( string [] args)
{
int [] X = {1, 3, 7 };
int [] Y = {1, 2, 4, 6};
N = X.Length;
var M = Y.Length;
countSquares(X, Y, N, M);
}
}
|
Javascript
let N = 100;
function countSquares(hor, ver, n, m)
{
let hpos = 0
let vpos = 0
for ( var i = 0; i < n; i++)
hpos |= (1 << hor[i])
for ( var i = 0; i < m; i++)
vpos |= (1 << ver[i])
let hdiff = 0
let vdiff = 0
for ( var i = 0; i < n; i++)
hdiff = hdiff | (hpos >> hor[i])
for ( var i = 0; i < m; i++)
vdiff = vdiff | (vpos >> ver[i])
let common = 0;
let res = hdiff & vdiff
while (res > 0)
{
if (res & 1)
common++;
res = Math.floor(res / 2)
}
console.log(common - 1);
}
let X = [1, 3, 7 ];
let Y = [1, 2, 4, 6]
N = X.length
let M = Y.length
countSquares(X, Y, N, M);
|
Time Complexity: O(N + M)
Auxiliary Space: O(maxE), where maxE is the maximum element among both the arrays X[] and Y[].
Last Updated :
29 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...