From ffb5d7655e5358bf7a5d4c4b17bb8381e36741e5 Mon Sep 17 00:00:00 2001 From: chayan das Date: Fri, 12 Sep 2025 00:00:07 +0530 Subject: [PATCH] Create Minimum Jumps --- Minimum Jumps | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Minimum Jumps diff --git a/Minimum Jumps b/Minimum Jumps new file mode 100644 index 0000000..e04c7b7 --- /dev/null +++ b/Minimum Jumps @@ -0,0 +1,26 @@ +class Solution { + public: + int minJumps(vector& arr) { + int n = arr.size(); + if (n <= 1) return 0; // Already at last index + if (arr[0] == 0) return -1; // Can't move anywhere + + int maxReach = arr[0]; // Farthest index reachable + int steps = arr[0]; // Steps left in current jump + int jumps = 1; // We make the first jump from index 0 + + for (int i = 1; i < n; i++) { + if (i == n - 1) return jumps; // Reached last index + + maxReach = max(maxReach, i + arr[i]); + steps--; + + if (steps == 0) { // Need to make another jump + jumps++; + if (i >= maxReach) return -1; // Can't move further + steps = maxReach - i; + } + } + return -1; // If end is never reached + } +};