
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Possible number of Rectangle and Squares with the given set of elements in C++
In this problem, we are given an array of N integers denoting the length of n sticks. Our task is to print the count of rectangles and squares that can be created from the sticks of the given length.
Let’s take an example to understand the problem
Input − array = {5, 5, 7, 7, 1, 4}
Output − 1
Explanation − a rectangle with sides 5 5 7 7.
To solve this problem, we will have to check if rectangles and squares are possible or not.
Now, for creating a square or rectangle, there should be two sticks of the same length, 2 for rectangle and 4 for square. Now, in our array, we will have to check for pairs of sticks of the same length. For making this search easy, we will sort the array and then find pairs and half of the total pair count will be the number of squares or rectangles that can be created.
Example
Program to show the implementation of our solution,
#include <bits/stdc++.h> using namespace std; int countRecSqr(int sticks[], int n) { sort(sticks, sticks + n); int pairs = 0; for (int i = 0; i < n - 1; i++) { if (sticks[i]==sticks[i + 1]) { pairs++; i++; } } return pairs / 2; } int main() { int sticks[] = { 2, 2, 4, 4, 4, 4, 6, 6, 6, 7, 7, 9, 9 }; int n = sizeof(sticks) / sizeof(sticks[0]); cout<<"The total number of squares or rectangles that can be created is "; cout<<countRecSqr(sticks, n); return 0; }
Output
The total number of squares or rectangles that can be created is 3
- Related Articles
- Maximum area of rectangle possible with given perimeter in C++
- Count number of squares in a rectangle in C++
- Count the number of rhombi possible inside a rectangle of given size in C++
- Find minimum area of rectangle with given set of coordinates in C++
- Tiling a Rectangle with the Fewest Squares in C++
- Maximum number of contiguous array elements with same number of set bits in C++
- Count number of right triangles possible with a given perimeter in C++
- See the figure and find the ratio of(a) Number of triangles to the number of circles inside the rectangle.(b) Number of squares to all the figures inside the rectangle.(c) Number of circles to all the figures inside the rectangle."
- Number of indexes with equal elements in given range in C++
- Find minimum possible size of array with given rules for removing elements in C++
- Check if is possible to get given sum from a given set of elements in Python
- Count of matrices (of different orders) with given number of elements in C++
- Count occurrences of the average of array elements with a given number in C++
- Rectangle with minimum possible difference between the length and the width in C++
- Count subarrays with equal number of occurrences of two given elements in C++
