Skip to main content

Find the number of negative integers from row wise or column wise from a sorted matrix

Find the number of negative integers from row wise or column wise from a sorted matrix
This is a frequently asked question for a experienced interview.
Here i am trying to solve this in two different ways, normal way and an optimised method.

Main Class

/**
 * 
 */
package com.rakesh.matrix.bo;

/**
 * @author Rakesh KR
 * @since  August 19th 13:15 2k17
 */
public class RunMatrix {

 public static void main(String[] args) {
  
  MatrixUtils matrixUtils = new MatrixUtils();
  
  try{
   
   System.out.println("------ Matrix ------");
   
   matrixUtils.makeMartix();
  // matrixUtils.printMatrix();
   
   matrixUtils.sortRowMatrix();
   matrixUtils.transpose();
   matrixUtils.sortRowMatrix();
   matrixUtils.transpose(); 
  // matrixUtils.printMatrix();
   
   long t1 = System.currentTimeMillis();
   System.out.println("NegativeNumbers Count Normal    Way : "+matrixUtils.countNegativeNumbersNormal());
   long t2 = System.currentTimeMillis();
   System.out.println("Time for Normal Way :"+(t2-t1)+" ms");
   t1 = System.currentTimeMillis();
   System.out.println("NegativeNumbers Count Optimized Way : "+matrixUtils.countNegativeNumbersOptimized());
   t2 = System.currentTimeMillis();
   System.out.println("Time for Optimized Way :"+(t2-t1)+" ms");
   
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   matrixUtils = null;
  }

 }

}


Sub Class

/**
 * 
 */
package com.rakesh.matrix.bo;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * @author Rakesh KR
 * @since  August 19th 13:17 2k17
 */
public class MatrixUtils {
 
 public int m;
 public int n;
 public int[][] matrix;
 
 public void makeMartix(){
  
  Scanner sc = new Scanner(System.in);
  
  try{
   System.out.println("Size Of Matrix (mxn) : ");
   n = sc.nextInt();
   m = sc.nextInt();
   matrix = new int[n][m];
  // System.out.println("Enter the details to matrix : ");
   int    max    = 99;
   int    min    = -99;
   Random random = new Random();
   for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
  //   matrix[i][j] = sc.nextInt();
     matrix[i][j] = random.nextInt(max + 1 -min) + min; 
    }
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   sc.close();
  }
  
 }
 
 public void printMatrix(){
  
  try{
   System.out.print("The matrix is : \n");
   for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
     System.out.print(" "+matrix[i][j]+" ");
    }
    System.out.print("\n");
   }
   System.out.print("\n");
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   
  }
  
 }
 
 public void sortRowMatrix(){
  
  try{
   for(int i=0; i<matrix.length; i++){
    Arrays.sort(matrix[i]);
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   
  }
  
 }
 
 public void transpose(){
     for (int i = 0; i < matrix.length; i++) {
         for (int j = i; j < matrix[0].length; j++) {
             int tmp1 = matrix[i][j];
             int tmp2 = matrix[j][i];
             matrix[j][i] = tmp1;
             matrix[i][j] = tmp2;
         }
     }
 }
 
 public int countNegativeNumbersNormal(){
  
  int count = 0;
  
  try{
   for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
     if(matrix[i][j]<0){
      count ++;
     }
    }
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   
  }
  
  return count;
 }
 
 public int countNegativeNumbersOptimized(){
  
  int count = 0;
  
  try{
   int i = 0;
   int j = m-1;
   while(j>=0 && i<n){
    if(matrix[i][j]<0){
     count+=(j+1);
     i+=1;
    } else{
     j-=1;
    }
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   
  }
  
  return count;
 }
 
}

Methods:

makeMartix()
This method is used to make a random matrix which consists of positive and negative vales. 
Having min value as -99 and max value as 99.
printMatrix()
This method is used to print an mxn matrix in a user readable manner.
sortRowMatrix()
This method is used to sort an mxn matrix.
transpose()
This method is used to find the transpose of an mxn matrix.
countNegativeNumbersNormal()
An normal way to count the negative numbers from an mxn matrix. 
Its having an 0(n^2) complexity. 
countNegativeNumbersOptimized()
An optimized way to count the negative numbers from an mxn sorted matrix. 

Some tested Results : 
With an 2x2 matrix  :-
------ Matrix ------
Size Of Matrix (mxn) : 
2 2
The matrix is : 
 -53  40 
 -57  10 

The matrix is : 
 -57  10 
 -53  40 

NegativeNumbers Count Normal    Way : 2
Time for Normal Way :0 ms
NegativeNumbers Count Optimized Way : 2
Time for Optimized Way :0 ms

With an 100x100 matrix  :-

------ Matrix ------
Size Of Matrix (mxn) : 
100 100
NegativeNumbers Count Normal    Way : 4906
Time for Normal Way :1 ms
NegativeNumbers Count Optimized Way : 4906
Time for Optimized Way :0 ms

With an 5000x5000 matrix  :-

------ Matrix ------
Size Of Matrix (mxn) : 
5000 5000
NegativeNumbers Count Normal    Way : 12438372
Time for Normal Way :27 ms
NegativeNumbers Count Optimized Way : 12438372
Time for Optimized Way :1 ms

With an 10000x10000 matrix  :-

------ Matrix ------
Size Of Matrix (mxn) : 
10000 10000
NegativeNumbers Count Normal    Way : 49745496
Time for Normal Way :88 ms
NegativeNumbers Count Optimized Way : 49745496
Time for Optimized Way :2 ms

Comments

  1. Advertise what you do with a Home service websites templates from TemplateMonster. Easily customize using our drag-and-drop feature.

    ReplyDelete

Post a Comment

Popular posts from this blog

ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ ഭയങ്കര പവർഫുൾ ആണ്.

ജാവ യെ  കുറിച്ച്  മലയാളത്തിൽ  ഡോകുമെന്റുകളോ  ബ്ലോഗ്‌കളോ  കൂടുതൽ ഇല്ലാത്തതുകൊണ്ട് എനികറിയവുന്നത് ഷെയർ ചെയ്യാം എന്ന് വിചാരിച്ചു .  ജവയെ കുറിച്ച് പറയുകയാണെങ്കിൽ  ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ  ഭയങ്കര പവർഫുൾ ആണ്. :) 1. നമ്മടെ Sun MicroSystems 1995 ൽ തുടങ്ങി വച്ചാ സംഭവമാണിത് . 2. ഇപ്പൊ ജാവയുടെ എട്ടാമത്തെ വെർഷൻ  വരെ ഇറങ്ങികഴിഞ്ഞു . ജാവയുടെ തിയറിയെ കുറിച്ച് എനിക്ക് പറയാൻ താല്പര്യം  ഇല്ലാ, അത് അറിവിള്ളവർ നല്ലപോലെ പറഞ്ഞിടുണ്ട്. നമുക്ക് ഡയറക്റ്റ്  ആയി കോഡിംഗ് പാർട്ടിലേക്ക്  പോകാം. എങ്ങനെയാ ജാവ ഇൻസ്റ്റോൾ ചെയണ്ടേ എന്നൊക്കെ ഒന്ന് ഗൂഗിൾനോട് ചോദിച്ചാ മതി പുള്ളി പറഞ്ഞുതരും. പിന്നെ എല്ലാവരും പറയുന്നു ജാവാ Object  Oriented ആണെന്ന് . അതെ ജാവാ Object Oriented ആണ് . എന്താണ് Object ?, Object  നു വച്ചാ ഒരു സാധനം അത്രതന്നെ. ഇപ്പൊ example പറയുകയാണെങ്കിൽ Book ഒരു സാധനമാണ് . Book റ്റെ പ്രതേകത എന്തൊക്കെ അന്നെന്നുവച്ചാ ആതിനൊരു Auther ഉണ്ടാകും, അതൊരു Category ൽ പെട്ട book ആയിരിക്കും (Novel / Academic ) പിന്നെ ഓരോ bookനും അ...

Angular (web framework)

 Angular is a TypeScript-based free and open-source web application framework lead by the Angular Team at Google and by a community of individuals and corporations. Angular is a complete rewrite from the same team that built AngularJS. Google designed Angular as a ground-up rewrite of AngularJS. Angular 14 was released on June 02, 2022. Some new features include typed forms, standalone components, and new primitives in the Angular CDK (component dev kit). All the major releases are supported for 18 months.  This consists of 6 months of active support, during which regularly-scheduled updates and patches are released. It is then followed by 12 months of long-term support (LTS), during which only critical fixes and security patches are released. Official website :- https://angular.io/

Java String Token

Problem    Given a string, find number of words in that string.     For this problem a word is defined by a string of one or more english alphabets. Input Format   A string not more than 400000 characters long.    The string can be defined by following regular expression:       [A-Za-z !,?.\_'@]+   That means the string will only contain english alphabets,    blank spaces and this characters: "!,?._'@". Output Format   In the first line, print number of words nn in the string.    The words don't need to be unique. In the next nn lines,    print all the words you found in the order they appeared in the input. Sample Input   He is a very very good boy, isn't he? Sample Output 10 He is a very very good boy isn t he Solution import java.io.* ; import java.util.* ; public class Solution { ...