Open In App

Last Moment Before All Ants Fall Out of a Plank

Last Updated : 27 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a plank of length n with ants moving left and right at 1 unit per second. When ants collide, they reverse their directions and if an ant reaches either end of the plank, it falls off immediately.

Given two integer arrays, left[] and right[] denoting the positions of ants moving left and right respectively, find the time when the last ant falls off the plank.

Examples:

Input: n = 4, left[] = [2], right[] = [0, 1, 3]
Output: 4
Explanation:

last-moment-before-all-ants-fall-out-of-a-plank

Input: n = 4, left[] = [] right[] = [0, 1, 2, 3, 4]
Output: 4
Explanation:

last-moment-before-all-ants-fall-out-of-a-plank-2

Input: n = 3, left[] = [0], right[] = [3]
Output: 0
Explanation: The ants will fall off the plank as they are already on the end of the plank.

Approach:

The idea is based on the observation that whenever two ants collide and reverse their directions, then it is same as if there was no collision and they continue moving in their original paths. This is because both ants move at the same speed and their collision is same as passing through each other.

If one ant is moving left and another is moving right, upon collision, they both switch directions but their distance to the nearest end of the plank remains unchanged. Therefore, the time it takes for each ant to fall off the plank is independent on the collisions. Now, we can simply calculate the time to fall off the plank(without collisions) for each ant and the maximum time among all ants will be the final result.

Case 1: With Collisions


Case 2: Without Collisions


In both the cases, ants take 3 units of time to fall off the plank.

C++
#include <iostream>
#include <vector>
using namespace std;

int getLastMoment(int n, vector<int> &left, 
                  				vector<int> &right) {
	int res = 0;
  
    // Find the time to fall off the plank for all 
    // ants moving towards left
    for(int i = 0; i < left.size(); i++) {
    	res = max(res, left[i]);
    }
  
    // Find the time to fall off the plank for all 
    // ants moving towards right
    for(int i = 0; i < right.size(); i++) {
    	res = max(res, n - right[i]);
    }
    
    // Return the maximum time among all ants
    return res;
}

int main() {
	int n = 4;
    vector<int> left = {2};
    vector<int> right = {0, 1, 3};
    cout << getLastMoment(n, left, right);
}
C
#include <stdio.h>

int getLastMoment(int n, int left[], int l1, int right[], int l2) {
    
    int res = 0;
  
    // Find the time to fall off the plank for all 
    // ants moving towards left
    for(int i = 0; i < l1; i++) {
        if (left[i] > res) {
            res = left[i];
        }
    }
  
    // Find the time to fall off the plank for all 
    // ants moving towards right
    for(int i = 0; i < l2; i++) {
        int time = n - right[i];
        if (time > res) {
            res = time;
        }
    }
    
    // Return the maximum time among all ants
    return res;
}

int main() {
    int n = 4;
    int left[] = {2};
    int right[] = {0, 1, 3};
    int l1 = sizeof(left) / sizeof(left[0]);
    int l2 = sizeof(right) / sizeof(right[0]);
    printf("%d\n", getLastMoment(n, left, l1, right, l2));
    return 0;
}
Java
class GfG {
    static int getLastMoment(int n, int[] left, 
                             			int[] right) {
        int res = 0;

        // Find the time to fall off the plank for all 
        // ants moving towards left
        for (int i = 0; i < left.length; i++) {
            res = Math.max(res, left[i]);
        }

        // Find the time to fall off the plank for all 
        // ants moving towards right
        for (int i = 0; i < right.length; i++) {
            res = Math.max(res, n - right[i]);
        }

        // Return the maximum time among all ants
        return res;
    }

    public static void main(String[] args) {
        int n = 4;
        int[] left = {2};
        int[] right = {0, 1, 3};
        System.out.println(getLastMoment(n, left, right));
    }
}
Python
def getLastMoment(n, left, right):
    res = 0

    # Find the time to fall off the plank for all 
    # ants moving towards left
    for i in range(len(left)):
        res = max(res, left[i])

    # Find the time to fall off the plank for all 
    # ants moving towards right
    for i in range(len(right)):
        res = max(res, n - right[i])

    # Return the maximum time among all ants
    return res

if __name__ == "__main__":
    n = 4
    left = [2]
    right = [0, 1, 3]
    print(getLastMoment(n, left, right))
C#
using System;

class GfG {
    static int getLastMoment(int n, int[] left, 
                             			int[] right) {
        int res = 0;

        // Find the time to fall off the plank for all 
        // ants moving towards left
        for (int i = 0; i < left.Length; i++) {
            res = Math.Max(res, left[i]);
        }

        // Find the time to fall off the plank for all 
        // ants moving towards right
        for (int i = 0; i < right.Length; i++) {
            res = Math.Max(res, n - right[i]);
        }

        // Return the maximum time among all ants
        return res;
    }

    static void Main() {
        int n = 4;
        int[] left = { 2 };
        int[] right = { 0, 1, 3 };
        Console.WriteLine(getLastMoment(n, left, right));
    }
}
JavaScript
function getLastMoment(n, left, right) {
    let res = 0;

    // Find the time to fall off the plank for all 
    // ants moving towards left
    for (let i = 0; i < left.length; i++) {
        res = Math.max(res, left[i]);
    }

    // Find the time to fall off the plank for all 
    // ants moving towards right
    for (let i = 0; i < right.length; i++) {
        res = Math.max(res, n - right[i]);
    }

    // Return the maximum time among all ants
    return res;
}

// Driver Code
const n = 4;
const left = [2];
const right = [0, 1, 3];
console.log(getLastMoment(n, left, right));

Output
4

Time Complexity: O(m), where m is the number of ants on the plank.
Auxiliary Space: O(1), as only a constant amount of extra space is used regardless of input size.


Last Moment Before All Ants Fall Out of a Plank
Visit Course explore course icon
Article Tags :
Practice Tags :

Similar Reads