# Determining the inconsistently weighted object

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.

**Examples**:

Input: N = 6 Q = 3 1 2 = 5 6 1 2 3 > 4 5 6 3 4 < 5 6Output: 4Explanation: 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 = 2Output: 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:

`# Python program to determine the ` `# inconsistently weighted object ` ` ` `# Function to get the difference of two lists ` `def` `subt(A, B): ` ` ` `return` `list` `(` `set` `(A) ` `-` `set` `(B)) ` ` ` `# Function to get the intersection of two lists ` `def` `intersection(A, B): ` ` ` `return` `list` `(` `set` `(A).intersection(` `set` `(B))) ` ` ` `# Function to get the intersection of two lists ` `def` `union(A, B): ` ` ` `return` `list` `(` `set` `(A).union(` `set` `(B))) ` ` ` `# Function to find the inconsistently weighted object ` `def` `inconsistentlyWeightedObject(N, Q, comparisons): ` ` ` `# Objects which appear on the heavier side ` ` ` `heavierObj ` `=` `[i ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `)] ` ` ` ` ` `# Objects which appear on the lighter side ` ` ` `lighterObj ` `=` `[i ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `)] ` ` ` `equalObj ` `=` `[] ` `# Objects which appear in '=' comparisons ` ` ` ` ` `# Objects which don't appear in any comparison ` ` ` `objectNotCompared ` `=` `[i ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `)] ` ` ` ` ` `for` `c ` `in` `comparisons: ` ` ` `objectNotCompared ` `=` `subt(objectNotCompared, union(c[` `0` `], c[` `2` `])) ` ` ` ` ` `if` `c[` `1` `] ` `=` `=` `'='` `: ` ` ` `equalObj ` `=` `union(equalObj, union(c[` `0` `], c[` `2` `])) ` ` ` `elif` `c[` `1` `] ` `=` `=` `'<'` `: ` ` ` `# Removing those objects which do ` ` ` `# not appear on the lighter side ` ` ` `lighterObj ` `=` `intersection(lighterObj, c[` `0` `]) ` ` ` ` ` `# Removing thoe objects which do ` ` ` `# not appear on the heavier side ` ` ` `heavierObj ` `=` `intersection(heavierObj, c[` `2` `]) ` ` ` `else` `: ` ` ` `# Removing those objects which do ` ` ` `# not appear on the lighter side ` ` ` `lighterObj ` `=` `intersection(lighterObj, c[` `2` `]) ` ` ` ` ` `# Removing those objects which do ` ` ` `# not appear on the heavier side ` ` ` `heavierObj ` `=` `intersection(heavierObj, c[` `0` `]) ` ` ` ` ` `L_iwo ` `=` `subt(union(heavierObj, lighterObj), equalObj) ` `# Potential candidates ` ` ` ` ` `if` `len` `(L_iwo) ` `=` `=` `1` `: ` ` ` `return` `L_iwo[` `0` `] ` ` ` `elif` `not` `len` `(L_iwo): ` ` ` `if` `len` `(objectNotCompared) ` `=` `=` `1` `: ` ` ` `return` `objectNotCompared[` `0` `] ` ` ` `else` `: ` ` ` `return` `'Insufficient data'` ` ` `else` `: ` ` ` `return` `'Insufficient data'` ` ` ` ` `# Driver code ` `N ` `=` `6` `Q ` `=` `3` `comparisons ` `=` `[ [[` `1` `, ` `2` `], ` `'='` `, [` `5` `, ` `6` `]], [[` `1` `, ` `2` `, ` `3` `], ` `'>'` `, [` `4` `, ` `5` `, ` `6` `]], ` ` ` `[[` `3` `, ` `4` `], ` `'<'` `, [` `5` `, ` `6` `]] ] ` `print` `(inconsistentlyWeightedObject(N, Q, comparisons)) ` |

*chevron_right*

*filter_none*

**Output:**

4

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Program for weighted mean of natural numbers.
- Weighted Sum Method - Multi Criteria Decision Making
- Python | Check if a given object is list or not
- Python | Ways to convert string to json object
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Length of longest subarray whose sum is not divisible by integer K
- Largest subset having with sum less than equal to sum of respective indices
- Create matrix whose sum of diagonals in each sub matrix is even
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root
- Least root of given quadratic equation for value greater than equal to K
- Median of sliding window in an array | Set 2
- Kth diagonal from the top left of a given matrix
- Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference

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.