Open In App

Find the position of the bowl which was last modified

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
// Function to find last modified bowl
int solve(int N, int A[])
{
    for (int i = N - 1; i >= 0; i--) {
        if (A[i] != 9) {
 
            // Return the index of the
            // last non-9 element in A
            return i + 1;
        }
    }
 
    // If all elements in A are 9,
    // return 0
    return 0;
}
 
// Driver code
int main()
{
    int A[] = { 3, 1, 4, 5 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function call
    int result = solve(N, A);
    cout << result << endl;
 
    return 0;
}
 
 
//This code is contribute by Ayush Pathak


Java




// Java Implementation of the approach
import java.util.*;
public class GFG {
 
    // Function to find last modified bowl
    static int solve(int N, int[] A)
    {
        for (int i = N - 1; i >= 0; i--) {
            if (A[i] != 9) {
 
                // Return the index of the
                // last non-9 element in A
                return i + 1;
            }
        }
 
        // If all elements in A are 9,
        // return 0
        return 0;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int[] A = { 3, 1, 4, 5 };
        int N = A.length;
 
        // Function call
        int result = solve(N, A);
        System.out.println(result);
    }
}


Python3




# Function to find last modified bowl
def solve(N, A):
    for i in range(N-1, -1, -1):
        if A[i] != 9:
            # Return the index of the
            # last non-9 element in A
            return i + 1
 
    # If all elements in A are 9,
    # return 0
    return 0
 
 
# Driver code
if __name__ == "__main__":
    A = [3, 1, 4, 5]
    N = len(A)
 
    # Function call
    result = solve(N, A)
    print(result)
 
# This code is contribute by rutikbhosale


C#




// C# Implementation of the approach
 
using System;
 
public class GFG {
    // Function to find last modified bowl
    public static int Solve(int N, int[] A)
    {
        for (int i = N - 1; i >= 0; i--) {
            if (A[i] != 9) {
                // Return the index of the
                // last non-9 element in A
                return i + 1;
            }
        } // If all elements in A are 9,
        // return 0
        return 0;
    }
 
    // Driver code
    public static void Main()
    {
        int[] A = { 3, 1, 4, 5 };
        int N = A.Length;
 
        // Function call
        int result = Solve(N, A);
        Console.WriteLine(result);
    }
}


Javascript




// JavaScript Implementation of the approach
function solve(N, A) {
    for (let i = N - 1; i >= 0; i--)
    {
        if (A[i] != 9)
        {
         
            // Return the index of the last non-9 element in A
            return i + 1;
        }
    }
     
    // If all elements in A are 9, return 0
    return 0;
}
 
// Driver code
let A = [3, 1, 4, 5];
let N = A.length;
 
// Function call
let result = solve(N, A);
console.log(result);
 
// This code is contributed by lokesh.


Output

4

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads