2481. Minimum Cuts to Divide a Circle LeetCode Solution

In this guide, you will get 2481. Minimum Cuts to Divide a Circle LeetCode Solution with the best time and space complexity. The solution to Minimum Cuts to Divide a Circle problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Cuts to Divide a Circle solution in C++
  4. Minimum Cuts to Divide a Circle solution in Java
  5. Minimum Cuts to Divide a Circle solution in Python
  6. Additional Resources
2481. Minimum Cuts to Divide a Circle LeetCode Solution image

Problem Statement of Minimum Cuts to Divide a Circle

A valid cut in a circle can be:

A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or
A cut that is represented by a straight line that touches one point on the edge of the circle and its center.

Some valid and invalid cuts are shown in the figures below.

Given the integer n, return the minimum number of cuts needed to divide a circle into n equal slices.

Example 1:

Input: n = 4
Output: 2
Explanation:
The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.

Example 2:

Input: n = 3
Output: 3
Explanation:
At least 3 cuts are needed to divide the circle into 3 equal slices.
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
Also note that the first cut will not divide the circle into distinct parts.

See also  279. Perfect Squares LeetCode Solution

Constraints:

1 <= n <= 100

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2481. Minimum Cuts to Divide a Circle LeetCode Solution in C++

class Solution {
 public:
  int numberOfCuts(int n) {
    if (n == 1)
      return 0;
    return n % 2 == 0 ? n / 2 : n;
  }
};
/* code provided by PROGIEZ */

2481. Minimum Cuts to Divide a Circle LeetCode Solution in Java

class Solution {
  public int numberOfCuts(int n) {
    if (n == 1)
      return 0;
    return n % 2 == 0 ? n / 2 : n;
  }
}
// code provided by PROGIEZ

2481. Minimum Cuts to Divide a Circle LeetCode Solution in Python

class Solution:
  def numberOfCuts(self, n: int) -> int:
    if n == 1:
      return 0
    return n // 2 if n % 2 == 0 else n
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.