Home > Net >  c# Find the Longest Continuous Subarrays first and last elements Indexes
c# Find the Longest Continuous Subarrays first and last elements Indexes

Time:11-26

In the inputs first row, there are two numbers the first one is the amount of rows N and the second one is a limit K. I have to find the first and last element's indexes of the longest continuous subarray which's elements are greater than K.

(There are lots of inputs with different numbers, but they have the same base.)

The example input is:

7 20
18
23
44
32
9
30
26

So the N is 7 and K is 20, in this case there are 2 continuous subarrays which would be correct: [23, 44, 32] and [30, 26], but I only need the longer ones indexes.

Therefore the output is:

1 3

I have split the first row, so i have the N and K, I have added the remaining rows in an array H[ ]. Now I just have to find the longest continuous subarray and get the first and last element's indexes.

static void Main(string[] args)
        {
            string[] fRow = Console.ReadLine().Split(' ');
            int N = int.Parse(fRow[0]);
            int K = int.Parse(fRow[1]);
            int[] H = new int[N];
            for (int i = 0; i < N; i  )
            {
                H[i] = int.Parse(Console.ReadLine());
            }
        }

And I'm stuck here, if someone could help me I would greatly appreciate their assistance.

CodePudding user response:

Sounds like homework, but an interesting challenge none the less. Here's one way of doing it.

static void Main(string[] args)
{
    string[] fRow = Console.ReadLine().Split(' ');
    int N = int.Parse(fRow[0]);
    int K = int.Parse(fRow[1]);
    int[] H = new int[N];
    
    for (int i = 0; i < N; i  )
    {
        H[i] = int.Parse(Console.ReadLine());
    }

    int greatesRangeStartIndex = -1;
    int greatestRangeEndIndex = -1;
    int greatestIndexSpan = 0;
    
    for (int i = 0; i < N; i  )
    {
        // Find the first array item that meets the criteria.
        if (H[i] > K)
        {
            var rangeStartIndex = i;

            // Continue spinning through the array while we still meet the criteria.
            do
            {
                i  ;
            } while (i < N && H[i] > K);

            var rangeEndIndex = i - 1;
            
            // Determine the width of our current range and check if its our largest one.
            // If the range is the biggest so far, store that as the current largest range.
            var indexSpan = rangeEndIndex - rangeStartIndex   1;
            if (indexSpan > greatestIndexSpan)
            {
                greatesRangeStartIndex = rangeStartIndex;
                greatestRangeEndIndex = rangeEndIndex;
                greatestIndexSpan = indexSpan;
            }
        }
    }

    // Report out the results.
    // Not part of the requirements, but will remove false reporting of the criteria being in index position 1.
    if (greatesRangeStartIndex == -1 && greatestRangeEndIndex == -1)
    {
        Console.WriteLine($"No values in the array were greater than {K}.");
    }
    else
    {
        Console.WriteLine($"{greatesRangeStartIndex} {greatestRangeEndIndex}");
    }
}

CodePudding user response:

You could do something like this (could be improved a lot with LINQ, but i suppose this is an introduction exercise of some sorts, so i'll stick to this):

        static void Main(string[] args)
        {
            string[] fRow = Console.ReadLine().Split(' ');
            int N = int.Parse(fRow[0]);
            int K = int.Parse(fRow[1]);
            int[] H = new int[N];
            int firstIndex = 0;
            int lastIndex = 0;
            int subarraySize = 0;

            int firstIndexTemp = 0;
            int lastIndexTemp = 0;
            int subarraySizeTemp = 0;
            bool arrayContinues = false;
            for (int i = 0; i < N; i  )
            {
                //Read the newest index
                H[i] = int.Parse(Console.ReadLine());
                /*If arrrayContinues is true, and the current value is higher than the threshold K,
                  this means this is the continuation of a subarray. For now, the current value is the last index value
                  */
                if (H[i] > K && arrayContinues)
                {
                    subarraySizeTemp  ;
                    lastIndexTemp = i;
                }
                /*If arrrayContinues is false, but the current value is higher than the threshold K,
                  this means this is the first index of a new subarray
                  */
                else if (H[i] > K)
                {
                    subarraySizeTemp = 1;
                    firstIndexTemp = i;
                    arrayContinues = true;
                }
                /*If we reach this statement, the current value is smaller than K,
                 * so the array streak stopped (or was already stopped by a previous smaller value)
                 */
                else
                {
                    arrayContinues = false;
                }

                /* We're only interested in the largest subarray,
                 * so let's override the previous largest array only when the current one is larger.
                 */
                if(subarraySizeTemp > subarraySize)
                {
                    subarraySize = subarraySizeTemp;
                    firstIndex = firstIndexTemp;
                    lastIndex = lastIndexTemp;
                }

            }
            /*Let's print our result!*/
            Console.WriteLine($"{firstIndex} {lastIndex}");
        }

CodePudding user response:

Other answers work out but you can use something simpler like this;

private static void Main()
{
    var input = Console.ReadLine().Split(' ');
    var n = int.Parse(input[0]);
    var k = int.Parse(input[1]);

    var startingIndex = 0;
    var endingIndex = 0;

    var temporaryIndex = 0;

    var items = new int[n];

    for (var i = 0; i < n; i  )
    {
        var value = int.Parse(Console.ReadLine());
        items[i] = value;

        if (value < k)
        {
            temporaryIndex = i;
            continue;
        }

        var currentSize = i - temporaryIndex;
        var currentBiggestSize = endingIndex - startingIndex;

        if (currentSize > currentBiggestSize)
        {
            startingIndex = temporaryIndex   1;
            endingIndex = i;
        }
    }

    Console.WriteLine($"Biggest Subset's Start and Ending Indexes: {startingIndex} {endingIndex}");
    Console.ReadLine();
}

CodePudding user response:

Another Option:

static void Main(string[] args)
{
    Example(new string[] { "7","20"},new string[] { "18", "23", "44", "32", "9", "30", "26"});
}

static void Example(string[] arr,string[] values)
{
    int N = int.Parse(arr[0]);
    int K = int.Parse(arr[1]);

    int counter = 0;
    int most_succesfull_index_yet = 0;
    int most_succesfull_length_yet = 0;
    for (int i = 1; i < N; i  )
    {
        if (int.Parse(values[i]) > K)
        {
            counter  ;
        }
        else
        {
            if (counter > most_succesfull_length_yet)
            {
                most_succesfull_length_yet = counter;
                most_succesfull_index_yet = i - counter;
            }

            counter = 0;
        }
    }

    // For last index
    if (counter > most_succesfull_length_yet)
    {
        most_succesfull_length_yet = counter;
        most_succesfull_index_yet = N - counter;
    }

    var bestStart = most_succesfull_index_yet;
    var bestEnd = most_succesfull_index_yet   most_succesfull_length_yet -1;

    Console.WriteLine(bestStart   ","   bestEnd);
    Console.ReadLine();
}

CodePudding user response:

Another solution, keeping indexes of the longest match, and displaying the max length, indexes, and rows values at the end.

        static void Main(string[] args)
        {
            var data = @"7 20
18
23
44
32
9
30
26";
            var rows = data.Split("\r\n");
            var frow = rows[0].Split(" ");
            int N = int.Parse(frow[0]);
            int K = int.Parse(frow[1]);

            int max = 0, currentmax = 0;
            int i = 1;
            int[] indexes = null;

            while(i < rows.Length)
            {
                if (int.Parse(rows[i]) > K)
                {
                    currentmax  ;
                }
                else
                {
                    if (currentmax > max)
                    {
                        max = currentmax;
                        indexes = new int[max];
                        indexes[--currentmax] = i;
                        do
                        {
                            indexes[--currentmax] = indexes[currentmax   1] - 1;
                        } while (currentmax > 0);

                        currentmax = 0;
                    }
                }
                i  ;
            }

            if (indexes != null) {
                Console.WriteLine($"{max} occured on indexes {string.Join(",", indexes)} with values {string.Join(",", indexes.Select(i => rows[i]).ToList())}");
            }
        }

CodePudding user response:

string[] fRow = Console.ReadLine().Split(' ');
int N = int.Parse(fRow[0]);
int K = int.Parse(fRow[1]);


bool isChain = false;
int currentFirstIndex = -1;
int maxFirstIndex = -1;
int currentLastIndex = -1;
int maxLastIndex = -1;
int currentLength = 0;
int maxLength = 0;

int[] H = new int[N];
for (int i = 0; i < N; i  )
{
   H[i] = int.Parse(Console.ReadLine());
   if(H[i] > K)
   {
      if (isChain)
      {
         currentLastIndex = i;
      }
      else
      {
         currentFirstIndex = i;
         isChain = true;
      }
      currentLength  ;
   }
   else
   {
       if (maxLength < currentLength)
       {
          maxLength = currentLength;
          maxFirstIndex = currentFirstIndex;
          maxLastIndex = currentLastIndex;
       }
                       
       currentLength = 0;
       isChain = false;

    }
}

Console.WriteLine("first: "   maxFirstIndex   "    last: "   maxLastIndex);

CodePudding user response:

This could be one of the way

    static void Main(string[] args)
    {
        var firstLineInput = Console.ReadLine().Split(" ");
        var numberOfInput = Int64.Parse(firstLineInput[0]);
        var value = Int64.Parse(firstLineInput[1]);

        var startIndex = -1; //No value greater than value
        var endIndex = -1;

        var maxLength = 0;
        var maxStartIndex = -1;
        var maxEndIndex = -1;

        for (int i = 0; i < numberOfInput ; i  )
        {
            var input = Int64.Parse(Console.ReadLine());
            
            if (input > value && startIndex == -1)
            {
                startIndex = i;
                endIndex = i;

                if(maxLength == 0) 
                {
                    maxLength = 1;
                    maxStartIndex = startIndex;
                    maxEndIndex = endIndex;
                }
            }
            else if(input > value && startIndex != -1) 
            {
                endIndex = i;
            }
            else if(input < value) 
            {
                startIndex = -1;
                endIndex = -1;
            }

            if (maxLength < (endIndex - startIndex))
            {
                maxLength = endIndex - startIndex;
                maxStartIndex = startIndex;
                maxEndIndex = endIndex;
            }
        }

        Console.WriteLine($"{maxStartIndex} {maxEndIndex}");
    }
  • Related