diff --git a/Searching/splitArray.cpp b/Searching/splitArray.cpp new file mode 100644 index 0000000..11388f4 --- /dev/null +++ b/Searching/splitArray.cpp @@ -0,0 +1,51 @@ +/* Problem Statement : Given an integer array nums and an integer k, +split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. +Return the minimized largest sum of the split. +Note - A subarray is a contiguous part of the array.*/ + + +#include +using namespace std; +#define ll long long int + +int splitArray(vector& nums, int k) { + int l = 0, r = 0, n = nums.size(); + for (int i = 0; i < n; ++i) l = max(l, nums[i]), r += nums[i]; + + int mid = 0, ans = 0; + while (l <= r) { + mid = (l + r) / 2; + int count = 0, tempsum = 0; + for (int i = 0; i < n; ++i) { + if (tempsum + nums[i] <= mid) tempsum += nums[i]; + else count++, tempsum = nums[i]; + } + count++; + + if (count <= k) r = mid - 1, ans = mid; + else l = mid + 1; + } + return ans; +} + +int main() +{ + +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + + int n; + cin >> n; + vector vec(n); + for (int i = 0; i < n; i++) + { + cin >> vec[i]; + } + int k; + cin >> k; + + cout << splitArray(vec, k) << endl; + return 0; +}