Start learning free

LESSON 131 OF 347 · LOOPS & CONDITIONALS

Hurdle Jump in C#: Checking Every Height with foreach

Practice Read the full explanation

Given an array of hurdle heights and a jumper's jump height, determine whether or not the hurdler can clear all the hurdles. If they can, return true; otherwise return false.

A hurdler can clear a hurdle if their jump height is greater than or equal to the hurdle height.

Examples:

hurdleJump([1, 2, 3, 4, 5], 5) ➞ true

hurdleJump([5, 5, 3, 4, 5], 3) ➞ false

hurdleJump([5, 4, 5, 6], 10) ➞ true

hurdleJump([1, 2, 1], 1) ➞ false

Note: Return true for the edge case of an empty array of hurdles. (Zero hurdles means that any jump height can clear them).

STEP 1 OF 3
    Ctrl + Enter to check

    C# · LOOPS & CONDITIONALS

    Hurdle Jump in C#, explained

    Problem overview

    The method CanJump(int[] hurdleHeights, int jumperHeight) returns true if the jumper can clear every hurdle, and false if at least one hurdle is too tall. A hurdle is cleared when the jump height is greater than or equal to the hurdle height, so a jump of 5 clears a hurdle of height 5.

    The examples, written as {hurdles}, jump:

    • {1, 2, 3, 4, 5}, 5 gives true, since no hurdle is taller than 5.
    • {5, 5, 3, 4, 5}, 3 gives false: the first hurdle is 5, which is more than 3.
    • {5, 4, 5, 6}, 10 gives true.
    • {1, 2, 1}, 1 gives false, because of the hurdle of height 2.

    With no hurdles at all, the answer is true: nothing stands in the jumper's way.

    Approach: look for a hurdle that blocks

    "Clears all hurdles" means "no hurdle is too tall". It is easier to search for the opposite, a hurdle that fails, than to confirm that every hurdle passes. As soon as one hurdle is taller than the jump height, the final answer is false, and the rest of the array doesn't matter. If the scan finishes without finding such a hurdle, the answer is true.

    This is the "for all" version of a pattern you may have already used for "does any element match?". There you return true on the first match and false at the end. Here the roles are swapped: return false on the first failure and true at the end.

    Step by step

    1. Loop over the heights. foreach (int height in hurdleHeights) gives each hurdle in turn, with no index needed.
    2. Test for a failure and return at once. A hurdle blocks the jumper when height > jumperHeight. The test is a strict "greater than", because a hurdle of exactly the jump height can still be cleared. Returning false immediately ends the method, so later hurdles are skipped.
    3. Return true after the loop. Reaching it means no hurdle was too tall. An empty array skips the loop entirely and gets this answer, which is what the task asks for.

    The solution in C#

    class Solution {
        public bool CanJump(int[] hurdleHeights, int jumperHeight) {
            foreach (int height in hurdleHeights) {
                if (height > jumperHeight) {
                    return false;
                }
            }
            return true;
        }
    }
    

    Tracing {5, 5, 3, 4, 5} with a jump of 3: the first height is 5, and 5 > 3 is true, so the method returns false before it sees the other four hurdles. Tracing {1, 2, 1} with a jump of 1: 1 > 1 is false, 2 > 1 is true, so it returns false on the second hurdle.

    C# details:

    • The method name is CanJump, in PascalCase, and the return type is bool. Code ported from other languages often uses lowercase names such as canJump, which is a different name in C#.
    • Every path returns. The compiler demands a return value on every route. The final return true; handles the route where the loop completes.
    • The loop variable is read-only. Inside foreach you can read height but not assign to it.

    Complexity

    Time: O(n), where n is the number of hurdles. The worst case, when every hurdle can be cleared, looks at all of them once. When a tall hurdle comes early the method finishes sooner, but the worst case decides the bound.

    Space: O(1). Only the loop variable is used.

    Edge cases and common mistakes

    • Using >= in the test. That treats a hurdle equal to the jump height as a failure. {1, 2, 3, 4, 5} with a jump of 5 would wrongly return false.
    • Using < and returning true inside the loop. That answers after the first hurdle that can be cleared, ignoring the ones after it.
    • Forgetting the empty array. The reference handles it without any extra code. An early if (hurdleHeights.Length == 0) return false; would be wrong.
    • Comparing the wrong direction. jumperHeight > height in the failing test mixes up the roles and answers a different question.
    • A single hurdle. {3} with a jump of 3 gives true; {4} with a jump of 3 gives false.

    Testing

    • {1, 2, 3, 4, 5}, 5 gives true.
    • {5, 5, 3, 4, 5}, 3 gives false.
    • {5, 4, 5, 6}, 10 gives true.
    • {1, 2, 1}, 1 gives false.
    • {}, 0 gives true.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free