Home > Mobile >  How to go through a 2D array and find the index of specific sequence?
How to go through a 2D array and find the index of specific sequence?

Time:11-26

I am trying to go through a 2D array and find the longest period in which temperatures are above a given K temperature.

For example:
Input
11 10 30
1 2 3 4 5 6 7 8 9 10
34 34 30 10 34 34 10 10 10 10
35 35 35 3 35 35 3 3 3 3
11 11 11 11 11 11 11 11 11 11
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 39 4 5 6 7 8 9 10
3 3 3 3 3 3 35 35 35 35

Output: 11

Where:
11 is the number of rows (count of settlements)
10 is the number of columns (count of days)
30 is the limited temperature

And If there is more than one solution, the output should be the smallest index. If there is no solution output must be -1.

Getting the data and storing them in the array, is working fine. But I am having trouble setting up the condition.

Using this code, is giving me the number 3 back.

I apologise if this is the wrong format to ask a question, I am pretty new to coding.


using System;
internal class Program
{
    private static void Main(string[] args)
    {
        string input = Console.ReadLine();

        int cntSettlements = Convert.ToInt32(input.Split(" ")[0]);
        int cntDay = Convert.ToInt32(input.Split(" ")[1]);
        int tempLimit = Convert.ToInt32(input.Split(" ")[2]);

        int[,] dataIn = new int[cntSettlements, cntDay];

        for (int i = 0; i < cntSettlements; i  )
        {
            string input1 = Console.ReadLine();

            for (int j = 0; j < cntDay; j  )
            {
                dataIn[i, j] = Convert.ToInt32(input1.Split(" ")[j]);
            }
        }

        int longestTempAboveLimitCount = 0;
        int longestTempAboveLimitRowIndex = -1;

        for (int i = 0; i < cntSettlements; i  )
        {
            int currentTempAboveLimitCount = 0;

            for (int j = 0; j < cntDay; j  )
            { 
                if (dataIn[i, j] > tempLimit)
                {
                    currentTempAboveLimitCount  ;
                }
            }
            if (currentTempAboveLimitCount > longestTempAboveLimitCount)
            {
                longestTempAboveLimitCount = currentTempAboveLimitCount;
                longestTempAboveLimitRowIndex = i   1;
            }
        }
        Console.WriteLine(longestTempAboveLimitRowIndex);
    }
}
   

CodePudding user response:

You need something like this:

//current maximum and index of current maximum
int longestTempAboveLimitCount = 0;       
int longestTempAboveLimitRowIndex = -1;

for (int i = 0; i < cntSettlements; i  )  
{
   //current row counter
   int  currentTempAboveLimitCount = 0;   

   for (int j = 0; j < cntDay; j  )
   {
      //criterium
      if (data[i, j] > limitemp)          
      {
         currentTempAboveLimitCount  ;
      }
   }
   
   //comparing and adjusting the current maximum
   if (currentTempAboveLimitCount > longestTempAboveLimitCount)  
   {
      longestTempAboveLimitCount = currentTempAboveLimitCount;
      longestTempAboveLimitRowIndex = i;
   }
}

Console.WriteLine(longestTempAboveLimitRowIndex);
  • Related