Skip to content

Commit 3d57dbe

Browse files
committed
added Divide Array in Sets of K Consecutive Numbers (medium)
1 parent 3d78157 commit 3d57dbe

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# Divide Array in Sets of K Consecutive Numbers
3+
[Leetcode Link](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)
4+
5+
## Problem:
6+
7+
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
8+
Return `True` if its possible otherwise return `False`.
9+
10+
## Example:
11+
12+
```
13+
Input: nums = [1,2,3,3,4,4,5,6], k = 4
14+
Output: true
15+
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
16+
```
17+
```
18+
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
19+
Output: true
20+
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
21+
```
22+
```
23+
Input: nums = [3,3,2,2,1,1], k = 3
24+
Output: true
25+
```
26+
```
27+
Input: nums = [1,2,3,4], k = 3
28+
Output: false
29+
Explanation: Each array should be divided in subarrays of size 3.
30+
```
31+
32+
## Note:
33+
34+
- 1 <= nums.length <= 10^5
35+
- 1 <= nums[i] <= 10^9
36+
- 1 <= k <= nums.length
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
class Solution:
4+
def isPossibleDivide(self, nums: List[int], k: int) -> bool:
5+
# if array length is not divisible by k, then there can never be even sets of k intgers
6+
if len(nums) % k != 0:
7+
return False
8+
# sort array
9+
nums.sort()
10+
while nums:
11+
consecutiveSet = list()
12+
consecutiveSet.append(nums[0])
13+
for i in range(1, len(nums)+1):
14+
# if out of bound, then not possible with remaining integers
15+
if i == len(nums):
16+
return False
17+
# if current number is strictly ONE greater than prev one, add it to consecutive set (to be removed from array)
18+
if nums[i] == consecutiveSet[-1] + 1:
19+
consecutiveSet.append(nums[i])
20+
elif nums[i] > consecutiveSet[-1] + 1:
21+
return False
22+
# if we found k consecutive integers, remove them from array and repeat
23+
if len(consecutiveSet) == k:
24+
for num in consecutiveSet:
25+
nums.remove(num)
26+
break
27+
return True
28+
29+
30+
sol = Solution()
31+
nums = [3,2,1,2,3,4,3,4,5,9,10,11]
32+
k = 3
33+
print(f'Input: nums = {nums}, k = {k}')
34+
print("Output:", sol.isPossibleDivide(nums, k))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Languages used: Java and Python
8080
- [Design Linked List](Medium/DesignLinkedList)
8181
- [Single Number II](Medium/SingleNumber2)
8282
- [Count Square Submatrices with All Ones](Medium/CountSquareWithOnes)
83+
- [Divide Array is Sets of K Consecutive Numbers](Medium/DivideArrayInKConsecNums)
8384
- Hard
8485
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8586
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)