We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Number Line Jumps

# Number Line Jumps

+ 0 comments **Solution in C**char* kangaroo(int x1, int v1, int x2, int v2) { char *s = malloc (10 * sizeof(char)); int relativeSpeed; //if speed of kangaroo 1 is slower or equal to kangaroo 2, they will never meet if (v1 <= v2) { s = "NO"; } else { //finding relative speed relativeSpeed = x2 - x1; //If relativeSpeed % (v1 - v2) == 0, then the kangaroos will meet at some future time. if (relativeSpeed % (v1 - v2) == 0) { s = "YES"; } else { s = "NO"; } } return s; free(s); }

+ 0 comments forget about 'for', 'while'..., actually it is a math problem.

+ 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"; }

+ 0 comments def kangaroo(x1, v1, x2, v2): isTrue = False limit,K1,K2 =0,0,0 K1 = (x1+v1) K2 = (x2+v2) while True: if K1 == K2: isTrue = True break K1 +=v1 K2 +=v2 if limit==10000: break limit +=1 if isTrue: return "YES" else: return "NO"

+ 0 comments Python 3

def kangaroo(x1, v1, x2, v2): if v1 == v2: return 'NO' else: meet_jump_n = (x2 - x1)/(v1 - v2) if (meet_jump_n % 1 == 0) & (meet_jump_n > 0): return 'YES' else: return 'NO'

Load more conversations

Sort 3510 Discussions, By:

Please Login in order to post a comment