Program to find smallest difference of angles of two parts of a given circle in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find smallest difference of angles of two parts of a given circle.

For this we will be provided with the circle divided in n-parts. Our task is to combine these parts into two parts such that the difference of the angles made by those two pieces is minimum.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//returning the minimum difference
int findMinimumAngle(int arr[], int n) {
   int l = 0, sum = 0, ans = 360;
   for (int i = 0; i < n; i++) {
      sum += arr[i];
      while (sum >= 180) {
         ans = min(ans, 2 * abs(180 - sum));
         sum -= arr[l];
         l++;
      }
      ans = min(ans, 2 * abs(180 - sum));
   }
   return ans;
}
int main() {
   int arr[] = { 100, 100, 160 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << findMinimumAngle(arr, n) << endl;
   return 0;
}

Output

40
raja
Published on 19-May-2020 15:30:38
Advertisements