-2

help me find the error in my problem.

A very famous artist in certain circles, Ignat, painted one picture every day for N days. On the i day, Ignat painted a picture with bright colors ai.

Now Ignat wants to choose the most contrasting pair from N resulting pictures. Pair of paintings (i,j) drawn on days i and j, respectively, is called the most contrasting if the value (ai−aj) is maximal among all possible pairs.

Input data The first line contains an integer N — the number of days. The second line contains integers a1,a2,...,aN, where ai — the brightness of the picture drawn on the i day.

Output Print distinct integers i and j are the numbers of the days on which Ignat painted the most contrasting pair of pictures (i, j). The numbers i and j must lie between 1 and N. If there are several such pairs, print the pair with the maximum value of i−j. Note that the pair (i,j) differs from the pair (j,i). Input example: 6

1 2 1 3 1 3 Output: 6 1


Input example: 4

2 1 0 -1 Output: 1 4

My code:

int n = int.Parse(Console.ReadLine());
int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int maxIndex = 0, minIndex = 0;
for (int i = 1; i < n; i++)
{
    if (a[i] > a[maxIndex])
    {
        maxIndex = i;
    }
    if (a[i] < a[minIndex])
    {
        minIndex = i;
    }
}
if (maxIndex < minIndex)
{
    Console.WriteLine((maxIndex + 1) + " " + (minIndex + 1));
}
else
{
    Console.WriteLine((minIndex + 1) + " " + (maxIndex + 1));
}

I always get 1 4, thank u very much

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • To fulfill both your conditions, ie `a[i] - a[j]` is maximum and `i - j` is maximum, you have to find the *last* of multiple maximum values in your array and the *first* of multiple minumum values. And you must always output the index of the maximum element first and the index of the minimum element second – derpirscher Mar 22 '23 at 08:29
  • 1
    If you expect your `maxIndex` to be 6, you could for instance [use a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code and check, why it doesn't update the `maxIndex` variable when you are at index `i= 5` in your loop ... – derpirscher Mar 22 '23 at 09:16

1 Answers1

0

As far as I can see, you want min and max values indexes (in case of tie min index must be leftmost while max index rightmost):

private static (int leftIndex, int rightIndex) Solve(int[] array) {
  if (array is null || array.Length <= 0)
    return (-1, -1);

  int minIndex = 0;
  int min = array[minIndex];
  int maxIndex = array.Length - 1;
  int max = array[minIndex];

  for (int i = 0; i < array.Length; ++i) {
    // Note `<` 
    if (array[i] < min) {
      min = array[i];
      minIndex = i;
    }

    // Note `>=`
    if (array[i] >= max) {
      max = array[i];
      maxIndex = i;      
    }
  }

  return (maxIndex + 1, minIndex + 1);      
}

Usage

int n = int.Parse(Console.ReadLine());
int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);

var result = Solve(a);

Console.Write($"{result.leftIndex} {result.rightIndex}");

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215