Maximum Unique Element in every subarray of size K
Last Updated :
01 Dec, 2022
Given an array and an integer K. We need to find the maximum of every segment of length K which has no duplicates in that segment.
Examples:
Input : a[] = {1, 2, 2, 3, 3},
K = 3.
Output : 1 3 2
For segment (1, 2, 2), Maximum = 1.
For segment (2, 2, 3), Maximum = 3.
For segment (2, 3, 3), Maximum = 2.
Input : a[] = {3, 3, 3, 4, 4, 2},
K = 4.
Output : 4 Nothing 3
A simple solution is to run two loops. For every subarray, find all distinct elements and print maximum unique elements.
An efficient solution is to use the sliding window technique. We have two structures in every window.
- A hash table to store counts of all elements in the current window.
- A self-balancing BST (implemented using set in C++ STL and TreeSet in Java). The idea is to quickly find the maximum element and update the maximum elements.
We process the first K-1 elements and store their counts in the hash table. We also store unique elements in set. Now we, one by one, process the last element of every window. If the current element is unique, we add it to the set. We also increase its count. After processing the last element, we print the maximum from the set. Before starting the next iteration, we remove the first element of the previous window.
Implementation:
C++
// C++ code to calculate maximum unique
// element of every segment of array
#include <bits/stdc++.h>
using namespace std;
void find_max(int A[], int N, int K)
{
// Storing counts of first K-1 elements
// Also storing distinct elements.
map<int, int> Count;
for (int i = 0; i < K - 1; i++)
Count[A[i]]++;
set<int> Myset;
for (auto x : Count)
if (x.second == 1)
Myset.insert(x.first);
// Before every iteration of this loop,
// we maintain that K-1 elements of current
// window are processed.
for (int i = K - 1; i < N; i++) {
// Process K-th element of current window
Count[A[i]]++;
if (Count[A[i]] == 1)
Myset.insert(A[i]);
else
Myset.erase(A[i]);
// If there are no distinct
// elements in current window
if (Myset.size() == 0)
printf("Nothing\n");
// Set is ordered and last element
// of set gives us maximum element.
else
printf("%d\n", *Myset.rbegin());
// Remove first element of current
// window before next iteration.
int x = A[i - K + 1];
Count[x]--;
if (Count[x] == 1)
Myset.insert(x);
if (Count[x] == 0)
Myset.erase(x);
}
}
// Driver code
int main()
{
int a[] = { 1, 2, 2, 3, 3 };
int n = sizeof(a) / sizeof(a[0]);
int k = 3;
find_max(a, n, k);
return 0;
}
Java
// Java code to calculate maximum unique
// element of every segment of array
import java.io.*;
import java.util.*;
class GFG {
static void find_max(int[] A, int N, int K)
{
// Storing counts of first K-1 elements
// Also storing distinct elements.
HashMap<Integer, Integer> Count = new HashMap<>();
for (int i = 0; i < K - 1; i++)
if (Count.containsKey(A[i]))
Count.put(A[i], 1 + Count.get(A[i]));
else
Count.put(A[i], 1);
TreeSet<Integer> Myset = new TreeSet<Integer>();
for (Map.Entry x : Count.entrySet()) {
if (Integer.parseInt(String.valueOf(x.getValue())) == 1)
Myset.add(Integer.parseInt(String.valueOf(x.getKey())));
}
// Before every iteration of this loop,
// we maintain that K-1 elements of current
// window are processed.
for (int i = K - 1; i < N; i++) {
// Process K-th element of current window
if (Count.containsKey(A[i]))
Count.put(A[i], 1 + Count.get(A[i]));
else
Count.put(A[i], 1);
if (Integer.parseInt(String.valueOf(Count.get(A[i]))) == 1)
Myset.add(A[i]);
else
Myset.remove(A[i]);
// If there are no distinct
// elements in current window
if (Myset.size() == 0)
System.out.println("Nothing");
// Set is ordered and last element
// of set gives us maximum element.
else
System.out.println(Myset.last());
// Remove first element of current
// window before next iteration.
int x = A[i - K + 1];
Count.put(x, Count.get(x) - 1);
if (Integer.parseInt(String.valueOf(Count.get(x))) == 1)
Myset.add(x);
if (Integer.parseInt(String.valueOf(Count.get(x))) == 0)
Myset.remove(x);
}
}
// Driver code
public static void main(String args[])
{
int[] a = { 1, 2, 2, 3, 3 };
int n = a.length;
int k = 3;
find_max(a, n, k);
}
}
// This code is contributed by rachana soma
Python3
# Python3 code to calculate maximum unique
# element of every segment of array
def find_max(A, N, K):
# Storing counts of first K-1 elements
# Also storing distinct elements.
Count = dict()
for i in range(K - 1):
Count[A[i]] = Count.get(A[i], 0) + 1
Myset = dict()
for x in Count:
if (Count[x] == 1):
Myset[x] = 1
# Before every iteration of this loop,
# we maintain that K-1 elements of current
# window are processed.
for i in range(K - 1, N):
# Process K-th element of current window
Count[A[i]] = Count.get(A[i], 0) + 1
if (Count[A[i]] == 1):
Myset[A[i]] = 1
else:
del Myset[A[i]]
# If there are no distinct
# elements in current window
if (len(Myset) == 0):
print("Nothing")
# Set is ordered and last element
# of set gives us maximum element.
else:
maxm = -10**9
for i in Myset:
maxm = max(i, maxm)
print(maxm)
# Remove first element of current
# window before next iteration.
x = A[i - K + 1]
if x in Count.keys():
Count[x] -= 1
if (Count[x] == 1):
Myset[x] = 1
if (Count[x] == 0):
del Myset[x]
# Driver code
a = [1, 2, 2, 3, 3 ]
n = len(a)
k = 3
find_max(a, n, k)
# This code is contributed
# by mohit kumar
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void find_max(int[] A, int N, int K)
{
// Storing counts of first K-1 elements
// Also storing distinct elements.
Dictionary<int, int> count = new Dictionary<int, int>();
for (int i = 0; i < K - 1; i++)
{
if(count.ContainsKey(A[i]))
{
count[A[i]]++;
}
else
{
count.Add(A[i], 1);
}
}
HashSet<int> Myset = new HashSet<int>();
foreach(KeyValuePair<int, int> x in count)
{
if(x.Value == 1)
{
Myset.Add(x.Key);
}
}
// Before every iteration of this loop,
// we maintain that K-1 elements of current
// window are processed.
for (int i = K - 1; i < N; i++)
{
// Process K-th element of current window
if (count.ContainsKey(A[i]))
{
count[A[i]]++;
}
else
{
count.Add(A[i], 1);
}
if(count[A[i]] == 1)
{
Myset.Add(A[i]);
}
else
{
Myset.Remove(A[i]);
}
// If there are no distinct
// elements in current window
if (Myset.Count == 0)
Console.Write("Nothing\n");
// Set is ordered and last element
// of set gives us maximum element.
else
{
List<int> myset = new List<int>(Myset);
Console.WriteLine(myset[myset.Count - 1]);
}
// Remove first element of current
// window before next iteration.
int x = A[i - K + 1];
count[x]--;
if(count[x] == 1)
{
Myset.Add(x);
}
if(count[x] == 0)
{
Myset.Remove(x);
}
}
}
// Driver code
static public void Main ()
{
int[] a = { 1, 2, 2, 3, 3 };
int n=a.Length;
int k = 3;
find_max(a, n, k);
}
}
// This code is contributed by rag2127
JavaScript
<script>
// Javascript code to calculate maximum unique
// element of every segment of array
function find_max(A,N,K)
{
// Storing counts of first K-1 elements
// Also storing distinct elements.
let Count = new Map();
for (let i = 0; i < K - 1; i++)
if (Count.has(A[i]))
Count.set(A[i], 1 + Count.get(A[i]));
else
Count.set(A[i], 1);
let Myset = new Set();
for (let [key, value] of Count.entries()) {
if (value==1)
Myset.add(key);
}
// Before every iteration of this loop,
// we maintain that K-1 elements of current
// window are processed.
for (let i = K - 1; i < N; i++) {
// Process K-th element of current window
if (Count.has(A[i]))
Count.set(A[i], 1 + Count.get(A[i]));
else
Count.set(A[i], 1);
if ((Count.get(A[i])) == 1)
Myset.add(A[i]);
else
Myset.delete(A[i]);
// If there are no distinct
// elements in current window
if (Myset.size == 0)
document.write("Nothing<br>");
// Set is ordered and last element
// of set gives us maximum element.
else
document.write(Array.from(Myset)[Myset.size-1]+"<br>");
// Remove first element of current
// window before next iteration.
let x = A[i - K + 1];
Count.set(x, Count.get(x) - 1);
if (Count.get(x) == 1)
Myset.add(x);
if (Count.get(x) == 0)
Myset.delete(x);
}
}
// Driver code
let a=[1, 2, 2, 3, 3];
let n = a.length;
let k = 3;
find_max(a, n, k);
// This code is contributed by unknown2108
</script>
Time Complexity: O(N Log K)
Auxiliary Space: O(N)
Similar Reads
Binary Search Tree
A Binary Search Tree (or BST) is a data structure used in computer science for organizing and storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right chi
3 min read
Introduction to Binary Search Tree
Binary Search Tree is a data structure used in computer science for organizing and storing data in a sorted manner. Binary search tree follows all properties of binary tree and for every nodes, its left subtree contains values less than the node and the right subtree contains values greater than the
3 min read
Applications of BST
Binary Search Tree (BST) is a data structure that is commonly used to implement efficient searching, insertion, and deletion operations along with maintaining sorted sequence of data. Please remember the following properties of BSTs before moving forward.The left subtree of a node contains only node
3 min read
Applications, Advantages and Disadvantages of Binary Search Tree
A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the p
2 min read
Insertion in Binary Search Tree (BST)
Given a BST, the task is to insert a new node in this BST.Example: How to Insert a value in a Binary Search Tree:A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node. Once a leaf node is fo
15 min read
Searching in Binary Search Tree (BST)
Given a BST, the task is to search a node in this BST. For searching a value in BST, consider it as a sorted array. Now we can easily perform search operation in BST using Binary Search Algorithm. Input: Root of the below BST Output: TrueExplanation: 8 is present in the BST as right child of rootInp
7 min read
Deletion in Binary Search Tree (BST)
Given a BST, the task is to delete a node in this BST, which can be broken down into 3 scenarios:Case 1. Delete a Leaf Node in BSTDeletion in BSTCase 2. Delete a Node with Single Child in BSTDeleting a single child node is also simple in BST. Copy the child to the node and delete the node. Deletion
10 min read
Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order
Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1
10 min read
Balance a Binary Search Tree
Given a BST (Binary Search Tree) that may be unbalanced, the task is to convert it into a balanced BST that has the minimum possible height.Examples: Input: Output: Explanation: The above unbalanced BST is converted to balanced with the minimum possible height.Input: Output: Explanation: The above u
10 min read
Self-Balancing Binary Search Trees
Self-Balancing Binary Search Trees are height-balanced binary search trees that automatically keep the height as small as possible when insertion and deletion operations are performed on the tree. The height is typically maintained in order of logN so that all operations take O(logN) time on average
4 min read