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 Specification:
input1: N-1, number of bowls.
input2: Array of size N-1, storing number of marbles in each bowl.
Output Specification:
Your function should return the number of marbles in the missing bowl.
Example 1:
input1: 5
input2: {1,3,5,9,11}
Output: 7
Explanation:
The total sum of marbles in two bowls should be 12. Hence, the missing bowl is Bowl 4 which must contain 7 marbles.
Example 2:
input1: 5
input2: {2,4,6,10,12}
Output: 8
Explanation:
The total sum of marbles in two bowls should be 14. Hence, the missing bowl is Bowl 4 which must contain 8 marbles.
Solution
public static int missingBowl(int input1, int[] input2) {
// start writing your code here
int sum = input2[0] + input2[input1 - 1];
int missing = 0;
int i = 1;
int j = input1 - 2;
for (int k = 0; k < input1 / 2; k++) {
if (input2[i] + input2[j] != sum) {
// https://sauravhathi.github.io/lpu-cse/
missing = sum - input2[i];
break;
}
i++;
j--;
}
return missing;
// end of code
}
Happy Learning – If you require any further information, feel free to contact me.