Given an array arr[] consisting of N integers from the range [1, N]( repetition allowed ), the task is to find minimum common element for each possible subarray lengths. If no such element exists for any particular length of subarray, then print -1.
Examples:
Input: arr[] = {1, 3, 4, 5, 6, 7}
Output: -1 -1 -1 4 3 1
Explanation:
K = 1: No common element exists. Therefore, print -1.
K = 2: No common element exists. Therefore, print -1.
K = 3: No common element exists. Therefore, print -1.
K = 4: Since 4 is common in all subarrays of size 4, print 4.
K = 5: Since 3 and 4 is common in all subarrays of size 5, print 3 as it is the minimum.
K = 6: Print 1 as it is the minimum element in the array.Input: arr[]: {1, 2, 2, 2, 1}
Output: -1 2 2 1 1
Approach: Follow the steps below to solve the problem:
- Traverse the array and store the last occurrence of every element in a Map.
- Initialize an array temp[] and store in it for each value, the maximum distance between any pair of consecutive repetitions of it in the array.
- Oonce the above step is completed, update temp[] by comparing temp[i] with the distance of last occurrence of i from the end of the array.
- Now, store the minimum comment element for all subarrays of length 1 to N one by one and print them.
Below is the implementation of the above approach:
C++
// C++ Program to implement the // above approach #include <bits/stdc++.h> using namespace std; // Function to find maximum distance // between every two element void max_distance(int a[], int temp[], int n) { // Stores index of last occurence // of each array element map<int, int> mp; // Initialize temp[] with -1 for (int i = 1; i <= n; i++) { temp[i] = -1; } // Traverse the array for (int i = 0; i < n; i++) { // If array element has // not occurred previously if (mp.find(a[i]) == mp.end()) // Update index in temp temp[a[i]] = i + 1; // Otherwise else // Compare temp[a[i]] with distance // from its previous occurence and // store the maximum temp[a[i]] = max(temp[a[i]], i - mp[a[i]]); mp[a[i]] = i; } for (int i = 1; i <= n; i++) { // Compare temp[i] with distance // of its last occurence from the end // of the array and store the maximum if (temp[i] != -1) temp[i] = max(temp[i], n - mp[i]); } } // Function to find the minimum common // element in subarrays of all possible lengths void min_comm_ele(int a[], int ans[], int temp[], int n) { // Function call to find a the maximum // distance between every pair of repetition max_distance(a, temp, n); // Initialize ans[] to -1 for (int i = 1; i <= n; i++) { ans[i] = -1; } for (int i = 1; i <= n; i++) { // Check if subarray of length // temp[i] contains i as one // of the common elements if (ans[temp[i]] == -1) ans[temp[i]] = i; } for (int i = 1; i <= n; i++) { // Find the minimum of all // common elements if (i > 1 && ans[i - 1] != -1) { if (ans[i] == -1) ans[i] = ans[i - 1]; else ans[i] = min(ans[i], ans[i - 1]); } cout << ans[i] << " "; } } // Driver Code int main() { int N = 6; int a[] = { 1, 3, 4, 5, 6, 7 }; int temp[100], ans[100]; min_comm_ele(a, ans, temp, N); return 0; } |
-1 -1 -1 4 3 1
Time Complexity: O(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.
Recommended Posts:
- Maximum sum of lengths of non-overlapping subarrays with k as the max element.
- Minimum common element in all subarrays of size K
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Min difference between maximum and minimum element in all Y size subarrays
- Sum of minimum element of all subarrays of a sorted array
- Minimum cost required to convert all Subarrays of size K to a single element
- Minimum LCM and GCD possible among all possible sub-arrays
- Count of contiguous subarrays possible for every index by including the element at that index
- Count of all possible bitonic subarrays
- Count of all possible reverse bitonic subarrays
- Find all possible subarrays having product less than or equal to K
- Sum of products of all possible Subarrays
- Count of common subarrays in two different permutations of 1 to N
- Root to leaf paths having equal lengths in a Binary Tree
- Find middle point segment from given segment lengths
- Reach the numbers by making jumps of two given lengths
- Split the array into odd number of segments of odd lengths
- Sort an array of strings according to string lengths using Map
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

