- Related Questions & Answers
- Making two sequences increasing in JavaScript
- Converting array into increasing sequence in JavaScript
- Minimum Swaps To Make Sequences Increasing in C++
- Total number of longest increasing sequences in JavaScript
- How to divide an array into half in java?
- All ways to divide array of strings into parts in JavaScript
- How to generate sequences in Python?
- Print all increasing sequences of length k from first n natural numbers in C++
- Removing least number of elements to convert array into increasing sequence using JavaScript
- Divide a string into n equal parts - JavaScript
- Divide a number into two parts in C++ Program
- Make Array Strictly Increasing in C++
- Longest Increasing Subsequence in Python
- Increasing Triplet Subsequence in Python
- Program to sort array by increasing frequency of elements in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a non-decreasing array of positive integers called nums and an integer K, we have to find out if this array can be divided into one or more number of disjoint increasing subsequences of length at least K.

So, if the input is like nums = [1,2,2,3,3,4,4], K = 3, then the output will be true, as this array can be divided into the two subsequences like [1,2,3,4] and [2,3,4] with lengths at least 3 each.

To solve this, we will follow these steps −

d := a new map

req := 0

for each i in nums, do

if i not in d is non-zero, then

d[i]:= 1

otherwise,

d[i] := d[i] + 1

req := maximum of req, d[i]

return true when req*K <= size of nums

Let us see the following implementation to get better understanding −

class Solution(object): def canDivideIntoSubsequences(self, nums, K): d = {} req = 0 for i in nums: if i not in d: d[i]=1 else: d[i]+=1 req = max(req,d[i]) return req*K<=len(nums) ob = Solution() print(ob.canDivideIntoSubsequences([1,2,2,3,3,4,4],3))

[1,2,2,3,3,4,4]. 3

True

Advertisements