ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/insertion_sort_recursive.cpp (C++)

Code before:
            arr->at(j + 1) = arr->at(j);
            j--;
        }
        arr->at(j + 1) = temp;
    }
}

}  // namespace sorting

/**
 * @brief Helper function to create a random array
 * 
 * @tparam T Type of the array elements
 * @param arr Array to fill (must be pre-allocated)
 * @param N Number of elements in the array
 */
template <typename T>
static void create_random_array(T *arr, int N) {
    while (N--) {
        double r = (std::rand() % 10000 - 5000) / 100.f;
        arr[N] = static_cast<T>(r);
    }
}

/** 
 * @brief Test Cases for the sorting algorithm 
 */
static void tests() {
    int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
    std::cout << "Test 1... ";
    sorting::insertionSort(arr1, 10);
    assert(std::is_sorted(arr1, arr1 + 10));
    std::cout << "passed" << std::endl;

    int arr2[5] = {5, -3, 7, -2, 1};
    std::cout << "Test 2... ";
    sorting::insertionSort(arr2, 5);
    assert(std::is_sorted(arr2, arr2 + 5));
    std::cout << "passed" << std::endl;

    float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
    std::cout << "Test 3... ";
    sorting::insertionSort(arr3, 5);
    assert(std::is_sorted(arr3, arr3 + 5));
    std::cout << "passed" << std::endl;

    std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
    std::cout << "Test 4... ";
    sorting::insertionSort(&arr4);
    assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
    std::cout << "passed" << std::endl;

Review comment (suggestion):
```suggestion
 * @brief self test implementation
 * @return void
```


Code after:
            arr->at(j + 1) = arr->at(j);
            j--;
        }
        arr->at(j + 1) = temp;
    }
}

}  // namespace sorting

/**
 * @brief Helper function to create a random array
 * 
 * @tparam T Type of the array elements
 * @param arr Array to fill (must be pre-allocated)
 * @param N Number of elements in the array
 */
template <typename T>
static void create_random_array(T *arr, int N) {
    while (N--) {
        double r = (std::rand() % 10000 - 5000) / 100.f;
        arr[N] = static_cast<T>(r);
    }
}

/** 
 * @brief self test implementation
 * @return void
 */
static void tests() {
    int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
    std::cout << "Test 1... ";
    sorting::insertionSort(arr1, 10);
    assert(std::is_sorted(arr1, arr1 + 10));
    std::cout << "passed" << std::endl;

    int arr2[5] = {5, -3, 7, -2, 1};
    std::cout << "Test 2... ";
    sorting::insertionSort(arr2, 5);
    assert(std::is_sorted(arr2, arr2 + 5));
    std::cout << "passed" << std::endl;

    float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
    std::cout << "Test 3... ";
    sorting::insertionSort(arr3, 5);
    assert(std::is_sorted(arr3, arr3 + 5));
    std::cout << "passed" << std::endl;

    std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
    std::cout << "Test 4... ";
    sorting::insertionSort(&arr4);
    assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
Repository: TheAlgorithms/C-Plus-Plus
File: math/gcd_of_n_numbers.cpp (C++)

Code before:
/**
 * @brief Function to check if all elements in array are 0
 * @param a Array of numbers
 * @param n Number of elements in array
 * @return 'True' if all elements are 0
 * @return 'False' if not all elements are 0
 */
bool check_all_zeros(int a[], size_t n) {
    // Check for the undefined GCD cases
    int zero_count = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] == 0) {
            ++zero_count;
        }
    }

    // return whether all elements in array are 0
    return zero_count == n;
}
/** 
 * @brief Main program to compute GCD using division algorithm
 * @param a Array of integers to compute GCD for
 * @param n number of integers in the array
 * @return GCD of the numbers in the array
 */
int gcd(int a[], size_t n) {
    // GCD is undefined if all elements in the array are 0
    if (check_all_zeros(a, n))
        return -1;  // since gcd is positive, use -1 to mark undefined gcd

    int gcd = a[0];
    for(int i = 1; i < n; i++) {
        gcd = gcd_two(gcd, a[i]);
        if (std::abs(gcd) == 1) 
            break;  // if gcd is already 1, further computations still result in gcd of 1
    }

    return std::abs(gcd);  // divisors can be negative, and we only want positive value
}
} // namespace gcd_of_n_numbers
} // namespace math

/**
 * @brief Self-test implementation
 * @return void
 */
static void test() { 
    int array_1[1] = {0};
    int array_2[1] = {1};
    int array_3[2] = {0, 2};
    int array_4[3] = {-60, 24, 18};

Review comment (suggestion):
instead of using a fundamental array
an array from <array>
that is std::array<type>

this lets you remove the size parameter

Code after:
  }
  if (x == 0) {
    return y;
  }
  return gcd_two(y, x % y); // Euclidean method
}

/**
 * @brief Function to check if all elements in the array are 0
 * @param a Array of numbers
 * @return 'True' if all elements are 0
 * @return 'False' if not all elements are 0
 */
template <std::size_t n>
bool check_all_zeros(const std::array<int, n> &a) {
  // Use std::all_of to simplify zero-checking
  return std::all_of(a.begin(), a.end(), [](int x) { return x == 0; });
}

/**
 * @brief Main program to compute GCD using the Euclidean algorithm
 * @param a Array of integers to compute GCD for
 * @return GCD of the numbers in the array or std::nullopt if undefined
 */
template <std::size_t n>
int gcd(const std::array<int, n> &a) {
  // GCD is undefined if all elements in the array are 0
  if (check_all_zeros(a)) {
    return -1; // Use std::optional to represent undefined GCD
  }

  // divisors can be negative, we only want the positive value
  int result = std::abs(a[0]);
  for (std::size_t i = 1; i < n; ++i) {
    result = gcd_two(result, std::abs(a[i]));
    if (result == 1) {
      break; // Further computations still result in gcd of 1
    }
  }
  return result;
}
} // namespace gcd_of_n_numbers
} // namespace math

/**
 * @brief Self-test implementation
 * @return void
 */
static void test() {
  std::array<int, 1> array_1 = {0};
  std::array<int, 1> array_2 = {1};
Repository: TheAlgorithms/C-Plus-Plus
File: search/Longest_Increasing_Subsequence_using_binary_search.cpp (C++)

Code before:

/**
 * @file
 * @brief find the length of the Longest Increasing Subsequence (LIS)
 * using Binary Search(https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
 * @details
 * Given an integer array nums, return the length of the longest strictly
 * increasing subsequence.
 * The longest increasing subsequence is described as a subsequence of an array
 * where: All elements of the subsequence are in increasing order. This subsequence
 * itself is of the longest length possible.

 * For solving this problem we have Three Approaches :-

 * Approach 1 :- Using Brute Force
 * The first approach that came to your mind is the Brute Force approach where we
 * generate all subsequences and then manually filter the subsequences whose
 * elements come in increasing order and then return the longest such subsequence.
 * Time Complexity :- O(2^n)
 * It's time complexity is exponential. Therefore we will try some other
 * approaches.

 * Approach 2 :- Using Dynamic Programming
 * To generate all subsequences we will use recursion and in the recursive logic we
 * will figure out a way to solve this problem. Recursive Logic to solve this
 * problem:-
 * 1. We only consider the element in the subsequence if the element is grater then
 * the last element present in the subsequence
 * 2. When we consider the element we will increase the length of subsequence by 1
 * Time Complexity: O(N*N)

Review comment (suggestion):
```suggestion
 * using [Binary Search](https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
```

Code after:
/**
 * @file
 * @brief find the length of the Longest Increasing Subsequence (LIS)
 * using [Binary Search](https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
 * @details
 * Given an integer array nums, return the length of the longest strictly
 * increasing subsequence.
 * The longest increasing subsequence is described as a subsequence of an array
 * where: All elements of the subsequence are in increasing order. This subsequence
 * itself is of the longest length possible.

 * For solving this problem we have Three Approaches :-

 * Approach 1 :- Using Brute Force
 * The first approach that came to your mind is the Brute Force approach where we
 * generate all subsequences and then manually filter the subsequences whose
 * elements come in increasing order and then return the longest such subsequence.
 * Time Complexity :- O(2^n)
 * It's time complexity is exponential. Therefore we will try some other
 * approaches.

 * Approach 2 :- Using Dynamic Programming
 * To generate all subsequences we will use recursion and in the recursive logic we
 * will figure out a way to solve this problem. Recursive Logic to solve this
 * problem:-
 * 1. We only consider the element in the subsequence if the element is grater then
 * the last element present in the subsequence
 * 2. When we consider the element we will increase the length of subsequence by 1
 * Time Complexity: O(N*N)
 * Space Complexity: O(N*N) + O(N)
Repository: TheAlgorithms/C-Plus-Plus
File: search/Longest_Increasing_Subsequence_using_binary_search.cpp (C++)

Code before:
    assert(longest_increasing_subsequence_using_binary_search(arr2) == 4);

    std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
    assert(longest_increasing_subsequence_using_binary_search(arr3) == 1);

    std::vector<int> arr4 = {-10, -1, -5, 0, 5, 1, 2};
    assert(longest_increasing_subsequence_using_binary_search(arr4) == 5);

    std::vector<double> arr5 = {3.5, 1.2, 2.8, 3.1, 4.0};
    assert(longest_increasing_subsequence_using_binary_search(arr5) == 4);

    std::vector<char> arr6 = {'a', 'b', 'c', 'a', 'd'};
    assert(longest_increasing_subsequence_using_binary_search(arr6) == 4);

    std::vector<int> arr7 = {};
    assert(longest_increasing_subsequence_using_binary_search(arr7) == 0);

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

/**
 * @brief Main function to run tests
 * @returns 0 on exit
 */
int main() {
    tests();
    return 0;
}


Review comment (suggestion):
```suggestion
    tests(); // run self test implementation
```

Code after:

    std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
    assert(longest_increasing_subsequence_using_binary_search(arr3) == 1);

    std::vector<int> arr4 = {-10, -1, -5, 0, 5, 1, 2};
    assert(longest_increasing_subsequence_using_binary_search(arr4) == 5);

    std::vector<double> arr5 = {3.5, 1.2, 2.8, 3.1, 4.0};
    assert(longest_increasing_subsequence_using_binary_search(arr5) == 4);

    std::vector<char> arr6 = {'a', 'b', 'c', 'a', 'd'};
    assert(longest_increasing_subsequence_using_binary_search(arr6) == 4);

    std::vector<int> arr7 = {};
    assert(longest_increasing_subsequence_using_binary_search(arr7) == 0);

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

/**
 * @brief Main function to run tests
 * @returns 0 on exit
 */
int main() {
    tests();  // run self test implementation
    return 0;
}
Repository: TheAlgorithms/C-Plus-Plus
File: math/modular_inverse_fermat_little_theorem.cpp (C++)

Code before:
 */
namespace modular_inverse_fermat {
/**
 * @brief Calculate exponent with modulo using binary exponentiation.
 * @param a The base
 * @param b The exponent
 * @param m The modulo
 * @return The result of \f$a^{b} % m\f$
 */
std::int64_t binExpo(std::int64_t a, std::int64_t b, std::int64_t m) {
  a %= m;
  std::int64_t res = 1;
  while (b > 0) {
    if (b % 2 != 0) {
      res = res * a % m;
    }
    a = a * a % m;
    // Dividing b by 2 is similar to right shift by 1 bit
    b >>= 1;
  }
  return res;
}
/**
 * @brief Check if a given integer is a prime number.
 * @param m An intger to check for primality
 * @return 'true' if the number is prime, otherwise 'false'
 */
bool isPrime(std::int64_t m) {
  if (m <= 1) {
    return false;
  } 
  for (std::int64_t i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      return false;
    }
  }
  return true;
}
/**
 * @brief Main function to calculate the modular inverse.
 * @param a Integer value for the base
 * @param m Integer value for modulo
 * @return The result that is the modular inverse of a modulo m
 */
std::int64_t modular_inverse(std::int64_t a, std::int64_t m) {
  while (a < 0) {
    a += m;
  }

  // Check for invalid cases
  if (!isPrime(m) || a == 0) {

Review comment (suggestion):
```suggestion
 * @return true if the number is prime
 * @return false if the number is not prime
```

Code after:
 */
namespace modular_inverse_fermat {
/**
 * @brief Calculate exponent with modulo using binary exponentiation in \f$O(\log b)\f$ time.
 * @param a The base
 * @param b The exponent
 * @param m The modulo
 * @return The result of \f$a^{b} % m\f$
 */
std::int64_t binExpo(std::int64_t a, std::int64_t b, std::int64_t m) {
  a %= m;
  std::int64_t res = 1;
  while (b > 0) {
    if (b % 2 != 0) {
      res = res * a % m;
    }
    a = a * a % m;
    // Dividing b by 2 is similar to right shift by 1 bit
    b >>= 1;
  }
  return res;
}
/**
 * @brief Check if an integer is a prime number in \f$O(\sqrt{m})\f$ time.
 * @param m An intger to check for primality
 * @return true if the number is prime
 * @return false if the number is not prime
 */
bool isPrime(std::int64_t m) {
  if (m <= 1) {
    return false;
  } 
  for (std::int64_t i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      return false;
    }
  }
  return true;
}
/**
 * @brief calculates the modular inverse.
 * @param a Integer value for the base
 * @param m Integer value for modulo
 * @return The result that is the modular inverse of a modulo m
 */
std::int64_t modular_inverse(std::int64_t a, std::int64_t m) {
  while (a < 0) {
    a += m;
  }

  // Check for invalid cases
Repository: TheAlgorithms/C-Plus-Plus
File: math/modular_inverse_fermat_little_theorem.cpp (C++)

Code before:
      res = res * a % m;
    }
    a = a * a % m;
    // Dividing b by 2 is similar to right shift by 1 bit
    b >>= 1;
  }
  return res;
}
/**
 * @brief Check if a given integer is a prime number.
 * @param m An intger to check for primality
 * @return 'true' if the number is prime, otherwise 'false'
 */
bool isPrime(std::int64_t m) {
  if (m <= 1) {
    return false;
  } 
  for (std::int64_t i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      return false;
    }
  }
  return true;
}
/**
 * @brief Main function to calculate the modular inverse.
 * @param a Integer value for the base
 * @param m Integer value for modulo
 * @return The result that is the modular inverse of a modulo m
 */
std::int64_t modular_inverse(std::int64_t a, std::int64_t m) {
  while (a < 0) {
    a += m;
  }

  // Check for invalid cases
  if (!isPrime(m) || a == 0) {
    return -1; // Invalid input
  }

  return binExpo(a, m - 2, m);  // Fermat's Little Theorem
}
} // namespace modular_inverse_fermat
} // namespace math

/**
 * @brief Self-test implementation
 * @return void
 */
static void test() {
  assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1);

Review comment (suggestion):
```suggestion
 * @brief calculates the modular inverse.
```

Code after:
      res = res * a % m;
    }
    a = a * a % m;
    // Dividing b by 2 is similar to right shift by 1 bit
    b >>= 1;
  }
  return res;
}
/**
 * @brief Check if an integer is a prime number in \f$O(\sqrt{m})\f$ time.
 * @param m An intger to check for primality
 * @return true if the number is prime
 * @return false if the number is not prime
 */
bool isPrime(std::int64_t m) {
  if (m <= 1) {
    return false;
  } 
  for (std::int64_t i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      return false;
    }
  }
  return true;
}
/**
 * @brief calculates the modular inverse.
 * @param a Integer value for the base
 * @param m Integer value for modulo
 * @return The result that is the modular inverse of a modulo m
 */
std::int64_t modular_inverse(std::int64_t a, std::int64_t m) {
  while (a < 0) {
    a += m;
  }

  // Check for invalid cases
  if (!isPrime(m) || a == 0) {
    return -1; // Invalid input
  }

  return binExpo(a, m - 2, m);  // Fermat's Little Theorem
}
} // namespace modular_inverse_fermat
} // namespace math

/**
 * @brief Self-test implementation
 * @return void
 */
static void test() {
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
/**
 * @file
 * @brief [Exponential Distribution] (https://en.wikipedia.org/wiki/Exponential_distribution)
 * 
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 * 
 * P(x,⋋) = ⋋e^(-⋋x)
 * 
 * Summary of variables used:
 * ⋋: rate parameter
 */
#include<cmath>
#include<cassert>
#include<iostream>

/** Find the expected value of the exponential distribution
 * \param [in] lambda
 * \returns \f$\mu=1/lambda\f$
 */
double exponential_expected(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/lambda;
}


Review comment (suggestion):
```suggestion
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
```

Code after:
/**
 * @file
 * @brief [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 *
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 *
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 *
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include <cassert>    // For assert
#include <cmath>      // For std::pow
#include <iostream>   // For I/O operation
#include <stdexcept>  // For std::invalid_argument
#include <string>     // For std::string

/**
 * @namespace probability
 * @brief Probability algorithms
 */
namespace probability {
/**
 * @namespace exponential_dist
 * @brief Functions for the [Exponential
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
    std::cout << "Output : " << exponential_expected(lambda) << std::endl;
    assert(exponential_expected(lambda)==expected);
    std::cout << "TEST PASSED" << std::endl;
    std::cout<<std::endl;

    //Test 2
    std::cout << "Variance" << std::endl;
    std::cout << "Lambda : " << lambda << std::endl;
    std::cout << "Expected Output : " << var <<std::endl;
    std::cout << "Output : " << exponential_var(lambda) << std::endl;
    assert(exponential_var(lambda)==var);
    std::cout << "TEST PASSED" << std::endl;
    std::cout<<std::endl;

    //Test 3
    std::cout << "Standard Deviation" << std::endl;
    std::cout << "Lambda : " << lambda << std::endl;
    std::cout << "Expected Output : " << std <<std::endl;
    std::cout << "Output : " << exponential_std(lambda) << std::endl;
    assert(exponential_std(lambda)==std);
    std::cout << "TEST PASSED" << std::endl;
    std::cout<<std::endl;
}

int main(){
    test(); //Self-implemented test
    return 0;
}

Review comment (suggestion):
```suggestion
    test(); //Self test implementation
```

Code after:
    return 1 / lambda;
}
}  // namespace geometric_dist
}  // namespace probability

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    double lambda_1 = 1;
    double expected_1 = 1;
    double var_1 = 1;
    double std_1 = 1;

    double lambda_2 = 2;
    double expected_2 = 0.5;
    double var_2 = 0.25;
    double std_2 = 0.5;

    double lambda_3 = 3;
    double expected_3 = 0.333333;
    double var_3 = 0.111111;
    double std_3 = 0.333333;

    double lambda_4 = 0;     // Test 0
    double lambda_5 = -2.3;  // Test negative value

    const float threshold = 1e-3f;

    std::cout << "Test for lambda = 1 \n";
    assert(
        std::abs(expected_1 - probability::geometric_dist::exponential_expected(
                                  lambda_1)) < threshold);
    assert(std::abs(var_1 - probability::geometric_dist::exponential_var(
                                lambda_1)) < threshold);
    assert(std::abs(std_1 - probability::geometric_dist::exponential_std(
                                lambda_1)) < threshold);
    std::cout << "ALL TEST PASSED\n\n";

    std::cout << "Test for lambda = 2 \n";
    assert(
        std::abs(expected_2 - probability::geometric_dist::exponential_expected(
                                  lambda_2)) < threshold);
    assert(std::abs(var_2 - probability::geometric_dist::exponential_var(
                                lambda_2)) < threshold);
    assert(std::abs(std_2 - probability::geometric_dist::exponential_std(
                                lambda_2)) < threshold);
    std::cout << "ALL TEST PASSED\n\n";

    std::cout << "Test for lambda = 3 \n";
Repository: TheAlgorithms/C-Plus-Plus
File: probability/exponential_dist.cpp (C++)

Code before:
/**
 * @file
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * @brief [Exponential Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * 
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 * 
 * \( P(x, \lambda) = \lambda e^{-\lambda x} \)
 * 
 * Summary of variables used:
 * \lambda : rate parameter
 */

#include<cmath> // For power function
#include<cassert> // For asserting the test cases
#include<iostream> // For I/O operation

/** 
 * @brief the expected value of the exponential distribution
 * @returns \( \mu = \frac{1}{\lambda} \)
 */
double exponential_expected(double lambda){
    if (lambda<=0){
        std::cout << "Error: Lambda must be greater than 0." << std::endl;
        assert(lambda>0);
    }
    return 1/lambda;
}

/** 
 * @brief the variance of the exponential distribution
 * @returns \( \sigma^2 = \frac{1}{\lambda^2} \)
 */

Review comment (suggestion):
```suggestion
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
```


Code after:
/**
 * @file
 * @brief [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 *
 * The exponential distribution is used to model
 * events occuring between a Poisson process like radioactive decay.
 *
 * \f[P(x, \lambda) = \lambda e^{-\lambda x}\f]
 *
 * Summary of variables used:
 * \f$\lambda\f$ : rate parameter
 */

#include <cassert>    // For assert
#include <cmath>      // For std::pow
#include <iostream>   // For I/O operation
#include <stdexcept>  // For std::invalid_argument
#include <string>     // For std::string

/**
 * @namespace probability
 * @brief Probability algorithms
 */
namespace probability {
/**
 * @namespace exponential_dist
 * @brief Functions for the [Exponential
 * Distribution](https://en.wikipedia.org/wiki/Exponential_distribution)
 * algorithm implementation
 */
namespace geometric_dist {
/**
 * @brief the expected value of the exponential distribution