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++
// 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; } |
Java
// 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.. |
Python 3
# 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 |
C#
// 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 |
PHP
<?php // PHP implementation of above approach // Recursive function to return // gcd of a and b function __gcd( $a , $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 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. ?> |
Javascript
<script> // Javascript implementation of above approach // Recursive function to return gcd of a and b function __gcd(a, 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 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]); } // Since last part is connected with the first factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]); var cuts = (360 / factor) - N; return cuts; } // Driver code var Arr = [ 0, 1 ]; var N = Arr.length; document.write( Parts(Arr, N)); </script> |
358
Time Complexity: O(N * log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!