Smallest element present in every subarray of all possible lengths | Java program for above approach ; Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths ; Initialize with - 1. ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Driver function ; Given array ; Size of array ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void printAnswer ( int answer [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( answer [ i ] + " ▁ " ) ; } } static void updateAnswerArray ( int answer [ ] , int N ) { int i = 0 ; while ( answer [ i ] == - 1 ) i ++ ; int minimum = answer [ i ] ; while ( i <= N ) { if ( answer [ i ] == - 1 ) answer [ i ] = minimum ; else answer [ i ] = Math . min ( minimum , answer [ i ] ) ; minimum = Math . min ( minimum , answer [ i ] ) ; i ++ ; } } static void lengthOfSubarray ( ArrayList < ArrayList < Integer > > indices , Set < Integer > st , int N ) { int [ ] answer = new int [ N + 1 ] ; Arrays . fill ( answer , - 1 ) ; Iterator itr = st . iterator ( ) ; while ( itr . hasNext ( ) ) { int start = - 1 ; int gap = - 1 ; int t = ( int ) itr . next ( ) ; indices . get ( t ) . add ( N ) ; for ( int i = 0 ; i < indices . get ( t ) . size ( ) ; i ++ ) { gap = Math . max ( gap , indices . get ( t ) . get ( i ) - start ) ; start = indices . get ( t ) . get ( i ) ; } if ( answer [ gap ] == - 1 ) answer [ gap ] = t ; } updateAnswerArray ( answer , N ) ; printAnswer ( answer , N ) ; } static void smallestPresentNumber ( int arr [ ] , int N ) { ArrayList < ArrayList < Integer > > indices = new ArrayList < > ( ) ; for ( int i = 0 ; i <= N ; i ++ ) indices . add ( new ArrayList < Integer > ( ) ) ; Set < Integer > elements = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { indices . get ( arr [ i ] ) . add ( i ) ; elements . add ( arr [ i ] ) ; } lengthOfSubarray ( indices , elements , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 } ; int N = arr . length ; smallestPresentNumber ( arr , N ) ; } }