Greatest contiguous sub | Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
function findSubarray ( a , k , n ) { var vec = [ ] ; for ( var i = 0 ; i < n - k + 1 ; i ++ ) { var temp = [ ] ; for ( var j = i ; j < i + k ; j ++ ) { temp . push ( a [ j ] ) ; } vec . push ( temp ) ; } vec . sort ( ) return vec [ vec . length - 1 ] ; } var a = [ 1 , 4 , 3 , 2 , 5 ] ; var k = 4 ; var n = a . length ; var ans = findSubarray ( a , k , n ) ; ans . forEach ( it => { document . write ( it + " " ) } ) ;