• + 19 comments

    Hello friends,

    This problem can be solved in O(1) time by using simple mathematics.

    If interested to know more about this algorithm in details-

    click here for the video explanation of generic algorithm with complexity analysis.

    or you can click on the image too to follow youtube tutorial.

    Here is the working solution with O(1) complexity:-

    source code :

    static String kangaroo(int x1, int v1, int x2, int v2) {
    
    		if (v1 > v2) {
    			
    			int remainder = (x1 - x2) % (v2 - v1);
    			
    			if (remainder == 0) {
    				return "YES";
    			}
    		}
    		return "NO";
    
    	}
    

    Would really appreciate your feedback like, dislike , comment etc. on my video.

    Do not forget to upvote, if you find it useful.