Given **‘N’** number of sticks of length **a _{1}, a_{2}, a_{3}…a_{n}**. 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 2Input: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*

## 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*

**Output:**

3

## Recommended Posts:

- Sort array after converting elements to their squares
- Largest sub-array whose all elements are perfect squares
- Count number of elements between two given elements in array
- Maximum number of contiguous array elements with same number of set bits
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Number of elements that can be seen from right side
- Count number of even and odd elements in an array
- Sum of all the elements in an array divisible by a given number K
- Number of segments where all elements are greater than X
- Find number of Positional Elements
- A permutation where each element indicates either number of elements before or after it
- Count the number of elements in an array which are divisible by k
- Product of all the elements in an array divisible by a given number K
- Minimum number of subsets with distinct elements
- Total number of subsets in which the product of the elements is even

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.