Open In App

Longest Subarray with K Sections of Unique Items

Last Updated : 20 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

You are given an array of positive integers arr[] and an integer k. The task is to find length of the longest subarray with the following conditions

  • Each element must fit into one of k sections.
  • Each section can only store a unique number and its multiple consecutive instances.

Examples:

Input: arr[] = [1, 2, 2, 3, 1, 4], k = 2
Output: 3
Explanation: The subarray is [1, 2, 2, 3, 1], the sections are [1], [2, 2], [3] and [1]
Total elements chosen = 3.

Input: arr[] = [4, 1, 1, 3, 2, 2], k = 3
Output: 5
Explanation: The subarray is [1, 1, 3, 2, 2], the sections are [1, 1], [3] and [2, 2]

Input: arr[] = [5, 5, 5, 5, 5], k = 1
Output: 5
Explanation: All elements are 5, and k = 1, so all can be stored in one section.

Approach:

The idea is to find the maximum number of elements that can be selected while ensuring that at most k unique elements exist at any time. Using the Sliding Window technique, we expand the window by adding elements and shrink it when the number of unique elements exceeds k. The frequency map helps track the count of elements, ensuring efficient updates. The observation is that instead of checking all possible selections, a dynamic window allows solving the problem efficiently in linear time.

Steps to implement the above idea:

  • Initialize left, right, and count to track the sliding window, along with a frequency map (mpp) to store element counts.
  • Iterate using right, adding elements to mpp, increasing count, and ensuring each element fits within the allowed k unique elements.
  • If the number of unique elements in mpp exceeds k, shrink the window by moving left and decreasing count.
  • Remove elements from mpp if their count becomes zero to maintain the valid unique element limit.
  • Continuously update maxCount whenever a valid window with k or fewer unique elements is found.
  • Expand the window by incrementing right until the entire array is processed.
  • Return maxCount, which stores the maximum number of elements.
C++
// C++ Code to find the maximum number of elements 
// that can be selected using Sliding Window
#include <bits/stdc++.h>
using namespace std;

int maxElements(vector<int> &arr, int k) {
    int n = arr.size();
    int right = 0, left = 0, count = 0;
    
    // Map to maintain count of elements in sections
    unordered_map<int, int> mpp;
    
    int maxCount = 0;

    // Iterate through the array
    while (right < n) {
        
        // Add current element to the frequency map
        mpp[arr[right]]++;
        count++;

        // If unique elements exceed k, shrink 
        // window from left
        while (mpp.size() > k) {
            mpp[arr[left]]--;
            count--;

            // Remove element if count becomes zero
            if (mpp[arr[left]] == 0) {
                mpp.erase(arr[left]);
            }
            left++;
        }

        // Update the maximum number of elements chosen
        if (mpp.size() <= k) {
            maxCount = max(maxCount, count);
        }
        right++;
    }

    // Return the final answer
    return maxCount;
}

int main() {
    
    vector<int> arr = {1, 2, 2, 3, 1, 4};
    int k = 2;
    
    cout << maxElements(arr, k) << endl;

    return 0;
}
Java
// Java Code to find the maximum number of elements 
// that can be selected using Sliding Window
import java.util.*;

class GfG {
    static int maxElements(int[] arr, int k) {
        int n = arr.length;
        int right = 0, left = 0, count = 0;

        // Map to maintain count of elements in sections
        HashMap<Integer, Integer> mpp = new HashMap<>();

        int maxCount = 0;

        // Iterate through the array
        while (right < n) {

            // Add current element to the frequency map
            mpp.put(arr[right], mpp.getOrDefault(arr[right], 0) + 1);
            count++;

            // If unique elements exceed k, shrink 
            // window from left
            while (mpp.size() > k) {
                mpp.put(arr[left], mpp.get(arr[left]) - 1);
                count--;

                // Remove element if count becomes zero
                if (mpp.get(arr[left]) == 0) {
                    mpp.remove(arr[left]);
                }
                left++;
            }

            // Update the maximum number of elements chosen
            if (mpp.size() <= k) {
                maxCount = Math.max(maxCount, count);
            }
            right++;
        }

        // Return the final answer
        return maxCount;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 2, 3, 1, 4};
        int k = 2;

        System.out.println(maxElements(arr, k));
    }
}
Python
# Python Code to find the maximum number of elements 
# that can be selected using Sliding Window

def maxElements(arr, k):
    n = len(arr)
    right = 0
    left = 0
    count = 0

    # Dictionary to maintain count of elements in sections
    mpp = {}

    maxCount = 0

    # Iterate through the array
    while right < n:
        
        # Add current element to the frequency map
        mpp[arr[right]] = mpp.get(arr[right], 0) + 1
        count += 1

        # If unique elements exceed k, shrink 
        # window from left
        while len(mpp) > k:
            mpp[arr[left]] -= 1
            count -= 1

            # Remove element if count becomes zero
            if mpp[arr[left]] == 0:
                del mpp[arr[left]]
            left += 1

        # Update the maximum number of elements chosen
        if len(mpp) <= k:
            maxCount = max(maxCount, count)
        right += 1

    # Return the final answer
    return maxCount

if __name__ == "__main__":
    
    arr = [1, 2, 2, 3, 1, 4]
    k = 2

    print(maxElements(arr, k))
C#
// C# Code to find the maximum number of elements 
// that can be selected using Sliding Window
using System;
using System.Collections.Generic;

class GfG {
    public static int maxElements(int[] arr, int k) {
        int n = arr.Length;
        int right = 0, left = 0, count = 0;

        // Dictionary to maintain count of elements in sections
        Dictionary<int, int> mpp = new Dictionary<int, int>();

        int maxCount = 0;

        // Iterate through the array
        while (right < n) {

            // Add current element to the frequency map
            if (mpp.ContainsKey(arr[right])) {
                mpp[arr[right]]++;
            } else {
                mpp[arr[right]] = 1;
            }
            count++;

            // If unique elements exceed k, shrink 
            // window from left
            while (mpp.Count > k) {
                mpp[arr[left]]--;
                count--;

                // Remove element if count becomes zero
                if (mpp[arr[left]] == 0) {
                    mpp.Remove(arr[left]);
                }
                left++;
            }

            // Update the maximum number of elements chosen
            if (mpp.Count <= k) {
                maxCount = Math.Max(maxCount, count);
            }
            right++;
        }

        // Return the final answer
        return maxCount;
    }

    public static void Main() {
        int[] arr = {1, 2, 2, 3, 1, 4};
        int k = 2;

        Console.WriteLine(maxElements(arr, k));
    }
}
JavaScript
// JavaScript Code to find the maximum number of elements 
// that can be selected using Sliding Window

function maxElements(arr, k) {
    let n = arr.length;
    let right = 0, left = 0, count = 0;

    // Map to maintain count of elements in sections
    let mpp = new Map();

    let maxCount = 0;

    // Iterate through the array
    while (right < n) {

        // Add current element to the frequency map
        mpp.set(arr[right], (mpp.get(arr[right]) || 0) + 1);
        count++;

        // If unique elements exceed k, shrink 
        // window from left
        while (mpp.size > k) {
            mpp.set(arr[left], mpp.get(arr[left]) - 1);
            count--;

            // Remove element if count becomes zero
            if (mpp.get(arr[left]) === 0) {
                mpp.delete(arr[left]);
            }
            left++;
        }

        // Update the maximum number of elements chosen
        if (mpp.size <= k) {
            maxCount = Math.max(maxCount, count);
        }
        right++;
    }

    // Return the final answer
    return maxCount;
}

// Driver code
let arr = [1, 2, 2, 3, 1, 4];
let k = 2;

console.log(maxElements(arr, k));

Output
3

Time Complexity: O(n), as each element is processed at most twice, once when added and once when removed.
Space Complexity: O(k), as the frequency map stores at most k unique elements at any time.


Next Article
Article Tags :
Practice Tags :

Similar Reads