We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
thanks for your c++ code. I implemented the same in java and it worked :) .here is my code:
publicclassSolution{staticintn,m,arr[][];publicstaticintcells(inti,intj){if(((i<0||j<0)||(i>=n||j>=m))||arr[i][j]==0||arr[i][j]==-1)return0;arr[i][j]=-1;return1+cells(i-1,i-1)+cells(i-1,j)+cells(i-1,j+1)+cells(i,j-1)+cells(i,j+1)+cells(i+1,j-1)+cells(i+1,j)+cells(i+1,j+1);}publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscan=newScanner(System.in);intlarge=0;n=scan.nextInt();m=scan.nextInt();scan.nextLine();arr=newint[n][m];for(inti=0;i<n;i++){Stringinput[]=scan.nextLine().split(" ");for(intj=0;j<input.length;j++){arr[i][j]=Integer.parseInt(input[j]);//System.out.println(arr[i][j]);}}for(inti=0;i<n;i++){for(intj=0;j<m;j++){if(arr[i][j]==1)large=Math.max(large,cells(i,j));}}System.out.print(large);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →
thanks for your c++ code. I implemented the same in java and it worked :) .here is my code: