title: Optimise the code to find max consecutive points in a straight line(row,column or diagonal) in a matrix
body: <p>I need to find the maximum number of consecutive points in a straight line(row,column or diagonal) in a matrix .</p>
  
  <p>For eg:- if it is a 4*4 matrix
  input is (1#1,2#2,3#3,2#1) the answer should be 3 as the max consecutive points in diagonal is 3.</p>
  
  <p>My code is successfully getting executed with expected results.But the code complexity is very high.</p>
  
  <p>Can someone please suggest the best approach complexity wise.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>// Get the max number of Continuous points in a matrix row
  private static int getMaxContinuousPointsInRow(boolean[][] matrix, int row){
      int maxCount = 0;
      int currCount = 0;
      int pos = 0;
      while(pos &lt; matrix[row].length){
          currCount = 0;
      while(pos &lt; matrix[row].length &amp;&amp; !matrix[row][pos])
          pos++;
      if(pos &gt;= matrix[row].length)
          break;
      while(pos &lt; matrix[row].length &amp;&amp; matrix[row][pos]){
          currCount++;
          pos++;
      }
      if(currCount &gt; maxCount)
          maxCount = currCount;
      }
      return maxCount;
  }
  
  // Get the max number of Continuous points in a matrix row
  private static int getMaxContinuousPointsInCol(boolean[][] matrix, int col) {
      int maxCount = 0;
      int currCount = 0;
      int pos = 0;
  
      while (pos &lt; matrix.length) {
          currCount = 0;
      while (pos &lt; matrix.length &amp;&amp; !matrix[pos][col])
          pos++;
      if(pos &gt;= matrix.length)
          break;
      while (pos &lt; matrix.length &amp;&amp; matrix[pos][col]) {
          currCount++;
          pos++;
      }
      if (currCount &gt; maxCount)
          maxCount = currCount;
      }
      return maxCount;
  }
  
  // Get the max number of Continuous points in a matrix diagonal right starting from position (row,col)
  private static int getMaxContinuousPointsInDiagonalRight(boolean[][] matrix, int row, int col) {
      int maxCount = 0;
      int currCount = 0;
      int i = row, j = col;
      while (i &lt; matrix.length &amp;&amp; j &lt; matrix[row].length) {
          currCount = 0;
      while (i &lt; matrix.length &amp;&amp; j &lt; matrix[row].length &amp;&amp; !matrix[i][j]){
          i++;
          j++;
      }
      if(i &gt;= matrix.length || j &gt;= matrix[row].length)
          break;
      while (i &lt; matrix.length &amp;&amp; j &lt; matrix[row].length &amp;&amp; matrix[i][j]) {
          currCount++;
          i++;
          j++;
      }
      if (currCount &gt; maxCount)
          maxCount = currCount;
      }
      return maxCount;
  }
  
  public static int function_called_by_main_method(int input1, int input2, String[] input3) {
      // create a boolean matrix of size  input1 x input2
      // M[i][j] = true if  input3 contains a point i#j, else M[i][j] = false
      boolean M[][] = new boolean[input1 + 1][input2 + 1];
      // initialize the matrix with all false values
      for(int i=0; i &lt;= input1; i++){
          for(int j=0; j &lt;= input2; j++){
              M[i][j] = false;
          }
      }
      // process each value in input3 and populate the matrix
      for (String s : input3) {
          // extract row, column value
          String[] data = s.split("#");
          int i = Integer.parseInt(data[0]);
          int j = Integer.parseInt(data[1]);
          M[i][j] = true;
      }
      // get max number of Continuous points among all matrix rows
      int max = 0;
      for(int row = 0; row &lt;= input1; row++){
          int rowMax = getMaxContinuousPointsInRow(M, row);
          if(rowMax &gt; max)
          max = rowMax;
      }
      // get max number of Continuous points among all matrix rows and columns
      for (int col = 0; col &lt;= input2; col++) {
          int colMax = getMaxContinuousPointsInCol(M, col);
          if (colMax &gt; max)
          max = colMax;
      }
      // get max number of Continuous points among all matrix rows, columns and right diagonals
      for(int col = input2 ; col &gt;= 0; col--){
          int diagMax = getMaxContinuousPointsInDiagonalRight(M, 0, col);
          if(diagMax &gt; max)
          max = diagMax;
      }
      for(int row = 1 ; row &lt;= input1; row++){
          int diagMax = getMaxContinuousPointsInDiagonalRight(M, row, 0);
          if(diagMax &gt; max)
              max = diagMax;
      }
      return max;
  }
  </code></pre>