Given N objects numbered from 1 to N out of which all are of the same weights except only one object which is not known beforehand. We are also given Q comparisons, in each of which an equal number of objects are placed on both sides of a balance scale, and we are told the heavier side.
The task is to find the inconsistently weighted object or determine if the data is not sufficient enough.
Input : N = 6 Q = 3 1 2 = 5 6 1 2 3 > 4 5 6 3 4 < 5 6 Output : 4 Explanation: Object 4 is lighter than all other objects. Input : N = 10 Q = 4 1 2 3 4 < 7 8 9 10 1 = 9 2 3 4 > 1 5 10 6 = 2 Output : Insufficient data
It is told that except only one element, the rest of the elements are of the same weights. So, if we observe carefully, it can be said that:
- In a ‘=’ comparison, none of the objects on both sides is the inconsistently weighted one.
- If an object appears on the heavier side in one comparison and on the lighter side in another, then it is not the inconsistently weighted object. This is because, if an object appears on the heavier side then it is of the maximum weight and if it appears on the lighter side then it is of the minimum weight. Since a single element can’t be both maximum and minimum at the same time. So, this case will never occur.
- The inconsistently weighted object must appear in all of the non-balanced (‘>’ or ‘<') comparisons.
We can use the above three observations to narrow down the potential candidates for the inconsistently weighted object. We will consider only those objects which are either on the heavier side or the lighter side; if there is only one such object then it is the required one. If there is no such object, then we will consider all those objects which do not appear in any comparison. If there is only one such object then it is the inconsistently weighted object. If none of these scenarios arises, the data is insufficient.
Below is the implementation of the above approach:
- Program for weighted mean of natural numbers.
- Python | Check if a given object is list or not
- Python | Ways to convert string to json object
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find an array of size N having exactly K subarrays with sum S
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum number of cuts required to pay salary from N length Gold Bar
- Sum of Maximum and Minimum prime factor of every number in the Array
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Python - Summation after elements removal
- Python | Custom Index Range Summation
- Pair of integers (a, b) which satisfy the given equations
- Python | Elements with Frequency equal K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.