Minimum cuts required to divide the Circle into equal parts

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the number of cuts
// required to divide a circle into equal parts
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]);
    }
  
    // Since last part is connected with the first
    factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
  
    int cuts = (360 / factor) - N;
  
    return cuts;
}
  
// Driver code
int main()
{
    int Arr[] = { 0, 1 };
    int N = sizeof(Arr) / sizeof(Arr[0]);
  
    cout << Parts(Arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
import java.io.*;
  
  
class GFG {
    // Recursive function to return gcd of a and b 
    static int __gcd(int a, int b) 
    
        // Everything divides 0  
        if (a == 0
          return b; 
        if (b == 0
          return a; 
         
        // base case 
        if (a == b) 
            return a; 
         
        // a is greater 
        if (a > b) 
            return __gcd(a-b, b); 
        return __gcd(a, b-a); 
    
       
  
// Function to return the number of cuts
// required to divide a circle into equal parts
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]);
    }
  
    // Since last part is connected with the first
    factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
  
    int cuts = (360 / factor) - N;
  
    return cuts;
}
  
// Driver code
  
    public static void main (String[] args) {
    int Arr[] = { 0, 1 };
    int N = Arr.length;
  
    System.out.println( Parts(Arr, N));
    }
}
// This code is contributed by anuj_67..

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of
# above approach
import math
  
# Function to return the number 
# of cuts required to divide a
# circle into equal parts
def Parts(Arr, N):
  
    factor = Arr[1] - Arr[0]
    for i in range(2, N) :
        factor = math.gcd(factor, Arr[i] - 
                                  Arr[i - 1])
      
    # Since last part is connected
    # with the first
    factor = math.gcd(factor, 360 - 
                      Arr[N - 1] + Arr[0])
  
    cuts = (360 // factor) - N
  
    return cuts
  
# Driver code
if __name__ == "__main__":
    Arr = [ 0, 1 ]
    N = len(Arr) 
  
    print( Parts(Arr, N))
  
# This code is contributed 
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//  C# implementation of above approach
  
using System;
  
class GFG
{
   // Recursive function to return gcd of a and b 
    static int __gcd(int a, int b) 
    
        // Everything divides 0  
        if (a == 0) 
          return b; 
        if (b == 0) 
          return a; 
         
        // base case 
        if (a == b) 
            return a; 
         
        // a is greater 
        if (a > b) 
            return __gcd(a-b, b); 
        return __gcd(a, b-a); 
    
       
  
    // Function to return the number of cuts
    // required to divide a circle into equal parts
    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]);
        }
      
        // Since last part is connected with the first
        factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
      
        int cuts = (360 / factor) - N;
      
        return cuts;
    }
  
    // Driver code
    static void Main()
    {
            int []Arr = { 0, 1 };
            int N = Arr.Length;
            Console.WriteLine(Parts(Arr, N));
    }
}
// This code is contributed by ANKITRAI1

chevron_right


PHP

$b)
return __gcd($a – $b, $b);
return __gcd($a, $b – $a);
}

// Function to return the number of cuts
function Parts($Arr, $N)
{
$factor = $Arr[1] – $Arr[0];
for ($i = 2; $i < $N; $i++) { $factor = __gcd($factor, $Arr[$i] - $Arr[$i - 1]); } // Since last part is connected // with the first $factor = __gcd($factor, 360 - $Arr[$N - 1] + $Arr[0]); $cuts = (360 / $factor) - $N; return $cuts; } // Driver code $Arr = array( 0, 1 ); $N = sizeof($Arr); echo (Parts($Arr, $N)); // This code is contributed by ajit. ?>

Output:

358


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m, AnkitRai01, Ita_c, jit_t