Minimum cuts required to divide the Circle into equal parts
Last Updated :
11 Jul, 2022
Given an array arr which represents the different angles at which a circle is cut, the task is to determine the minimum number of more cuts required so that the circle is divided into equal parts.
Note: The array is already sorted in ascending order.
Examples:
Input: arr[] = {0, 90, 180, 270}
Output: 0
No more cuts are required as the circle is already divided into four equal parts.
Input: arr[] = {90, 210}
Output: 1
A single cut is required at 330 degree to divide the circle in three equal parts.
Approach: The idea is to calculate the Greatest Common Divisor of all the values obtained with the consecutive difference of two elements in the array in order to find the greatest (to reduce the number of cuts required) possible size for a part the circle can be divided into.
- First store the absolute difference of 1st two values of the array in a variable named factor = arr[1] – arr[0].
- Now traverse the array from index 2 to N-1 and for every element update factor as factor = gcd(factor, arr[i] – arr[i-1]).
- Then for the last element update factor = gcd(factor, 360 – arr[N-1] + arr[0]).
- Finally, the total cuts required will be (360 / factor) – N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Parts( int Arr[], int N)
{
int factor = Arr[1] - Arr[0];
for ( int i = 2; i < N; i++) {
factor = __gcd(factor, Arr[i] - Arr[i - 1]);
}
factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
int cuts = (360 / factor) - N;
return cuts;
}
int main()
{
int Arr[] = { 0, 1 };
int N = sizeof (Arr) / sizeof (Arr[0]);
cout << Parts(Arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int Parts( int Arr[], int N)
{
int factor = Arr[ 1 ] - Arr[ 0 ];
for ( int i = 2 ; i < N; i++) {
factor = __gcd(factor, Arr[i] - Arr[i - 1 ]);
}
factor = __gcd(factor, 360 - Arr[N - 1 ] + Arr[ 0 ]);
int cuts = ( 360 / factor) - N;
return cuts;
}
public static void main (String[] args) {
int Arr[] = { 0 , 1 };
int N = Arr.length;
System.out.println( Parts(Arr, N));
}
}
|
Python 3
import math
def Parts(Arr, N):
factor = Arr[ 1 ] - Arr[ 0 ]
for i in range ( 2 , N) :
factor = math.gcd(factor, Arr[i] -
Arr[i - 1 ])
factor = math.gcd(factor, 360 -
Arr[N - 1 ] + Arr[ 0 ])
cuts = ( 360 / / factor) - N
return cuts
if __name__ = = "__main__" :
Arr = [ 0 , 1 ]
N = len (Arr)
print ( Parts(Arr, N))
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int Parts( int []Arr, int N)
{
int factor = Arr[1] - Arr[0];
for ( int i = 2; i < N; i++) {
factor = __gcd(factor, Arr[i] - Arr[i - 1]);
}
factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
int cuts = (360 / factor) - N;
return cuts;
}
static void Main()
{
int []Arr = { 0, 1 };
int N = Arr.Length;
Console.WriteLine(Parts(Arr, N));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function Parts( $Arr , $N )
{
$factor = $Arr [1] - $Arr [0];
for ( $i = 2; $i < $N ; $i ++)
{
$factor = __gcd( $factor , $Arr [ $i ] -
$Arr [ $i - 1]);
}
$factor = __gcd( $factor , 360 -
$Arr [ $N - 1] + $Arr [0]);
$cuts = (360 / $factor ) - $N ;
return $cuts ;
}
$Arr = array ( 0, 1 );
$N = sizeof( $Arr );
echo (Parts( $Arr , $N ));
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
function Parts(Arr, N)
{
var factor = Arr[1] - Arr[0];
for ( var i = 2; i < N; i++) {
factor = __gcd(factor, Arr[i] - Arr[i - 1]);
}
factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
var cuts = (360 / factor) - N;
return cuts;
}
var Arr = [ 0, 1 ];
var N = Arr.length;
document.write( Parts(Arr, N));
</script>
|
Time Complexity: O(N * log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))
Share your thoughts in the comments
Please Login to comment...