Finding local maxima in a 2D array

前端 未结 7 2082
生来不讨喜
生来不讨喜 2021-02-03 12:39

A local maximum in a 2D array can be defined as a value such that all it\'s 4 neighbours are less than or equal to it, ie, for a[i][j] to be a local maximum,

7条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 13:26

    You are given an array of size 4 x 4. You will need to do the following task.

    1. Fill the array with some random numbers using rand() function.

    2. For each cell (i,j) your will need to find the cell with maximum number among all the possible neighbors the cell (i,j) has.

    3. Then put that maximum number in the that cell (i,j)

    Sample Input:

    177 -90 12 7
    1 34 43 67
    11 11 122 45
    6 90 98 93
    

    Sample Output:

    34 177 67 67
    177 177 122 122
    90 122 98 122
    90 98 122 122
    

提交回复
热议问题