I feel too detailed in solving the problem. Is this something good considering I am a beginner programmer? Are there certain limitations in solving problems in the world of programming? I give an example like this:
Write a program, which finds the maximal sequence of consecutively placed increasing integers. Example: {3, 2, 3, 4, 2, 2, 4} => {2, 3, 4}.
It looks easy if there is only one result. What if there is more than one result? For example: {1, 2, 3, 2, 1, 5, 6} => {1, 2, 3} and {1, 5, 6}.
After hours, finally I can solve it.
int[] array = new int[] { -2, -1, 3, 1, 6, 5, 6, 7, 1, 12, 13 };
int[] value = new int[array.Length];
int counter = 1, maxCounter = 1;
Console.WriteLine("Array:");
for (int i = 0; i < array.Length; i++)
{
Console.Write($"{array[i]}");
Console.Write(i != array.Length - 1 ? ", " : "");
}
ResetValue(value);
Console.WriteLine("nnThe maximal sequence of consecutive increasing elements in an array:");
for (int i = 1; i < array.Length; i++)
{
if (array[i] > array[i - 1])
{
counter++;
if (i != array.Length - 1)
{
continue;
}
}
if (counter >= maxCounter)
{
if (counter > maxCounter)
{
ResetValue(value);
maxCounter = counter;
}
if (i == array.Length - 1 && array[i] > array[i - 1])
{
counter = Result(array, value, maxCounter, i + 1);
}
else
{
counter = Result(array, value, maxCounter, i);
}
}
else
{
counter = 1;
}
}
int count = 0;
for (int i = 0; i < value.Length; i++)
{
if (value[i] == int.MinValue)
{
continue;
}
else
{
Console.Write($"{value[i]}");
count++;
Console.Write(count != maxCounter ? ", " : "");
if (count == maxCounter)
{
Console.WriteLine();
count = 0;
}
}
}
static void ResetValue(int[] value)
{
for (int i = 0; i < value.Length; i++)
{
value[i] = int.MinValue;
}
}
static int Result(int[] array, int[] value, int maxCounter, int i)
{
int counter;
for (int j = i - maxCounter; j < i; j++)
{
value[j] = array[j];
}
counter = 1;
return counter;
}
Hanef Samsudin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1