• + 0 comments

    Hey everyone, I've come up with a solution to a kangaroo problem and I'm excited to share it with you all. I would greatly appreciate your feedback on my solution, as I'm always looking to improve and learn from others. Feel free to review and provide your thoughts. Thanks in advance!

    public static String kangaroo(int x1, int v1, int x2, int v2) {
        // Write your code here
            int jumps = 0;
            if(x1 > x2 && v1 < v2){
                while (x1 != x2) {
                    x1 = x1 + v1;
                    x2 = x2 + v2;
                    jumps++;
                    if(x2 > x1)
                        return "NO";
                }
                return "YES";
            }
            else if(x2 > x1 && v2 < v1){
                while (x2 != x1) {
                    x1 = x1 + v1;
                    x2 = x2 + v2;
                    jumps++;
                    if(x1 > x2)
                        return "NO";
                }
                return "YES";
            }
            return "NO";
        }