Help me in solving MATNEGCOUNT problem

My issue

why i cant solve the problem, what is the problem with my loops

My code

import java.util.Scanner;

public class Main {
    public static int countNegatives(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int count = 0;
        
        for(i=0; i<rows; i++) {
            for(j = cols-1; j>=0; j--) {
                if (mat[i][j] < 0) 
                count++;
                else
                break;
            }
        }
        
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[][] mat = new int[n][m];
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                mat[i][j] = scanner.nextInt();
            }
        }

        System.out.println(count(mat));
    }
}

Learning course: Matrices
Problem Link: Count Negative Numbers Practice Problem in Matrices - CodeChef

You can refer this solution:

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
public static int countNegatives(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int count = 0;

    for(int i=0; i<rows; i++) {
        for(int j = cols-1; j>=0; j--) {
            if (matrix[i][j] < 0) 
            count++;
            else
            break;
        }
    }

    return count;
}
public static void main (String[] args) throws java.lang.Exception
{
	// your code goes here
	Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    int m = scanner.nextInt();

    int[][] mat = new int[n][m];

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            mat[i][j] = scanner.nextInt();
        }
    }

    System.out.println(countNegatives(mat));
}

}

1 Like

hey friend, there are few mistakes in your code , just take a look at this solution and your doubts will be cleared.

import java.util.Scanner;

public class Main {
    public static int countNegatives(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int count = 0;
        
        for(int i=0; i<rows; i++) {
            for(int j = cols-1; j>=0; j--) {
                if (matrix[i][j] < 0) 
                count++;
                else
                break;
            }
        }
        
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[][] mat = new int[n][m];
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                mat[i][j] = scanner.nextInt();
            }
        }

        System.out.println(countNegatives(mat));
    }
}
  1. declare data types of i and j .
    2)inside countNegative function use “matrix” instead of mat.
    3)in final output line use countNegative instead of count.