Program to find smallest difference of angles of two parts of a given circle
Last Updated :
17 Feb, 2023
Given a division of a circle into n pieces as an array of size n. The ith 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 parts 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 its angle and update the answer.
C++
#include <bits/stdc++.h>
using namespace std;
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;
}
|
Java
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 += arr[i];
while (sum >= 180 )
{
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));
}
}
|
Python3
import math
def findMinimumAngle (arr, n):
l = 0
_sum = 0
ans = 360
for i in range (n):
_sum + = arr[i]
while _sum > = 180 :
ans = min (ans, 2 * abs ( 180 - _sum))
_sum - = arr[l]
l + = 1
ans = min (ans, 2 * abs ( 180 - _sum))
return ans
arr = [ 100 , 100 , 160 ]
n = len (arr)
print (findMinimumAngle (arr, n))
|
C#
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 += arr[i];
while (sum >= 180)
{
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()
{
int []arr = { 100, 100, 160 };
int n = 3;
Console.WriteLine(findMinimumAngle(arr, n));
}
}
|
PHP
<?php
function findMinimumAngle( $arr , $n )
{
$l = 0; $sum = 0; $ans = 360;
for ( $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 ;
}
$arr = array ( 100, 100, 160 );
$n = sizeof( $arr );
echo findMinimumAngle( $arr , $n ), "\n" ;
?>
|
Javascript
<script>
let MAXN = 109;
function findMinimumAngle(arr, n)
{
let l = 0, sum = 0, ans = 360;
for (let i = 0; i < n; i++)
{
sum += arr[i];
while (sum >= 180)
{
ans = Math.min(ans,
2 * Math.abs(180 - sum));
sum -= arr[l];
l++;
}
ans = Math.min(ans,
2 * Math.abs(180 - sum));
}
return ans;
}
let arr = [ 100, 100, 160 ];
let n = 3;
document.write(findMinimumAngle(arr, n));
</script>
|
Output :
40
Time Complexity: O(n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...