Smallest subarray which upon repetition gives the original array | Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call
function printArray ( brr ) { for ( let it of brr ) { document . write ( it + ' ' ) ; } } function RepeatingSubarray ( arr , N ) { if ( N < 2 ) { document . write ( " " ) ; } let brr = [ ] ; brr . push ( arr [ 0 ] ) ; brr . push ( arr [ 1 ] ) ; for ( let i = 2 ; i < Math . floor ( N / 2 ) + 1 ; i ++ ) { if ( N % i == 0 ) { let a = false ; let n = brr . length ; let j = i ; while ( j < N ) { let K = j % i ; if ( arr [ j ] == brr [ K ] ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . push ( arr [ i ] ) ; } document . write ( " " ) ; return ; } let arr = [ 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 ] ; let N = arr . length ; RepeatingSubarray ( arr , N ) ;