max increase and decrease in arrays ?

I have an array that is
int arr[4][2]={{1, 3}, {5, 9}, {0,1}, {2, 0}};
and I am trying to get this code worked to find the max drop which is [1][1]
- [2,0] = 9 and min drop which is [3][0] - [3][1]=2 but my code seems to be wrong.
Can anyone see where I am doing wrong ?
Many thanks

#include <iostream>
#include <vector>
using namespace std;

int main()
int arr[4][2]={{1, 3}, {5, 9}, {0,1}, {8, 3}};

int max=arr[0][0], min=arr[0][0];

for(int i=0; i<4; i++)
for(int j=0; j<2; j++)
else if(arr[i][j]<min){min=arr[i][j];}
cout<<"\nmax= "<< max <<"\nmin= "<< min <<endl;
return 0;
You do find now the minimum and maximum element. Their difference is a "max drop". Note that in your example [3][1] is 0 just like [2][0], so there are two answers.

For the "min drop" you have to calculate differences between elements. Btw, the min drop is 0, and you have two such solutions: [0][0] to [2][1] and [2][0] to [3][1].
No. Try to think a discrete graph that starts from 1 then goes up to 3 then 5 then 9 then drops down to 0 so max drop is |9|=9.
Do you understand what I am trying to say ?
What are the edges of the graph? You did show part: 1-3-5-9-0, but what other paths are there? Are the edges directed?
int arr[4][2]={{1, 3}, {5, 9}, {0,1}, {2, 0}};

say t1=t2=......=t8=t, this is x axis
and for t1 y=1
for t2 y=3
for t8=0
so when 9 drops down to 0, its the greatest drop.
Is it clear this time ?

Basically, say arr[8]={1,3,5,9,0,2,0}
and the difference between 1 and 3 is 2, between 3 and 5 is 2, between 5 and 9 is 4 between 9 and 0 is 9 (which is the biggest gap).
So, essentially you do have a 1D array, that you just happen to have stored 2D.

A directed graph, where each node has at most one incoming and at most one outgoing edge. A singly linked list.

Now, you are interested in the difference between consecutive elements in the list. There is std::adjacent_difference(), which creates an array of differences. Then there is std::minmax_element() & Co.

You could copy the 2D into 1D, or you could create an iterator that advances the 2D array in column major order. The standard library algorithms take iterators.

The actual memory layout of 2D array in C/C++ is row-major, so the column major accesses are not "optimal".
Last edited on
Topic archived. No new replies allowed.