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.
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>usingnamespacestd;intgetLastMoment(intn,vector<int>&left,vector<int>&right){intres=0;// Find the time to fall off the plank for all // ants moving towards leftfor(inti=0;i<left.size();i++){res=max(res,left[i]);}// Find the time to fall off the plank for all // ants moving towards rightfor(inti=0;i<right.size();i++){res=max(res,n-right[i]);}// Return the maximum time among all antsreturnres;}intmain(){intn=4;vector<int>left={2};vector<int>right={0,1,3};cout<<getLastMoment(n,left,right);}
C
#include<stdio.h>intgetLastMoment(intn,intleft[],intl1,intright[],intl2){intres=0;// Find the time to fall off the plank for all // ants moving towards leftfor(inti=0;i<l1;i++){if(left[i]>res){res=left[i];}}// Find the time to fall off the plank for all // ants moving towards rightfor(inti=0;i<l2;i++){inttime=n-right[i];if(time>res){res=time;}}// Return the maximum time among all antsreturnres;}intmain(){intn=4;intleft[]={2};intright[]={0,1,3};intl1=sizeof(left)/sizeof(left[0]);intl2=sizeof(right)/sizeof(right[0]);printf("%d\n",getLastMoment(n,left,l1,right,l2));return0;}
Java
classGfG{staticintgetLastMoment(intn,int[]left,int[]right){intres=0;// Find the time to fall off the plank for all // ants moving towards leftfor(inti=0;i<left.length;i++){res=Math.max(res,left[i]);}// Find the time to fall off the plank for all // ants moving towards rightfor(inti=0;i<right.length;i++){res=Math.max(res,n-right[i]);}// Return the maximum time among all antsreturnres;}publicstaticvoidmain(String[]args){intn=4;int[]left={2};int[]right={0,1,3};System.out.println(getLastMoment(n,left,right));}}
Python
defgetLastMoment(n,left,right):res=0# Find the time to fall off the plank for all # ants moving towards leftforiinrange(len(left)):res=max(res,left[i])# Find the time to fall off the plank for all # ants moving towards rightforiinrange(len(right)):res=max(res,n-right[i])# Return the maximum time among all antsreturnresif__name__=="__main__":n=4left=[2]right=[0,1,3]print(getLastMoment(n,left,right))
C#
usingSystem;classGfG{staticintgetLastMoment(intn,int[]left,int[]right){intres=0;// Find the time to fall off the plank for all // ants moving towards leftfor(inti=0;i<left.Length;i++){res=Math.Max(res,left[i]);}// Find the time to fall off the plank for all // ants moving towards rightfor(inti=0;i<right.Length;i++){res=Math.Max(res,n-right[i]);}// Return the maximum time among all antsreturnres;}staticvoidMain(){intn=4;int[]left={2};int[]right={0,1,3};Console.WriteLine(getLastMoment(n,left,right));}}
JavaScript
functiongetLastMoment(n,left,right){letres=0;// Find the time to fall off the plank for all // ants moving towards leftfor(leti=0;i<left.length;i++){res=Math.max(res,left[i]);}// Find the time to fall off the plank for all // ants moving towards rightfor(leti=0;i<right.length;i++){res=Math.max(res,n-right[i]);}// Return the maximum time among all antsreturnres;}// Driver Codeconstn=4;constleft=[2];constright=[0,1,3];console.log(getLastMoment(n,left,right));
Output
4
Time Complexity: O(m), where mis 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.