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 = 5
Output: 2
Explanation: 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 = 9
Output: 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int solve( int N)
{
if (N == 0)
return 0;
if (N == 1)
return 0;
float rec = N;
return (solve( ceil (rec / 3.0)) + 1);
}
int main()
{
int N = 5;
cout << solve(N);
return 0;
}
|
Java
class GFG
{
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 );
}
public static void main(String args[]) {
int N = 5 ;
System.out.println(solve(N));
}
}
|
Python3
import math;
def solve(N):
if (N = = 0 ):
return 0 ;
if (N = = 1 ):
return 0 ;
rec = N;
return (solve(math.ceil(rec / 3.0 )) + 1 );
N = 5 ;
print (solve(N));
|
Javascript
<script>
const solve = (N) => {
if (N == 0)
return 0;
if (N == 1)
return 0;
let rec = N;
return (solve(Math.ceil(rec / 3.0)) + 1);
}
let N = 5;
document.write(solve(N))
</script>
|
C#
using System;
public class GFG {
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);
}
static public void Main()
{
int N = 5;
Console.Write(solve(N));
}
}
|
Time Complexity: O(log(N))
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int solve( int N)
{
int mini = ceil ( log (N) / log (3));
return mini;
}
int main()
{
int N = 5;
cout << solve(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int solve( int N)
{
int mini = ( int )Math.ceil(Math.log(N) / Math.log( 3 ));
return mini;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.print(solve(N));
}
}
|
Python
import math
def solve(N):
mini = math.ceil(math.log(N) / math.log( 3 ))
return mini
N = 5
print (solve(N))
|
C#
using System;
public class GFG{
static int solve( int N)
{
int mini = ( int )Math.Ceiling(Math.Log(N) / Math.Log(3));
return mini;
}
public static void Main(String[] args)
{
int N = 5;
Console.Write(solve(N));
}
}
|
Javascript
<script>
function solve(N)
{
let mini = Math.ceil(Math.log(N) / Math.log(3));
return mini;
}
let N = 5;
document.write(solve(N))
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
21 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...