# Minimum comparisons required to find the only heavier ball from N balls

Given **N** **(N > 1)** number of balls and a weight balancing machine. There are **N-1** balls of the same weight and one ball heavier than the others. The task is to find out the minimum number of times weighing is required to find the heavier ball where any number of balls can be weighted each time.

**Note: **The weight balancing machine can tell relative weights not absolute weights i.e. weight of one ball with respect to another.

**Examples:**

Input:N = 5Output:2Explanation:Divide the balls into 2+2+1 parts. Weigh the groups with 2 balls.

If they weigh the same the other is having a different weight.

Otherwise, take the group with greater weight(as it has the required ball) and

measure them one vs one(2 times measuring case).

Input:N = 9Output:2

**Naive Approach:** The solution is based on the following observation.

- Any number of balls
**(N)**can be divided into**3 groups**having nearly equal number of balls and two of them can be weighted at a time.- Now if
**any one**of them is**heavier**than other then the heavier**ball is in that group**. - Otherwise, it is in the group
**which is not weighted**.- Now again that group can be
**further divided into 3 subgroups**and this goes on.

- Now again that group can be

- Now if
- So this gives a recursive function.

So use a **recursive function** where N is divided by 3 in each step and the recursion is called on ceiling value of N/3 now.

Below is the implementation of the above approach.

## C++

`// C++ code to implement above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Recursive function to find` `// minimum number of weighing required` `int` `solve(` `int` `N)` `{` ` ` `if` `(N == 0)` ` ` `return` `0;` ` ` `if` `(N == 1)` ` ` `return` `0;` ` ` `float` `rec = N;` ` ` `return` `(solve(` `ceil` `(rec / 3.0)) + 1);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `N = 5;` ` ` `cout << solve(N);` ` ` `return` `0;` `}` |

## Java

`// Java code to implement above approach` `class` `GFG` `{` ` ` ` ` `// Recursive function to find` ` ` `// minimum number of weighing required` ` ` `public` `static` `int` `solve(` `int` `N) {` ` ` `if` `(N == ` `0` `)` ` ` `return` `0` `;` ` ` `if` `(N == ` `1` `)` ` ` `return` `0` `;` ` ` `float` `rec = N;` ` ` `return` `(solve((` `int` `)Math.ceil(rec / ` `3.0` `)) + ` `1` `);` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[]) {` ` ` `int` `N = ` `5` `;` ` ` `System.out.println(solve(N));` ` ` `}` `}` `// This code is contributed by gfgking.` |

## Python3

`# Python code to implement above approach` `import` `math;` `# Recursive function to find` `# minimum number of weighing required` `def` `solve(N):` ` ` `if` `(N ` `=` `=` `0` `):` ` ` `return` `0` `;` ` ` `if` `(N ` `=` `=` `1` `):` ` ` `return` `0` `;` ` ` `rec ` `=` `N;` ` ` `return` `(solve(math.ceil(rec ` `/` `3.0` `)) ` `+` `1` `);` `# Driver code` `N ` `=` `5` `;` `print` `(solve(N));` `# This code is contributed by gfgking.` |

## Javascript

`<script>` ` ` `// JavaScript code to implement above approach` ` ` `// Recursive function to find` ` ` `// minimum number of weighing required` ` ` `const solve = (N) => {` ` ` `if` `(N == 0)` ` ` `return` `0;` ` ` `if` `(N == 1)` ` ` `return` `0;` ` ` `let rec = N;` ` ` `return` `(solve(Math.ceil(rec / 3.0)) + 1);` ` ` `}` ` ` `// Driver code` ` ` `let N = 5;` ` ` `document.write(solve(N))` ` ` `// This code is contributed by rakeshsahni` ` ` `</script>` |

## C#

`using` `System;` `public` `class` `GFG {` ` ` `// Recursive function to find` ` ` `// minimum number of weighing required` ` ` `public` `static` `int` `solve(` `int` `N)` ` ` `{` ` ` `if` `(N == 0)` ` ` `return` `0;` ` ` `if` `(N == 1)` ` ` `return` `0;` ` ` `float` `rec = N;` ` ` `return` `(solve((` `int` `)Math.Ceiling(rec / 3.0)) + 1);` ` ` `}` ` ` `// Driver code` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `// Code` ` ` `int` `N = 5;` ` ` `Console.Write(solve(N));` ` ` `}` `}` `// This code is contributed by Potta lokesh` |

**Output**

2

* Time Complexity:* O(log(N))

*O(1)*

**Auxiliary Space:**

**Efficient Approach:** From the above approach it can be clearly seen that it is similar to finding the **exponent of 3** which is **equal to or greater than N**. Which can be calculated using **logarithm**.

## C++

`// C++ code to implement above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Functions to find the minimum` `// number of weighing required` `int` `solve(` `int` `N)` `{` ` ` `int` `mini = ` `ceil` `(` `log` `(N) / ` `log` `(3));` ` ` `return` `mini;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `N = 5;` ` ` `cout << solve(N);` ` ` `return` `0;` `}` |

## Java

`// Java code for the above approach` `import` `java.util.*;` `class` `GFG{` ` ` `// Functions to find the minimum` ` ` `// number of weighing required` ` ` `static` `int` `solve(` `int` `N)` ` ` `{` ` ` `int` `mini = (` `int` `)Math.ceil(Math.log(N) / Math.log(` `3` `));` ` ` `return` `mini;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `5` `;` ` ` `System.out.print(solve(N));` ` ` `}` `}` `// This code is contributed by code_hunt.` |

## Python

`# Python code to implement above approach` `import` `math` `# Functions to find the minimum` `# number of weighing required` `def` `solve(N):` ` ` `mini ` `=` `math.ceil(math.log(N) ` `/` `math.log(` `3` `))` ` ` `return` `mini` `# Driver code` `N ` `=` `5` `print` `(solve(N))` `# This code is contributed by Samim Hossain Mondal.` |

## C#

`// C# code for the above approach` `using` `System;` `public` `class` `GFG{` ` ` `// Functions to find the minimum` ` ` `// number of weighing required` ` ` `static` `int` `solve(` `int` `N)` ` ` `{` ` ` `int` `mini = (` `int` `)Math.Ceiling(Math.Log(N) / Math.Log(3));` ` ` `return` `mini;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String[] args)` ` ` `{` ` ` `int` `N = 5;` ` ` `Console.Write(solve(N));` ` ` `}` `}` `// This code is contributed by shikhasingrajput` |

## Javascript

`<script>` `// Javascript code to implement above approach` `// Functions to find the minimum` `// number of weighing required` `function` `solve(N)` `{` ` ` `let mini = Math.ceil(Math.log(N) / Math.log(3));` ` ` `return` `mini;` `}` `// Driver code` `let N = 5;` `document.write(solve(N))` `// This code is contributed by gfgking.` `</script>` |

**Output**

2

* Time Complexity:* O(1)

*O(1)*

**Auxiliary Space:**