- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum number of parallelograms that can be made using the given length of line segments in C++
Given the task is to find the maximum number of parallelograms that can be made using given N number of line segments if each line segment can be used at most in one parallelogram.
Let’s now understand what we have to do using an example −
Input − Arr[] = {8, 3, 1, 3, 8, 7, 1, 3, 5, 3}
Output − 2
Explanation − With the above given line segments, the two parallelograms that can be formed are with sides 8, 1, 8, 1 and 3, 3, 3, 3 respectively.
Input − Arr[] = {7, 9, 9, 7}
Output − 1
Approach used in the below program as follows
The maximum number of parallelograms that can be made will be = Parallelograms that can be made with 4 equal or similar sides plus parallelograms that can be made using 2 similar sides.
In function MaxParr(), initialize a variable L = Arr[0] which will be used as the size of the array that will be used to store the frequencies of the line segments.
Loop from i=1 till i<N and check if (Arr[i] > L), and inside the if statement put L=Arr[i]. Outside the loop increase the size of L by 1.
Then initialize the frequency array int Freq[L] = {0}. Loop from i=0 till i<N and increase occurrence of each segment by 1.
The initialize count = 0 of type int to store the final count of parallelograms.
Loop from i=0 till i<L and check for parallelograms that can be made with 4 similar sides and if found then increase the value of count accordingly.
Initialize left=0 of type int to store the number of parallelograms that can be formed using 2 similar sides.
Finally loop from i=0 till i<L and check if(Freq[i] >= 2) and if so then add 1 to left.
Put count+= left/2 and return count.
Example
#include <bits/stdc++.h> using namespace std; int MaxParr(int N, int Arr[]){ //Finding length of frequency array int L = Arr[0]; for (int i = 1; i < N; i++){ if (Arr[i] > L) L = Arr[i]; } L = L + 1; int Freq[L] = {0}; for (int i = 0; i < N; i++){ //Increasing occurrence of each line segment Freq[Arr[i]] += 1; } // To store the number of parallelograms int count = 0; for (int i = 0; i < L; i++){ /*parallelograms that can be made using 4 same sides*/ count += int(Freq[i] / 4); Freq[i] = Freq[i] % 4; } int left = 0; for (int i = 0; i < L; i++){ //Counting segments with 2 or more occurrences left if (Freq[i] >= 2) left += 1; } /*Adding parallelograms that can be formed using using 2 similar sides into the final count*/ count += left / 2; return count; } int main(){ int N = 10; int Arr[] = { 8, 3, 1, 3, 8, 7, 1, 3, 5, 3}; cout<< MaxParr(N, Arr); }
Output
If we run the above code we will get the following output −
2