GeeksforGeeks » Interview Questions

Neither Max nor Min

(5 posts)
  • Started 10 months ago by darkprince
  • Latest reply from srikantaggarwal
  1. darkprince
    Member
    Posted 10 months ago #

    Suppose a double Dimension array is given of order N x N . And it is filled with distinct elements . Now you have to find the atleast one row which neither contain the maximum or the minimum element among all the elements present . The runtime complexity should be less than O(N^2) .

  2. rajcools
    Member
    Posted 10 months ago #

    somehow i am confused here maybe thinking too much ---

    but when we traverse a N*N array why is complexity taken as O(n^2) arent we just visiting the element once so shouldnt it be O(n) ----n here number of elements is n = N*N?
    geeks

  3. abc
    guest
    Posted 10 months ago #

    Consider a Set S consisting of any three rows R1, R2, and R3, and
    compute the min and max of the elements in these rows.

    Since all elements are distinct, at most one the three rows
    will contain minimum (say R1), and at most one row will
    contain maximum (say R2). This means R3 contains neither
    max, nor min..

    We do not need to look at other rows, as inclusion of other
    rows in S, will not make R3 contain max or min.

  4. k53
    Member
    Posted 9 months ago #

    All the N^2 elements need to be traversed to find min and max elements. Should we consider the runtime complexity (less than N^2) only for identifying the required row (without considering time to find max and min elements) ?

  5. srikantaggarwal
    Member
    Posted 6 months ago #

    Hi,

    I think we need to go till the max of the row is less than the total_max, and min is greater than total_min till now.

    int total_max = 0, total_min = 0;

    for(int i = 0; i < N; i++)
    {
    int max = 0, min = 0;

    for(int j = 0; j < N; j++)
    {
    int el = A[i][j];
    if(el < min)
    min = el;
    else if(el > max)
    max = el;
    }
    if(min < total_min)
    total_min = min;
    else if(max > total_max)
    total_max = max;
    else
    {
    printf("Row is %d", i);
    break;
    }
    }


Reply

You must log in to post.

RSS feed for this topic