- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Program to find smallest difference of angles of two parts of a given circle in C++
In this problem, we are given an array that denotes the piece of a circle based on the angles of a circle. Our task is to create a program to find smallest difference of angles of two parts of a given circle in C++.
Problem Description − We are given the angles of all pieces of the circle in the array. We need to join the piece in such a way that the two pieces made have the smallest difference of angle.
Let’s take an example to understand the problem
Input
ang[] = {90, 45, 90, 135}
Output
90
Explanation
Taking 1st and 2nd together i.e. 90 + 45 = 135.
Taking 3rd and 4th together i.e. 90 + 135 = 225
Difference = 225 - 135 = 90
Solution Approach
Here, we have to merge all parts to make two parts. And for that, we need to take continuous parts (in the example we can’t take ang1 and ang3 together).
Let’s take the angle of part1 be A.
Then the angle of part2 will be 360 - A.
The difference will be |A - (360 - A)|. We have taken absolute value as there can only be positive angles.
Solving the difference equation,
2 * |A - 180|, this needs to be the minimum value. And for that, we will try to merge all possible parts of the circle and find the minimum value for (2* |A - 180|).
Program to illustrate the working of our solution
Example
#include <iostream> #include <math.h> using namespace std; int CalcSmallDiffAng(int ang[], int n) { int Left = 0, A = 0, minDiff = 360; for (int i = 0; i < n; i++) { A += ang[i]; while (A >= 180) { minDiff = min(minDiff, 2 * abs(180 - A)); A -= ang[Left]; Left++; } minDiff = min(minDiff, 2 * abs(180 - A)); } return minDiff; } int main() { int ang[] = { 90, 45, 90, 135 }; int n = sizeof(ang) / sizeof(ang[0]); cout<<"The smallest difference of angles of two parts of a given circle is "<<CalcSmallDiffAng(ang, n); return 0; }
Output
The smallest difference of angles of two parts of a given circle is 90
- Related Articles
- C++ program to find minimum possible difference of largest and smallest of crackers
- Program to find Circumference of a Circle in C++
- Program to find the angles of a quadrilateral in C++
- Find smallest permutation of given number in C++
- C++ Program to Find Second Smallest of n Elements with Given Complexity Constraint
- C++ Program to find the smallest digit in a given number
- C Program for Program to find the area of a circle?
- C program to find sum and difference of two numbers
- The difference between the measures of the two angles of a complementary pair is 40 degree. Find the measures of the two angles
- C++ Program to find maximum possible smallest time gap between two pair of clock readings
- Divide number into two parts divisible by given numbers in C++ Program
- Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points in C++
- Java Program to find all angles of a triangle
- Find smallest number with given number of digits and sum of digits in C++
- Program to find union of two given linked lists in Python
