bool isPossible(int k, int mid, const vector<int> &arr) {
int students = 1, curPage = 0;
for(pages : arr) {
if(pages > mid) return false;
if(curPage + pages > mid) {
students++;
curPage = pages;
if(students > k) return false;
} else {
curPage += pages;
}
}
return true;
}
int findPages(vector<int> &arr, int k) {
// code here
if(k > arr.size()) return -1;
int ret = -1;
int low = *max_element(arr.begin(), arr.end()); // max value in array
int high = accumulate(arr.begin(), arr.end(), 0); // sum of all elements in array
while(low <= high) {
int mid = low + (high-low) /2;
if(isPossible(k, mid, arr)) {
ret = mid;
high = mid-1;
} else {
low = mid+1;
}
}
return ret;
}
Category: GFG
GFG PTOD | 17th Dec | Aggressive Cows
bool isPossible(int mid, int k, vector<int> &stalls) {
int count = 1;
int lastPos = stalls[0];
for(int i=1; i < stalls.size(); i++) {
if(stalls[i] - lastPos >= mid) {
count++;
lastPos = stalls[i];
if(count == k) {
return true;
}
}
}
return false;
}
int aggressiveCows(vector<int> &stalls, int k) {
// Write your code here
sort(stalls.begin(), stalls.end());
int low = 1;
int high = stalls.back() - stalls.front();
int res = 0;
while(low <= high) {
int mid = low + (high - low) / 2;
if(isPossible(mid, k, stalls)) {
res = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return res;
}
GFG : Maximum MEX from all subarrays of length K
Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum MEX from all subarray of length K. The MEX is the smallest positive integer that is not present in the array. Solution :
GFG : Max Sum Subarray of size K
Problem: Given an array of integers Arr of size N and a number K. Return the maximum sum of a subarray of size K. NOTE*: A subarray is a contiguous part of any given array. Solution:
GFG : First negative integer in every window of size k
Problem Given an array A[] of size N and a positive integer K, find the first negative integer for each and every window(contiguous subarray) of size K. Solution-1 Solution-2
GFG : Count distinct elements in every window
Problem : Given an array of integers and a number K. Find the count of distinct elements in every window of size K in the array. Solution:
GFG : IPL 2021 – Match Day 2
Problem : Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 2 and it is between the most loved team Chennai Super Kings and the most underrated team – Punjab Kings. Stephen Fleming, the head coach of CSK, analyzing the…
121. Best Time to Buy and Sell Stock.
Problem : You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Solution…
LeetCode : 2461. Maximum Sum of Distinct Subarrays With Length K
Problem: You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions: The length of the subarray is k, andAll the elements of the subarray are distinct.Return the maximum subarray sum of all the subarrays that meet the conditions….
Read More “LeetCode : 2461. Maximum Sum of Distinct Subarrays With Length K” »
LeetCode : 239. Sliding Window Maximum
Problem: You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding…