Betlista's blog

By Betlista, 10 years ago, In English

This is just a note for me, to not "invent" it in next contests, maybe too easy for others.

I have two intervals [f1, t1] and [f2, t2] and I want to find intersection.

private static int[] intersect(int f1, int t1, int f2, int t2) {
    if ( isBetween(f1, f2, t2) ) {
        return new int[] { f1, Math.min(t1, t2) };
    } else if ( isBetween(f2, f1, t1) ) {
        return new int[] { f2, Math.min(t1, t2) };
    } else {
        return null;
    }
}

private static boolean isBetween(int n, int f, int t) {
    return f <= n && n <= t;
}
  • Vote: I like it
  • +8
  • Vote: I do not like it

»
10 years ago, # |
Rev. 2   Vote: I like it +41 Vote: I do not like it
private static int[] intersect(int f1, int t1, int f2, int t2) {
    int f = Math.max(f1, f2), t = Math.min(t1, t2);
    if (f <= t) {
        return new int[]{f, t};
    } else {
        return null;
    }
}