Given an integer **N** denoting the length of a given stick, the task is to minimize the time required to split the stick into pieces of unit length, given that, a single cut is possible for any portion of stick at any instance of time.

**Examples:**

Input:N = 100

Output:7

Explanation:

(100 units) —> (2 portions of 50 units) —> (4 portions of 25 units) —> (4 portions of 12 units, 4 portions of 13 units) —> (12 portions of 6 units, 4 portions of 7 units) —> (28 portions of 3 units, 4 portions of 4 units) —> (28 portions of 1 unit, 36 portions of 2 units) —> (100 portions of 1 unit)

Input:N = 65

Output:7

Explanation:

(65 units) —> (1 portion of 32 units, 1 portion of 33 units) —> (3 portions of 16 units, 1 portion of 17 units) —> (7 portions of 8 units, 1 portions of 9 units) —> (15 portions of 4 units, 1 portion of 5 units) —> (31 portions of 2 units, 1 portions of 3 units) —> (63 portions of 1 unit, 1 portion of 2 units) —> (65 portions of 1 unit)

**Approach:**

Since we can cut any portion of the stick once at a particular instance of time, we need to maximize the portions after every cut. So, we will cut the stick into two parts of the longest possible length with the first cut. In the next instance, further cut both the obtained parts into two longest parts each in the next cut. Repeat this until **N** unit pieces are obtained.

Illustration:

N = 100

1st Cut: (50) + (50)

2nd Cut: (25) + (25) + (25) + (25)

3rd Cut: (12) + (13) + (12) + (13) + (12) + (13) + (12) + (13)

4th Cut: (6) + (6) + (6) + (7) + (6) + (6) + (6) + (7) + (6) + (6) + (6) + (7) + (6) + (6) + (6) + (7)

5th Cut: (3) + (3) + (3) + (3) + (3) + (3) + (3) + (4) + (3) + (3) + (3) + (3) + (3) + (3) + (3) + (4) + (3) + (3) + (3) + (3) + (3) + (3) + (3) + (4) + (3) + (3) + (3) + (3) + (3) + (3) + (3) + (4)

6th Cut: 28 portions of 1 unit, 36 portions of 2 units

7th Cut: 100 portions of 1 unit

Hence, the minimum time required to split an N length stick into 1 unit pieces is **ceil(log _{2}N)**.

Below is the implementation of the above approach:

## C++

`// C++ program to find minimum ` `// time required to split a ` `// stick of N length into ` `// unit pieces ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// minimum time required ` `// to split stick of N into ` `// length into unit pieces ` `int` `min_time_to_cut(` `int` `N) ` `{ ` ` ` `if` `(N == 0) ` ` ` `return` `0; ` ` ` `// Return the minimum ` ` ` `// unit of time required ` ` ` `return` `ceil` `(log2(N)); ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 100; ` ` ` `cout << min_time_to_cut(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// time required to split a ` `// stick of N length into ` `// unit pieces ` `import` `java.lang.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the ` `// minimum time required ` `// to split stick of N into ` `// length into unit pieces ` `static` `int` `min_time_to_cut(` `int` `N) ` `{ ` ` ` `if` `(N == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Return the minimum ` ` ` `// unit of time required ` ` ` `return` `(` `int` `)Math.ceil(Math.log(N) / ` ` ` `Math.log(` `2` `)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `100` `; ` ` ` `System.out.print(min_time_to_cut(N)); ` `} ` `} ` ` ` `// This code is contributed by rock_cool ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum ` `# time required to split a stick ` `# of N length into unit pieces ` `import` `math ` ` ` `# Function to return the ` `# minimum time required ` `# to split stick of N into ` `# length into unit pieces ` `def` `min_time_to_cut(N): ` ` ` ` ` `if` `(N ` `=` `=` `0` `): ` ` ` `return` `0` ` ` ` ` `# Return the minimum ` ` ` `# unit of time required ` ` ` `return` `int` `(math.log2(N)) ` `+` `1` ` ` `# Driver Code ` `N ` `=` `100` ` ` `print` `(min_time_to_cut(N)) ` ` ` `# This code is contributed by Vishal Maurya ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// time required to split a ` `// stick of N length into ` `// unit pieces ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to return the ` `// minimum time required ` `// to split stick of N into ` `// length into unit pieces ` `static` `int` `min_time_to_cut(` `int` `N) ` `{ ` ` ` `if` `(N == 0) ` ` ` `return` `0; ` ` ` ` ` `// Return the minimum ` ` ` `// unit of time required ` ` ` `return` `(` `int` `)Math.Ceiling(Math.Log(N) / ` ` ` `Math.Log(2)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 100; ` ` ` `Console.Write(min_time_to_cut(N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O (1)

**Auxiliary Space:** O (1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of cuts required to pay salary from N length Gold Bar
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Number of squares of side length required to cover an N*M rectangle
- Minimize sum of smallest elements from K subsequences of length L
- Minimize length of Substrings containing at least one common Character
- Minimum number of cuts required to make circle segments equal sized
- Count of sticks required to represent the given string
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Program to find Length of Bridge using Speed and Length of Train
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Find the length of the median of a Triangle if length of sides are given
- Length of longest subarray of length at least 2 with maximum GCD
- Minimum cuts required to divide the Circle into equal parts
- Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given
- Number of K length subsequences with minimum sum
- Number of N length sequences whose product is M
- Number of non-decreasing sub-arrays of length K
- Smallest number containing all possible N length permutations using digits 0 to D
- Length of the smallest number which is divisible by K and formed by using 1's only

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.