Possible number of Rectangle and Squares with the given set of elements - GeeksforGeeks

23 Mar, 2021
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.
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to find the possible// rectangles and squaresint 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 codeint 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;}
// Java implementation of above approachimport 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
# Python3 implementation of above approach  # Function to find the possible# rectangles and squaresdef 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
// C# implementation of above approachusing 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
<?php// PHP implementation of above approach // Function to find the possible// rectangles and squaresfunction 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.?>
<script> // javascript implementation of above approach   // Function to find the possible// rectangles and squaresfunction rectangleSquare(arr , n){     // sort all the sticks    arr.sort();    var 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 lengthsvar arr = [2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9];var n = arr.length;document.write(rectangleSquare(arr, n)); // This code is contributed by 29AjayKumar </script>
3
 
ash264
princiraj1992
Rajput-Ji
Sach_Code
29AjayKumar
Sorting Quiz
Technical Scripter 2018
Arrays
C++ Programs
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Trapping Rain Water
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Window Sliding Technique
Find duplicates in O(n) time and O(1) extra space | Set 1
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
C++ program for hashing with chaining
delete keyword in C++