0
\$\begingroup\$

To find the row with max zero Matrix will have either 0 or 1 in sorted manner.

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[][] inp=new int[][]{
                            {0,0,0,1,1},
                            {0,1,1,1,1},
                            {0,0,1,1,1},
                            {0,0,0,0,1}
                            };
    int j=0,rownum=0;       
    for(int i=0;i<4;i++){
        while(j<=4&&inp[i][j]!=1){
            rownum=i;
            j++;
        }
    }
    System.out.println("Row "+(rownum+1)+" has Max zero");

}

Complexity of the above program is \$O(m+n)\$ where \$m\$ is row size and \$n\$ is column size.

If any better approach is there then please post in this

\$\endgroup\$
1
\$\begingroup\$

I do not think you can do something to reduce Complexity in any traditional way. You have to traverse all array elements in order to find the row with the max element,so m x n iterations will give you O(m+n) Complexity.

\$\endgroup\$
  • \$\begingroup\$ reversing is additional operation. I have already written program with O(m+n). I wanted to find any other way or approach with minimal complexity than O(m+n) \$\endgroup\$ – Mohan Raj May 9 '15 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.