Given an array of integers nums
and a positive integer k
, find whether it's possible to divide this array into sets of k
consecutive numbers
Return True
if it is possible. Otherwise, return False
.
Example :
Input: nums = [1,2,3,3,4,4,5,6], k = 4
Output: true
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
Approach:
C++
#include <bits/stdc++.h>using namespace std;bool isPossibleDivide(vector<int> &nums, int k){//sort the arraysort(nums.begin(), nums.end());priority_queue<pair<int, int>, vector<pair<int, int>>,greater<pair<int, int>>> pq;//iterate for all the elements of the//arrayfor (int num : nums){while (!pq.empty() && (pq.top().first < num - 1|| pq.top().second == k)){if (pq.top().second != k){return false;}pq.pop();}if (pq.empty() || pq.top().first == num){pq.push(make_pair(num, 1));continue;}int size = pq.top().second + 1;pq.pop();pq.push(make_pair(num, size));}while (!pq.empty()){if (pq.top().second != k){return false;}pq.pop();}return true;}int main(){vector<int> nums = {1, 2, 3, 3, 4, 4, 5, 6};int k = 4;if (isPossibleDivide(nums, k))cout << "true";elsecout << "false";return 0;}
No comments:
Post a Comment