Skip to content

Commit b890d4d

Browse files
committed
feat:Added Implementation of Split_Largest_Array_Sum Algorithm
1 parent c2adc3b commit b890d4d

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @file
3+
* @brief Given an integer array nums and an integer k, split nums into k
4+
* non-empty subarrays such that the largest sum of any subarray is minimized.
5+
* Return the minimized largest sum of the split.
6+
7+
* @details
8+
* Example 1:
9+
10+
* Input: nums = [7,2,5,10,8], k = 2
11+
* Output: 18
12+
* Explanation: There are four ways to split nums into two subarrays.
13+
* The best way is to split it into [7,2,5] and [10,8], where the largest sum
14+
* among the two subarrays is only 18.
15+
16+
* Example 2:
17+
18+
* Input: nums = [1,2,3,4,5], k = 2
19+
* Output: 9
20+
* Explanation: There are four ways to split nums into two subarrays.
21+
* The best way is to split it into [1,2,3] and [4,5], where the largest sum
22+
* among the two subarrays is only 9.
23+
24+
* @author [ARYA PRATAP SINGH](https://github.com/ARYPROGRAMMER)
25+
* [Leetcode](https://leetcode.com/problems/split-array-largest-sum/description/?envType=problem-list-v2&envId=dynamic-programming)
26+
*/
27+
28+
// header files
29+
#include <iostream>
30+
#include <iomanip>
31+
#include <cstdint>
32+
33+
/**
34+
* @namespace dynamic_programming
35+
* @brief Dynamic Programming algorithms
36+
*/
37+
namespace dynamic_programming{
38+
/**
39+
* @namespace split_array_largest_sum
40+
* @brief split_array_largest_sum algorithm
41+
*/
42+
namespace split_array_largest_sum{
43+
44+
/**
45+
* @brief This function calculates the minimum largest sum of the split
46+
* @param nums integer array
47+
* @param k integer
48+
* @
49+
*/
50+
51+
int dp[1003][53];
52+
53+
int f(int i, int j, vector<int>& v) {
54+
if (j < 0) {
55+
if (i < 0)
56+
return -1;
57+
return INT_MAX;
58+
}
59+
if (i < 0)
60+
return INT_MAX;
61+
if (dp[i][j] != -1)
62+
return dp[i][j];
63+
64+
int res = INT_MAX, sum = 0;
65+
for (int k = i; k >= 0; k--) {
66+
sum += v[k];
67+
res = min(res, max(sum, f(k - 1, j - 1, v)));
68+
}
69+
70+
return dp[i][j] = res;
71+
}
72+
73+
int splitArray(vector<int>& nums, int k) {
74+
memset(dp, -1, sizeof(dp));
75+
return f(nums.size() - 1, k - 1, nums);
76+
}
77+
}
78+
}
79+
80+
/**
81+
* Test Function
82+
* @return void
83+
*/
84+
static void test() {
85+
// custom input vector
86+
std::vector<int> v{
87+
7,2,5,10,8,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
88+
};
89+
// custom value of k
90+
int k = 5;
91+
92+
//calling the function
93+
int ans = dynamic_programming::split_array_largest_sum::splitArray(v, k);
94+
95+
// expected output
96+
int expectedOutput = 30;
97+
98+
// Testing implementation via assert function
99+
// It will throw error if any of the expected test fails
100+
// Else it will give nothing
101+
assert(ans == expectedOutput);
102+
103+
std::cout << "All tests passed successfully!\n";
104+
return;
105+
}
106+
107+
/** Main function (driver code)*/
108+
int main() {
109+
// test for implementation
110+
test();
111+
112+
// user input
113+
int n;
114+
std::cout << "Enter the number of elements in the array : ";
115+
std::cin >> n;
116+
std::vector<int> v(n);
117+
std::cout << "Enter the elements of the array : ";
118+
for (int i = 0; i < n; i++) {
119+
std::cin >> v[i];
120+
}
121+
int k;
122+
std::cout << "Enter the value of k : ";
123+
std::cin >> k;
124+
125+
int ans;
126+
127+
// user output
128+
ans = dynamic_programming::split_array_largest_sum::splitArray(v, k);
129+
std::cout << "The minimum largest sum of the split is : " << ans << std::endl;
130+
return 0;
131+
}

0 commit comments

Comments
 (0)