Missing Bowl - Coding Question Solution Explained!
Missing Bowl
There are N bowls arranged on a table in a row. Each bowl contains some marbles in such a way that the sum of the number of marbles in the first and the last bowl is equal to the sum of the number of marbles in the second and the second last bowl, and so forth. The bowls are kept in the increasing order of the number of marbles in it.
After some time, it is found that there are only N-1 bowls. The first and the last bowls are at their positions, but one of the bowls in between has gone missing.
Find the number of marbles in the missing bowl, given that N%2==0.
Input:
input1: 5
input2:{1,3,5,9,11}
input1 = No. of bowls in tables which is N
input2 = No. of marbles in the bowls. which is an array.
Simple Explanation:
the sum of the number of marbles in the first and the last bowl is equal to the sum of the number of marbles in the second and the second last bowl, and so forth. which is1+11 = 3+9 = 5+missing = 12
we have to find missing?
Solution
class Main {
public static void main(String[] args) {
int N =5; //no of bowls
int[] bowls = {1,3,5,9,11}; //no of marbles in that bowls
System.out.println(MissingMarbles(N,bowls));
}
public static int MissingMarbles(int number, int[] bowls)
{
int sum = bowls[0]+bowls[number-1]; // find the sum =12
int missing = 0;
int i = 1, j = number - 2;
while (i <= j){
if (bowls[i] + bowls[j] == sum)
{
i++;
j--;
}else{
if (i == j)
missing = sum - bowls[i];
else
missing = (sum - (bowls[i] + bowls[j]));
break;
}
}
return missing;
}
}

2 Comments
Thanks alot!!
ReplyDeleteThanks! Kindly Share with friends!
Delete