Question :
Long Number Possibilities
Samwell laid out N bowls in a straight line and put a few marbles randomly in each bowl
such that:
1. A bowl can never have more than 9 marbles at a time.
2. A bowl can have 0 marbles.
Now, Samwell's friend adds 1 mpre marble to the last bowl. After this addition, all the bowls
must still be aligned with the rules mentioned above. Adding a marble follows the same
rules as of addition with carry-overs.
You are given the initial list of number of marbles in each bowl. Find the position of the bowl
which was last modified. (Assuming indexing starts from 1).
Answers
Answered by
84
Answer:
public static int bowlArrangement(int arr[],int n){
if(arr[n-1]<9){
return n;
}
else{
int index = n;
while(index > 0 && arr[index-1]==9){
index--;
}
return index;
}
}
Explanation:
Answered by
45
The required programme is as follows:
Explanation:
Conditions given:
- max 9 marbles => (n-1 <= 9)
- zero marbles allowed => (n>= 0)
Therefore, the programme:
public static int bowl(int arr[],int n)
{if(arr[n-1] <= 9, n >= 0){return n;}
else{
int index = n;
while(index > 0 && arr[index-1]==9){index--; }
return index;
}
}
Similar questions