

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Unique Element in every subarray of size K in c++
In this problem, we are given an array of integers and a integers K. our task is to create a program that will find the maximum unique element in every subarray of size K with no duplicates.
Let’s take an example to understand the problem,
Input −
array = {4, 1, 1, 3, 3} k = 3
Output −
4 3 1
Explanation −
Sub-array of size 3 Subarray {4, 1, 1}, max = 4 Subarray {1, 1, 3}, max = 3 Subarray {1, 3, 3}, max = 1
To solve this problem, one simple method is to run two loops and create subarrays and find distinct elements and print a maximum of them.
But an effective solution is to use a sliding window method using a hash table and self-balancing BST to find maximum elements.
So, we will traverse the array and for k length summary store the counts of elements in hash table and store the elements in a set (which will store only unique elements). And the print the max of the set and do the same for every iteration in the array.
Example
Program to illustrate the working of our solution,
#include <bits/stdc++.h> using namespace std; void maxUniqueSubArrayElement(int A[], int N, int K){ map<int, int> eleCount; for (int i = 0; i < K - 1; i++) eleCount[A[i]]++; set<int> uniqueMax; for (auto x : eleCount) if (x.second == 1) uniqueMax.insert(x.first); for (int i = K - 1; i < N; i++) { eleCount[A[i]]++; if (eleCount[A[i]] == 1) uniqueMax.insert(A[i]); else uniqueMax.erase(A[i]); if (uniqueMax.size() == 0) cout<<"-1\t" ; else cout<<*uniqueMax.rbegin()<<"\t"; int x = A[i - K + 1]; eleCount[x]--; if (eleCount[x] == 1) uniqueMax.insert(x); if (eleCount[x] == 0) uniqueMax.erase(x); } } int main(){ int a[] = { 4, 3, 2, 2, 3, 5}; int n = sizeof(a) / sizeof(a[0]); int k = 4; cout<<"The maximum unique element for a subarray of size "<<k<<" is\n"; maxUniqueSubArrayElement(a, n, k); return 0; }
Output
The maximum unique element for a subarray of size 4 is 4 -1 5
- Related Questions & Answers
- Maximum Size Subarray Sum Equals k in C++
- Find maximum (or minimum) sum of a subarray of size k in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++ program
- Find maximum average subarray of k length in C++
- Maximum product of subsequence of size k in C++
- First negative integer in every window of size k in C++
- C++ program to find maximum of each k sized contiguous subarray
- Python – Maximum of K element in other list
- Maximum of all Subarrays of size k using set in C++ STL
- Maximum Subarray in Python
- Maximum size of a <canvas> element in HTML
- Maximum sum subarray removing at most one element in C++
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Maximum subarray sum by flipping signs of at most K array elements in C++