struggling struggling - 1 month ago 9
C# Question

How to create Monolithic lists from array in increasing order (sub-Lists)

I have to create a monolithic list from an array in increasing order in c#. I was given

[1 2 4 7 5 6 3 2]
as test input whose test output is
[[1 2 4 7] [5 6] [3] [2]]
. The thing is i am not at all able to understand what question is and what it is asking me to do so that i start.

Could some one please me what is monolithic mean in current scenario of array ? and how these 4 array output produced from the input array ?

Edit I have understood how these sublists are created upon input array, they are created when
input[i]>input[i+1]
. But how to code such dynamic sublists inside lists ?

Answer

This is the old fashioned way:

List<int> sublist=new List<int>();
int last=Int32.MinValue;
List<int[]> list=new List<int[]>();
foreach(var i in input)
{
   if(i<last) 
   {
     list.Add(sublist.ToArray());
     sublist=new List<int>();
   }
   last=i;
   sublist.Add(i);
}
list.Add(sublist.ToArray());
var answer=list.ToArray();

The newer way would be to return an IEnumerable<IEnumerable<int>> that is traversed only when enumerated, and is much better, but I'll leave that as an exercise for you or someone else to do.