Possible number of Rectangle and Squares with the given set of elements
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++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find the possible // rectangles and squares int rectangleSquare(int arr[], int n) { // sort all the sticks sort(arr, arr + n); int count = 0; // calculate all the pair of // sticks with same length for (int i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { count++; i++; } } // divide the total number of pair // which will be the number of possible // rectangle and square return count / 2; } // Driver code int main() { // initialize all the stick lengths 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; } |
chevron_right
filter_none
Java
// Java implementation of above approach import java.util.Arrays; class GFG { // Function to find the possible // rectangles and squares static int rectangleSquare(int arr[], int n) { // sort all the sticks Arrays.sort(arr); int count = 0; // calculate all the pair of // sticks with same length for (int i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { count++; i++; } } // divide the total number of pair // which will be the number of possible // rectangle and square return count / 2; } // Driver code public static void main(String[] args) { // initialize all the stick lengths 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)); } } // This code is contributed // by PrinciRaj1992 |
chevron_right
filter_none
Python3
# Python3 implementation of above approach # Function to find the possible # rectangles and squares def rectangleSquare( arr, n): # sort all the sticks arr.sort() count = 0 #print(" xx",arr[6]) # calculate all the pair of # sticks with same length 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 # divide the total number of pair # which will be the number of possible # rectangle and square return count/2 # Driver code if __name__=='__main__': # initialize all the stick lengths arr = [2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9] n = len(arr) print(rectangleSquare(arr, n)) # this code is written by ash264 |
chevron_right
filter_none
C#
// C# implementation of above approach using System; class GFG { // Function to find the possible // rectangles and squares static int rectangleSquare(int []arr, int n) { // sort all the sticks Array.Sort(arr); int count = 0; // calculate all the pair of // sticks with same length for (int i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { count++; i++; } } // divide the total number of pair // which will be the number of possible // rectangle and square return count / 2; } // Driver code public static void Main(String[] args) { // initialize all the stick lengths int []arr = {2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9}; int n = arr.Length; Console.WriteLine(rectangleSquare(arr, n)); } } // This code has been contributed // by Rajput-Ji |
chevron_right
filter_none
PHP
<?php // PHP implementation of above approach // Function to find the possible // rectangles and squares function rectangleSquare($arr, $n) { // sort all the sticks sort($arr); $count = 0; // calculate all the pair of // sticks with same length for ($i = 0; $i < $n - 1; $i++) { if ($arr[$i] == $arr[$i + 1]) { $count++; $i++; } } // divide the total number of pair // which will be the number of possible // rectangle and square return ($count / 2); } // Driver code // initialize all the stick lengths $arr = array(2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9 ); $n = sizeof($arr); echo rectangleSquare($arr, $n); // This code is contributed by Sachin. ?> |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Largest sub-array whose all elements are perfect squares
- Sort array after converting elements to their squares
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Count the number of rhombi possible inside a rectangle of given size
- Find the number of p-sided squares in a grid with K blacks painted
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Count number of elements between two given elements in array
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Maximum number of contiguous array elements with same number of set bits
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Number of elements that can be seen from right side
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Maximum sum rectangle in a 2D matrix | DP-27
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.



