The Wayback Machine - https://web.archive.org/web/20240728063124/https://www.geeksforgeeks.org/java-program-to-determine-if-a-given-matrix-is-a-sparse-matrix/
Open In App

Java Program to Determine if a given Matrix is a Sparse Matrix

Last Updated : 14 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

A matrix is a collection of data elements arranged in a two-dimensional array-like structure, consisting of rows and columns. A matrix with a lot of zero values is called a sparse matrix. The program aims to count the number of zero values present in the matrix. If the number of zero values is more than half of the total number of elements in the matrix, then the matrix is considered sparse, and the output is “YES.” Otherwise, the matrix is not sparse, and the output is “NO.” This program determines whether a matrix is sparse or not by comparing the number of zero values to half of the total number of elements in the matrix.

Examples:

Input:  1 0 3
        0 0 4
        6 0 0
Output: Yes
Explanation:There are 5 zeros in the matrix which 
          is more than half of the matrix size.

Input:  1 2 3
        0 7 8
        5 0 7 
Output: No 
Explanation:There are 2 zeros in the matrix which 
             is less than half of the matrix size.

 

To check whether a matrix is a sparse matrix we only need to check the total number of elements that are equal to zero. If this count is more than (m * n)/2 then print Yes else No. There are two approaches to do so.

In this approach, we will check whether a matrix is a sparse matrix or not.

  1. Take the matrix.
  2. Traverse the matrix’s element one by one and counting the number of zeros.
  3. Checking if the count is more than (m*n)/2, if yes else no.

Below is the implementation of the above approach:

Java




// Java Program to Determine if a given
// Matrix is a Sparse Matrix
 
// Importing Libraries
import java.io.*;
 
class GFG {
 
    // Driver Function
    public static void main(String args[])
    {
        // Initialising and declaring
        // variables and array
 
        int array[][]
            = { { 1, 0, 3 }, { 0, 0, 4 }, { 6, 0, 0 } };
 
        int m = 3;
        int n = 3;
        int counter = 0;
 
        // Count number of zeros in the matrix
 
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (array[i][j] == 0)
                    ++counter;
 
        // Printing result
 
        if (counter > ((m * n) / 2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Output

Yes

 

Time Complexity: O(m*n)  where m is no of rows and n is no of columns in matrix
Auxiliary Space: O(1) 

Space complexity: Space complexity of this code is O(1) because the space used by the program is constant, regardless of the input size. The program only uses a fixed amount of space to declare and initialize variables and arrays, and does not create any new objects or data structures that grow in size with the input. Therefore, the space used by the program remains constant, and the space complexity is O(1).



Similar Reads

Java Program to Check if a given matrix is sparse or not
A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elements in the matrix, Examples: Input : 1 0 3 0 0 4
2 min read
Implement a Sparse Matrix Representation and Operations in Java
A matrix with a high proportion of zero members is called a Sparse Matrix. Memory and computing resources may be conserved by effectively representing and manipulating sparse matrices. Java provides many methods for doing fundamental operations and representing sparse matrices. In this article, we will learn to sparse matrix representation and oper
4 min read
Java Program to Determine the Unicode Code Point at Given Index in String
ASCII is a code that converts the English alphabets to numerics as numeric can convert to the assembly language which our computer understands. For that, we have assigned a number against each character ranging from 0 to 127. Alphabets are case-sensitive lowercase and uppercase are treated differently. Complete ASCII values table can be interpreted
5 min read
Implementing Sparse Vector in Java
A vector or arraylist is a one-dimensional array of elements. The elements of a Sparse Vector have mostly zero values. It is inefficient to use a one-dimensional array to store a sparse vector. It is also inefficient to add elements whose values are zero in forming sums of sparse vectors. We convert the one-dimensional vector to a vector of (index,
5 min read
Java Program to Determine Hostname from IP Address
IP Address stands for internet protocol address. It is an identifying number that is associated with a specific computer or computer network. IP addresses are usually written and displayed in human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address). When connected to the internet, the IP address allows the computers to send and recei
3 min read
Java Program to Determine the Name and Version of the Operating System
The inbuilt System class in Java provides getProperty() method which is used to obtain the properties of the current working operating system. The System class has two versions of getProperty(). Both retrieve the value of the property named in the argument list. Methods: getProperty() version 1getProperty() version 2Version 1: One of the versions o
2 min read
Java Program to Determine Number of Bytes Written to File using DataOutputStream
The Java OutputStream class, java.io.OutputStream, is the base class of all output streams in the Java IO API. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. To get the number of bytes written we firs
3 min read
Determine the Upper Bound of a Two Dimensional Array in Java
Multidimensional arrays in Java are common and can be termed as an array of arrays. Data in a two-dimensional array in Java is stored in 2D tabular form. A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array can be seen as an array of the one-dimensional array for easier understanding. A two-dimensional a
3 min read
Java Program to Check Whether a Given Matrix is Lower Triangular Matrix or Not
In the lower triangular matrix, the elements that are present above the diagonal should be 0. Given a matrix and the task is to check the matrix is in lower triangular form or not. A matrix must be a square matrix. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Example - Input: 1 0 0 1 2 0 1 2 3 Outp
2 min read
Java Program for Sort the given matrix
Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where 1 Examples:   Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} } Output : 1 2 3 4 5 6 7 8 9   Approach: Create a temp[] array o
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg