Wave print coloumn wise


when i am running the code …it is giving me correct output…but when when i am submitting it…it is showing wrong answer in all the test cases…whats the issue in my code??..

Your code will not print complete output for a rectangular matrix. This is because your while loop will only run min(rows, columns) times. For example : for matrix
2 6
11 12 13 41 42 43
21 22 23 31 32 33

Your while loop will run only twice and hence print only 11, 21, 22, 12, END

To overcome this problem, you can keep a count variable initialized with zero and increment it everytime an element is printed, And you can run your loop till count is less than total number of elements

while(count<arr.length*arr[0].length)

thanks…the code works now…but i am still unable to understand…why it didnt worked earlier for rectangular cases…

It was happening because your while loop was like this

while(i<arr.length && j<arr[i].length)

Now here if the number of columns is 6 and the number of rows is 2, the loop will break after ‘i’ becomes equal to 2, and the rest of the pattern would not be printed, i.e only 2 columns would be printed.