Greatest contiguous sub | C # implementation of the approach ; 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
using System ; using System . Collections . Generic ; using System . Linq ; public class GFG { static List < int > findSubarray ( int [ ] a , int k , int n ) { List < List < int > > vec = new List < List < int > > ( ) ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { List < int > temp = new List < int > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { temp . Add ( a [ j ] ) ; } vec . Add ( temp ) ; } vec . OrderBy ( l => l [ 0 ] ) ; return vec [ vec . Count - 1 ] ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 3 , 2 , 5 } ; int k = 4 ; int n = a . Length ; List < int > ans = findSubarray ( a , k , n ) ; foreach ( int it in ans ) { Console . Write ( it + " ▁ " ) ; } } }