Given a division of circle into n pieces as an array of size n. The i-th element of the array denotes the angle of one piece. Our task is to make two continuous parts from these pieces so that the difference between angles of these two parts is minimum.

**Examples :**

Input : arr[] = {90, 90, 90, 90} Output : 0 In this example, we can take 1 and 2 pieces and 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0. Input : arr[] = {170, 30, 150, 10} Output : 0 In this example, we can take 1 and 4, and 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0. Input : arr[] = {100, 100, 160} Output : 40

We can notice that if one of the part is continuous then all the remaining pieces also form a continuous part. If angle of the first part is equal to x then difference between angles of first and second parts is |x – (360 – x)| = |2 * x – 360| = 2 * |x – 180|. So for each possible continuous part we can count it’s angle and update answer.

## C++

`// CPP program to find minimum ` `// difference of angles of two ` `// parts of given circle. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns the minimum difference ` `// of angles. ` `int` `findMinimumAngle(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `l = 0, sum = 0, ans = 360; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// sum of array ` ` ` `sum += arr[i]; ` ` ` ` ` `while` `(sum >= 180) { ` ` ` ` ` `// calculating the difference of ` ` ` `// angles and take minimum of ` ` ` `// previous and newly calculated ` ` ` `ans = min(ans, 2 * ` `abs` `(180 - sum)); ` ` ` `sum -= arr[l]; ` ` ` `l++; ` ` ` `} ` ` ` ` ` `ans = min(ans, 2 * ` `abs` `(180 - sum)); ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 100, 100, 160 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << findMinimumAngle(arr, n) << endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## Java

`// java program to find minimum ` `// difference of angles of two ` `// parts of given circle. ` `import` `java.util.*; ` ` ` `class` `Count{ ` ` ` `public` `static` `int` `findMinimumAngle(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `l = ` `0` `, sum = ` `0` `, ans = ` `360` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `// sum of array ` ` ` `sum += arr[i]; ` ` ` ` ` `while` `(sum >= ` `180` `) ` ` ` `{ ` ` ` ` ` `// calculating the difference of ` ` ` `// angles and take minimum of ` ` ` `// previous and newly calculated ` ` ` `ans = Math.min(ans, ` ` ` `2` `* Math.abs(` `180` `- sum)); ` ` ` `sum -= arr[l]; ` ` ` `l++; ` ` ` `} ` ` ` ` ` `ans = Math.min(ans, ` ` ` `2` `* Math.abs(` `180` `- sum)); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `100` `, ` `100` `, ` `160` `}; ` ` ` `int` `n = ` `3` `; ` ` ` `System.out.print(findMinimumAngle(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# java program to find minimum ` `# difference of angles of two ` `# parts of given circle. ` `import` `math ` ` ` `# function returns the minimum ` `# difference of angles. ` `def` `findMinimumAngle (arr, n): ` ` ` `l ` `=` `0` ` ` `_sum ` `=` `0` ` ` `ans ` `=` `360` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `#sum of array ` ` ` `_sum ` `+` `=` `arr[i] ` ` ` ` ` `while` `_sum >` `=` `180` `: ` ` ` ` ` `# calculating the difference of ` ` ` `# angles and take minimum of ` ` ` `# previous and newly calculated ` ` ` `ans ` `=` `min` `(ans, ` `2` `*` `abs` `(` `180` `-` `_sum)) ` ` ` `_sum ` `-` `=` `arr[l] ` ` ` `l` `+` `=` `1` ` ` `ans ` `=` `min` `(ans, ` `2` `*` `abs` `(` `180` `-` `_sum)) ` ` ` `return` `ans ` ` ` `# driver code ` `arr ` `=` `[` `100` `, ` `100` `, ` `160` `] ` `n ` `=` `len` `(arr) ` `print` `(findMinimumAngle (arr, n)) ` ` ` `# This code is contributed by "Abhishek Sharma 44" ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// difference of angles of two ` `// parts of given circle. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `findMinimumAngle(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `l = 0, sum = 0, ans = 360; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// sum of array ` ` ` `sum += arr[i]; ` ` ` ` ` `while` `(sum >= 180) ` ` ` `{ ` ` ` ` ` `// calculating the difference of ` ` ` `// angles and take minimum of ` ` ` `// previous and newly calculated ` ` ` `ans = Math.Min(ans, ` ` ` `2 * Math.Abs(180 - sum)); ` ` ` `sum -= arr[l]; ` ` ` `l++; ` ` ` `} ` ` ` ` ` `ans = Math.Min(ans, ` ` ` `2 * Math.Abs(180 - sum)); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = { 100, 100, 160 }; ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(findMinimumAngle(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum ` `// difference of angles of two ` `// parts of given circle. ` ` ` `// Returns the minimum difference ` `// of angles. ` `function` `findMinimumAngle(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$l` `= 0; ` `$sum` `= 0; ` `$ans` `= 360; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `// sum of array ` ` ` `$sum` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `while` `(` `$sum` `>= 180) ` ` ` `{ ` ` ` ` ` `// calculating the difference of ` ` ` `// angles and take minimum of ` ` ` `// previous and newly calculated ` ` ` `$ans` `= min(` `$ans` `, 2 * ` ` ` `abs` `(180 - ` `$sum` `)); ` ` ` `$sum` `-= ` `$arr` `[` `$l` `]; ` ` ` `$l` `++; ` ` ` `} ` ` ` ` ` `$ans` `= min(` `$ans` `, 2 * ` `abs` `(180 - ` `$sum` `)); ` ` ` `} ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 100, 100, 160 ); ` `$n` `= sizeof(` `$arr` `); ` `echo` `findMinimumAngle(` `$arr` `, ` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by m_kit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

40

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Program to find the angles of a quadrilateral
- Find all angles of a given triangle
- Minimum cuts required to divide the Circle into equal parts
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Equation of circle when three points on the circle are given
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Find all angles of a triangle in 3D
- Check whether the triangle is valid or not if angles are given
- Check whether Quadrilateral is valid or not if angles are given
- Check if an N-sided Polygon is possible from N given angles
- Check if a circle lies inside another circle or not
- Area of the circle that has a square and a circle inscribed in it
- Sum of internal angles of a Polygon
- Check if a line at 45 degree can divide the plane into two equal weight parts
- Divide a number into two parts such that sum of digits is maximum
- Check if any square (with one colored cell) can be divided into two equal parts
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.