Given ‘N’ number of sticks of length a1, a2, a3…an. The task is to count the number of squares and rectangles possible.
Note: One stick should be used only once i.e. either in any of the squares or rectangles.
Examples:
Input: arr[] = {1, 2, 1, 2}
Output: 1
Rectangle with sides 1 1 2 2
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output: 0
No square or rectangle is possible
Approach: Below is the step by step algorithm to solve this problem :
- Initialize the number of sticks.
- Initialize all the sticks with it’s lengths in an array.
- Sort the array in an increasing order.
- Calculate the number of pairs of sticks with the same length.
- Divide the total number of pairs by 2, which will be the total possible rectangle and square.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rectangleSquare( int arr[], int n)
{
sort(arr, arr + n);
int count = 0;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1]) {
count++;
i++;
}
}
return count / 2;
}
int main()
{
int arr[] = { 2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << rectangleSquare(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int rectangleSquare( int arr[], int n)
{
Arrays.sort(arr);
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (arr[i] == arr[i + 1 ])
{
count++;
i++;
}
}
return count / 2 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 4 , 4 , 4 , 4 , 6 , 6 , 6 , 7 , 7 , 9 , 9 };
int n = arr.length;
System.out.println(rectangleSquare(arr, n));
}
}
|
Python3
def rectangleSquare( arr, n):
arr.sort()
count = 0
k = 0
for i in range (n - 1 ):
if (k = = 1 ):
k = 0
continue
if (arr[i] = = arr[i + 1 ]):
count = count + 1
k = 1
return count / 2
if __name__ = = '__main__' :
arr = [ 2 , 2 , 4 , 4 , 4 , 4 , 6 , 6 , 6 , 7 , 7 , 9 , 9 ]
n = len (arr)
print (rectangleSquare(arr, n))
|
C#
using System;
class GFG
{
static int rectangleSquare( int []arr, int n)
{
Array.Sort(arr);
int count = 0;
for ( int i = 0; i < n - 1; i++)
{
if (arr[i] == arr[i + 1])
{
count++;
i++;
}
}
return count / 2;
}
public static void Main(String[] args)
{
int []arr = {2, 2, 4, 4, 4, 4, 6,
6, 6, 7, 7, 9, 9};
int n = arr.Length;
Console.WriteLine(rectangleSquare(arr, n));
}
}
|
PHP
<?php
function rectangleSquare( $arr , $n )
{
sort( $arr );
$count = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $arr [ $i ] == $arr [ $i + 1])
{
$count ++;
$i ++;
}
}
return ( $count / 2);
}
$arr = array (2, 2, 4, 4, 4, 4, 6,
6, 6, 7, 7, 9, 9 );
$n = sizeof( $arr );
echo rectangleSquare( $arr , $n );
?>
|
Javascript
<script>
function rectangleSquare(arr , n)
{
arr.sort();
var count = 0;
for (i = 0; i < n - 1; i++)
{
if (arr[i] == arr[i + 1])
{
count++;
i++;
}
}
return count / 2;
}
var arr = [2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9];
var n = arr.length;
document.write(rectangleSquare(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n*log n) where n is the size of the array.
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Sep, 2022
Like Article
Save Article