Find the position of the bowl which was last modified
Last Updated :
10 Apr, 2023
Samwell laid out N bowls in a straight line and put a few marbles randomly in each bowl, ith bowl has A[i] marbles. A bowl can never have more than 9 marbles at a time. A bowl can have zero marbles. Now Samwells friend adds one more 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 addition with carryover. You are given the initial list of the number of marbles in each bowl find the position of the bowl which was last modified. It is guaranteed that there is at least one bowl that has at least one space left.
Note: Consider one-based indexing
Examples:
Input: N = 4, A[] = {3, 1, 4, 5}
Output: 4
Explanation: The last bowl has 5 marbles, we can just add the marbles here.
Input: N = 3, A[] = {1, 9, 9}
Output: 1
Explanation: When we add the marble to the last bowl we have to move one marble to 2nd bowl, to add the marble to 2nd bowl we have to move one marble to 1st bowl. Hence the last modified bowl is 1.
Approach: This can be solved with the following idea:
The important finding is that because a bowl can hold no more than nine marbles if the current bowl currently has nine marbles, no additional marbles will be added to it; instead, they will be carried over to the immediately preceding position. The final modified bowl will be the final one of its kind to contain less than nine marbles.
Steps to follow for the above approach:
- Initialize the ans variable to n. Here, ans stands for our response or the location of the latest changed bowl.
- Start iterating from the last place.
- We set the value of ans to I and end the loop if the number of marbles at the ith position is less than 9.
- Provide the answer-back.
Below is the code implementation of the above code:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int solve( int N, int A[])
{
for ( int i = N - 1; i >= 0; i--) {
if (A[i] != 9) {
return i + 1;
}
}
return 0;
}
int main()
{
int A[] = { 3, 1, 4, 5 };
int N = sizeof (A) / sizeof (A[0]);
int result = solve(N, A);
cout << result << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int solve( int N, int [] A)
{
for ( int i = N - 1 ; i >= 0 ; i--) {
if (A[i] != 9 ) {
return i + 1 ;
}
}
return 0 ;
}
public static void main(String[] args)
{
int [] A = { 3 , 1 , 4 , 5 };
int N = A.length;
int result = solve(N, A);
System.out.println(result);
}
}
|
Python3
def solve(N, A):
for i in range (N - 1 , - 1 , - 1 ):
if A[i] ! = 9 :
return i + 1
return 0
if __name__ = = "__main__" :
A = [ 3 , 1 , 4 , 5 ]
N = len (A)
result = solve(N, A)
print (result)
|
C#
using System;
public class GFG {
public static int Solve( int N, int [] A)
{
for ( int i = N - 1; i >= 0; i--) {
if (A[i] != 9) {
return i + 1;
}
}
return 0;
}
public static void Main()
{
int [] A = { 3, 1, 4, 5 };
int N = A.Length;
int result = Solve(N, A);
Console.WriteLine(result);
}
}
|
Javascript
function solve(N, A) {
for (let i = N - 1; i >= 0; i--)
{
if (A[i] != 9)
{
return i + 1;
}
}
return 0;
}
let A = [3, 1, 4, 5];
let N = A.length;
let result = solve(N, A);
console.log(result);
|
Complexity analysis:
The time complexity of this program is O(n), where n is the size of the array A. The program loops through the array from the end until it finds the last non-9 element in A. If all elements in A are 9, it returns 0. The loop runs at most n times, so the time complexity is O(n).
The space complexity of this program is O(1) because it uses a constant amount of additional space, regardless of the size of the input. The program only creates a few integer variables to store the array size, loop index, and result, which are all of constant size.
Share your thoughts in the comments
Please Login to comment...