Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
dynamic_programming Namespace Reference

Dynamic Programming algorithms. More...

Functions

template<typename T >
bool is_armstrong (const T &number)
 Checks if the given number is armstrong or not.
 
uint64_t LIS (const std::vector< uint64_t > &a, const uint32_t &n)
 Calculate the longest increasing subsequence for the specified numbers.
 
std::string lps (const std::string &a)
 Function that returns the longest palindromic subsequence of a string.
 
int maxCircularSum (std::vector< int > &arr)
 returns the maximum contiguous circular sum of an array
 

Detailed Description

Dynamic Programming algorithms.

Dynamic programming algorithms.

for std::vector

Dynamic Programming algorithm.

Dynamic Programming Algorithms.

for assert for IO operations for std::string library for std::vector STL library

for assert for std::max for io operations

Dynamic Programming algorithms

for assert for std::max for IO operations

Dynamic Programming algorithms

for assert for IO operations

Dynamic Programming algorithms

for std::assert for IO operations for unordered map

Dynamic Programming algorithms

Function Documentation

◆ is_armstrong()

template<typename T >
bool dynamic_programming::is_armstrong ( const T & number)

Checks if the given number is armstrong or not.

Parameters
numberthe number to check
Returns
false if the given number is NOT armstrong
true if the given number IS armstrong
39 {
40 int count = 0, temp = number, result = 0, rem = 0;
41
42 // Count the number of digits of the given number.
43 // For example: 153 would be 3 digits.
44 while (temp != 0) {
45 temp /= 10;
46 count++;
47 }
48
49 // Calculation for checking of armstrongs number i.e.
50 // in an n-digit number sum of the digits is raised to a power of `n` is
51 // equal to the original number.
52 temp = number;
53 while (temp != 0) {
54 rem = temp % 10;
55 result += static_cast<T>(std::pow(rem, count));
56 temp /= 10;
57 }
58
59 if (result == number) {
60 return true;
61 } else {
62 return false;
63 }
64}
T pow(T... args)
Here is the call graph for this function:

◆ LIS()

uint64_t dynamic_programming::LIS ( const std::vector< uint64_t > & a,
const uint32_t & n )

Calculate the longest increasing subsequence for the specified numbers.

Parameters
athe array used to calculate the longest increasing subsequence
nthe size used for the arrays
Returns
the length of the longest increasing subsequence in the a array of size n
39 {
40 std::vector<int> lis(n);
41 for (int i = 0; i < n; ++i) {
42 lis[i] = 1;
43 }
44 for (int i = 0; i < n; ++i) {
45 for (int j = 0; j < i; ++j) {
46 if (a[i] > a[j] && lis[i] < lis[j] + 1) {
47 lis[i] = lis[j] + 1;
48 }
49 }
50 }
51 int res = 0;
52 for (int i = 0; i < n; ++i) {
53 res = std::max(res, lis[i]);
54 }
55 return res;
56}
T max(T... args)
Here is the call graph for this function:

◆ lps()

std::string dynamic_programming::lps ( const std::string & a)

Function that returns the longest palindromic subsequence of a string.

Parameters
astring whose longest palindromic subsequence is to be found
Returns
longest palindromic subsequence of the string
31 {
32 const auto b = std::string(a.rbegin(), a.rend());
33 const auto m = a.length();
34 using ind_type = std::string::size_type;
37
38 // Finding the length of the longest
39 // palindromic subsequence and storing
40 // in a 2D array in bottoms-up manner
41 for (ind_type i = 0; i <= m; i++) {
42 for (ind_type j = 0; j <= m; j++) {
43 if (i == 0 || j == 0) {
44 res[i][j] = 0;
45 } else if (a[i - 1] == b[j - 1]) {
46 res[i][j] = res[i - 1][j - 1] + 1;
47 } else {
48 res[i][j] = std::max(res[i - 1][j], res[i][j - 1]);
49 }
50 }
51 }
52 // Length of longest palindromic subsequence
53 auto idx = res[m][m];
54 // Creating string of index+1 length
55 std::string ans(idx, '\0');
56 ind_type i = m, j = m;
57
58 // starting from right-most bottom-most corner
59 // and storing them one by one in ans
60 while (i > 0 && j > 0) {
61 // if current characters in a and b are same
62 // then it is a part of the ans
63 if (a[i - 1] == b[j - 1]) {
64 ans[idx - 1] = a[i - 1];
65 i--;
66 j--;
67 idx--;
68 }
69 // If they are not same, find the larger of the
70 // two and move in that direction
71 else if (res[i - 1][j] > res[i][j - 1]) {
72 i--;
73 } else {
74 j--;
75 }
76 }
77
78 return ans;
79}
Here is the call graph for this function:

◆ maxCircularSum()

int dynamic_programming::maxCircularSum ( std::vector< int > & arr)

returns the maximum contiguous circular sum of an array

Parameters
arris the array/vector
Returns
int which is the maximum sum
27{
28 // Edge Case
29 if (arr.size() == 1)
30 return arr[0];
31
32 // Sum variable which stores total sum of the array.
33 int sum = 0;
34 for (int i = 0; i < arr.size(); i++) {
35 sum += arr[i];
36 }
37
38 // Every variable stores first value of the array.
39 int current_max = arr[0], max_so_far = arr[0], current_min = arr[0], min_so_far = arr[0];
40
41 // Concept of Kadane's Algorithm
42 for (int i = 1; i < arr.size(); i++) {
43 // Kadane's Algorithm to find Maximum subarray sum.
44 current_max = std::max(current_max + arr[i], arr[i]);
45 max_so_far = std::max(max_so_far, current_max);
46
47 // Kadane's Algorithm to find Minimum subarray sum.
48 current_min = std::min(current_min + arr[i], arr[i]);
49 min_so_far = std::min(min_so_far, current_min);
50 }
51
52 if (min_so_far == sum)
53 return max_so_far;
54
55 // Return the maximum value
56 return std::max(max_so_far, sum - min_so_far);
57}
T min(T... args)
T size(T... args)
Here is the call graph for this function: