whoknows
BAN USER
Questions (2)
Comments (4)
Reputation 20
 0of 0 votes
Answersthere is a 2d array and gbikes are located in that location. there is a person and he wants to know the nearest location of the bike which is available for him(there can be more than 1 nearest bike). person can only move left , right , up or down. output should be the distance in int.
 whoknows in United States Report Duplicate  Flag  PURGE
Google
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
package com.algorithms.test;
public class LongestPathInMatrixWith01 {
public static int FindLongestSequences(int[][] matrix, int row,int col){
int maxCnt = 0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int a = (i==0) ? 0: matrix[(i1)][j];
int b = (j==0) ? 0: matrix[i][j1];
System.out.println("i: "+i+" j:"+ j);
System.out.println("a: "+a+" b:"+b+" "+matrix[i][j]);
matrix[i][j] =( matrix[i][j]>0) ? max(a,b)+1 :0;
System.out.println(matrix[i][j]);
maxCnt = max(maxCnt,matrix[i][j]);
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.print(matrix[i][j]);
}
System.out.println();
}
return maxCnt;
}
public static int max(int a , int b){
return (a>b) ? a :b;
}
public static void DoTest(int[][] matrix, int rows, int col){
if(matrix == null ) return;
if(rows < 1) return;
if(col < 1) return;
for(int i=0;i<rows;i++){
for(int j=0;j<col;j++){
System.out.print(matrix[i][j]);
}
System.out.println();
}
int len = FindLongestSequences(matrix, rows, col);
System.out.println("Maximum Length "+len);
}
public static void main(String[] args) {
// TODO Autogenerated method stub
int[][] matrix = {
{0, 0, 0, 1, 1},
{1, 1, 1, 0, 1},
{0, 1, 1, 1, 0},
{0, 0, 1, 0, 0},
{1, 1, 1, 1, 1}
};
DoTest(matrix,matrix.length,matrix[0].length);
}
}

whoknows
June 07, 2018 Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
@tuttobenethx
 whoknows June 10, 2018Thanks for sharing your thoughts. Can you tell me the time complexity of bleed solution
And better solution is not correct solution
if we give manPosRow = 9 and manPosCol = 3 then answer should be zero
but it is giving 4