The Wayback Machine - https://web.archive.org/web/20210929185713/https://www.geeksforgeeks.org/check-if-given-array-can-be-divided-into-subsequences-of-k-increasing-consecutive-integers/
Skip to content
Related Articles

Related Articles

Improve Article

Check if given Array can be divided into subsequences of K increasing consecutive integers

  • Last Updated : 29 Sep, 2021

Given an array arr[] of N integers and a positive integer K, the task is to check if it is possible to divide the array into increasing subsequences of K consecutive integers such each element can contribute in only a single subsequence.

Example:

Input: arr[] = {1, 2, 1, 3, 2, 3}, K = 3
Output: Yes
Explanation: The given array can be divided as {1, 2, 1, 3, 2, 3} => {1, 2, 3} and {1, 2, 1, 3, 2, 3} => {1, 2, 3}. Both subsequences have 3 consecutive integers in increasing order.

Input: arr[] = {4, 3, 1, 2}, K = 2
Output: No

Approach: The above problem can be solved using a Greedy Approach using Binary Search. It can be observed that for any integer arr[i], the most optimal choice is to choose the smallest index of arr[i] + 1 in the subarray arr[i+1, N). Using this observation, follow the below steps to solve the given problem:



  • If K is not a divisor of N, no possible set of required subsequences exist. Hence, print No.
  • Store the indices of each integer in a Set data structure. It can be efficiently stored using a map that has the structure of key-set pair.
  • Maintain a visited array to keep track of indices that are already included in a subsequence.
  • Iterate for each i in the range [0, N), and if the integer at the current index is not already visited, perform the following steps:
    • Using the upper_bound function, find the smallest index of arr[i] + 1 in the range [i+1, N) and update the value of the last element of the current subsequence with it.
    • Repeat the above-mentioned step K-1 number of times until a complete subsequence of K integers has been created.
  • During any iteration, if the required integer does not exist, no possible set of required subsequences exist. Hence, print No. Otherwise, print Yes.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the array can
// be divided into subsequences of K
// consecutive integers in increasing order
bool isPossible(vector<int> nums, int K)
{
    int N = nums.size();
  
    // If N is not divisible by K or
    // K>N, no possible set of required
    // subsequences exist
    if (N % K != 0 || K > N) {
        return false;
    }
  
    // Stores the indices of each
    // element in a set
    map<int, set<int> > idx;
  
    // Stores the index of each number
    for (int i = 0; i < nums.size(); i++) {
        idx[nums[i]].insert(i);
    }
  
    // Stores if the integer at current
    // index is already included in
    // a subsequence
    int visited[N] = { 0 };
  
    // Stores the count of total
    // visited elements
    int total_visited = 0;
  
    for (int i = 0; i < nums.size(); i++) {
  
        // If current integer is already
        // in a subsequence
        if (visited[i]) {
            continue;
        }
  
        // Stores the value of last element
        // of the current subsequence
        int num = nums[i];
  
        // Stores the index of last element
        // of the current subsequence
        int last_index = i;
  
        // Mark Visited
        visited[i] = 1;
  
        // Increment the visited count
        total_visited++;
  
        // Find the next K-1 elements of the
        // subsequence starting from index i
        for (int j = num + 1; j < num + K; j++) {
  
            // No valid index found
            if (idx[j].size() == 0) {
                return false;
            }
  
            // Find index of j such that it
            // is greater than last_index
            auto it = idx[j].upper_bound(last_index);
  
            // if no such index found,
            // return false
            if (it == idx[j].end()
                || *it <= last_index) {
                return false;
            }
  
            // Update last_index
            last_index = *it;
  
            // Mark current index as visited
            visited[last_index] = 1;
  
            // Increment total_visited by 1
            total_visited++;
  
            // Remove the index from set because
            // it has been already used
            idx[j].erase(it);
        }
    }
  
    // Return the result
    return total_visited == N;
}
  
// Driver Code
int main()
{
    vector<int> arr = { 4, 3, 1, 2 };
    int K = 2;
    cout << (isPossible(arr, K) ? "Yes" : "No");
  
    return 0;
}
Output:
No

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :