713143. Last modified ball

Easy

Last Modified Ball

Summary

Given a sequence of balls, each with its own modification time, find the ball that has been modified most recently. In this problem, we are asked to identify the last modified ball in a given sequence.

Detailed Explanation

To solve this problem, we can iterate through the sequence and keep track of the maximum modification time seen so far. When we encounter a new ball with a modification time greater than our current maximum, we update our maximum.

Here's a step-by-step breakdown:

  1. Initialize max_time to 0.
  2. Iterate through the sequence of balls:
    • For each ball, check if its modification time is greater than max_time.
    • If it is, update max_time to be the current ball's modification time.
  3. The last modified ball is the one with the maximum modification time.

Time complexity: O(n), where n is the number of balls in the sequence. We need to iterate through each ball once.

Space complexity: O(1), as we only use a constant amount of space to store max_time.

Optimized Solutions

Java

View on GeeksforGeeks
public class LastModifiedBall {
    public static Ball findLastModified(Ball[] balls) {
        int maxTime = 0;
        Ball lastModified = null;
        
        for (Ball ball : balls) {
            if (ball.getModificationTime() > maxTime) {
                maxTime = ball.getModificationTime();
                lastModified = ball;
            }
        }
        
        return lastModified;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.