Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Title: Variable Length Sliding window
- * Author: Samuel Finocchio
- * Date: 05/03/2023
- *
- */
- #include <iostream>
- #include <vector>
- #include <limits>
- int main() {
- int N, C;
- std::cin >> N >> C;
- std::vector<int> numbers;
- numbers.reserve(N);
- for (int i = 0; i < N; i++) {
- int temp;
- std::cin >> temp;
- numbers.push_back(temp);
- }
- int optimal = std::numeric_limits<int>::max();
- int start = 0, end = 0, sum = numbers.at(0);
- while (start < numbers.size()) {
- if (sum >= C) {
- optimal = std::min(optimal, (end - start) + 1);
- // Removing from left side
- sum = sum - (numbers.at(start));
- start++;
- } else {
- // Adding to the right side
- end++;
- if (end < numbers.size()) {
- sum = sum + (numbers.at(end));
- } else {
- break;
- }
- }
- }
- std::cout << optimal;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement