I have the following case :
a list or array .the size is a variable i get from the user between 1 to 12 .
if the size is 3 then the array {1,2,3}
if the size is 5 then the array {1,2,3,4,5}
and so on
Now the beginning is a variable also.
the sequence i wanna to get is :
if the size 12 for example ,and the beginning is 9 for example
i wanna the following result with this specific order.
9,10,11,12,1,2,3,4,5,6,7,8
I mean i begin with the given beginning until the last item then if the beginning not 1 then i continue with 1 until the beginning.
I did that but it was specific to the size 12:
with this code :
int[] arr = new int[12];
int month = 9;//input from the user
List<int> source = new List<int>();
while (month <= 12)
{
source.Add(month);
month++;
}
if (source.Count < 12)
{
for (int i = 1; i < source[0]; i++)
{
source.Add(i);
}
}
I wanna more general solution to allow variable size not just 12
I have tested it and it works
Please add some conditions like array size should not be less than month and user always input integer and do it in try catch for good practice… etc etc
After using some logic from other answers i think below code is much better.
The second method has less complexity only O (n) because it uses only one loop rather 2.
Third Solution is even more simpler 🙂
hope it helps.