Repository: TheAlgorithms/C-Plus-Plus
File: greedy_algorithms/jump_game.cpp (C++)

Code before:
 *
 * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000)
 * @author [David Leal](https://github.com/Panquesito7)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for std::cout
#include <vector>    /// for std::vector

/**
 * @namespace
 * @brief Greedy Algorithms
 */
namespace greedy_algorithms {
/**
 * @brief Checks whether the given element (default is `1`) can jump to the last
 * index.
 * @param nums array of numbers containing the maximum jump (in steps) from that
 * index
 * @returns true  if the index can be reached
 * @returns false if the index can NOT be reached
 */
template <typename T>
bool can_jump(const std::vector<T> &nums) {
    size_t lastPos = nums.size() - 1;
    for (int i = nums.size() - 1; i >= 0; i--) {
        if (i + nums[i] >= lastPos) {
            lastPos = i;
        }
    }
    return lastPos == 0;
}
}  // namespace greedy_algorithms

/**
 * @brief Function to test the above algorithm
 * @returns void
 */
static void test() {
    // 1st test
    std::vector<int> nums = { 4, 3, 1, 0, 5 };
    assert(greedy_algorithms::can_jump(nums) == true);

    // 2nd test
    nums = { 3, 2, 1, 0, 4 };
    assert(greedy_algorithms::can_jump(nums) == false);

    // 3rd test
    nums = { 5, 9, 4, 7, 15, 3 };
    assert(greedy_algorithms::can_jump(nums) == true);


Review comment (suggestion):
You are comparing `size_t` with `int`. In order to fix `-Wsign-compare` and `-Wsign-conversion`, I would suggest:
```suggestion
    for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
```

In general, I would suggest to replace `size_t` with `std::size_t`.

Code after:
 *
 * @author [Rakshaa Viswanathan](https://github.com/rakshaa2000)
 * @author [David Leal](https://github.com/Panquesito7)
 */

#include <cassert>   /// for assert
#include <iostream>  /// for std::cout
#include <vector>    /// for std::vector

/**
 * @namespace
 * @brief Greedy Algorithms
 */
namespace greedy_algorithms {
/**
 * @brief Checks whether the given element (default is `1`) can jump to the last
 * index.
 * @param nums array of numbers containing the maximum jump (in steps) from that
 * index
 * @returns true  if the index can be reached
 * @returns false if the index can NOT be reached
 */
bool can_jump(const std::vector<int> &nums) {
    size_t lastPos = nums.size() - 1;
    for (size_t i = lastPos; i != static_cast<size_t>(-1); i--) {
        if (i + nums[i] >= lastPos) {
            lastPos = i;
        }
    }
    return lastPos == 0;
}
}  // namespace greedy_algorithms

/**
 * @brief Function to test the above algorithm
 * @returns void
 */
static void test() {
    assert(greedy_algorithms::can_jump(std::vector<int>({4, 3, 1, 0, 5})));
    assert(!greedy_algorithms::can_jump(std::vector<int>({3, 2, 1, 0, 4})));
    assert(greedy_algorithms::can_jump(std::vector<int>({5, 9, 4, 7, 15, 3})));
    assert(!greedy_algorithms::can_jump(std::vector<int>({1, 0, 5, 8, 12})));
    assert(greedy_algorithms::can_jump(std::vector<int>({2, 1, 4, 7})));

    std::cout << "All tests have successfully passed!\n";
}

/**
 * @brief Main function
 * @returns 0 on exit
 */