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.
There are some really good solutions in this thread, but I wanted to throw out some variations in case anyone else was curious about how Java performed using different constructs.
I tried 4 approaches, the first 2 using conventional summation. As expected, both of these were the fastest, but what I didn't expect to see was that solution 1 had a distinct advantage over #2. (The delta was small but consistently ~ 2x). Despite this, I personally favor #2 unless performance is a major concern - strictly because it would be the easiest to maintain.
The 3rd and 4th approaches both used the Java8 stream/lambda functionality (mostly for my curiousity). As expected both approaches were measurably slower than #1 (100-150x and 110-200x slower respectively). I would caution anyone to take too much from this as overhead in this problem(and my inexperience with streams) overwhelmed any benefit from using streams.
// Approach 1: explicitly add the array values (TIED FASTEST)privatestaticintsolve1(int[][]arr){intmaxValue=Integer.MIN_VALUE;intwidth=arr[0].length-2;for(introw=0;row<arr.length-2;row++){for(intcol=0;col<width;col++){maxValue=Math.max(maxValue,arr[row][col]+arr[row][col+1]+arr[row][col+2]+arr[row+1][col+1]+arr[row+2][col]+arr[row+2][col+1]+arr[row+2][col+2]);}}returnmaxValue;}// Approach 2: Inner loop to sum top/bottom rows (2ND FASTEST)privatestaticintsolve2(int[][]arr){intmaxValue=Integer.MIN_VALUE;intwidth=arr[0].length-2;inttotal=0;for(introw=0;row<arr.length-2;row++){for(intcol=0;col<width;col++){total=arr[row+1][col+1];// centerfor(intpos=col;pos<col+3;pos++){// add the top/bottom rowstotal+=arr[row][pos]+arr[row+2][pos];}maxValue=Math.max(maxValue,total);}}returnmaxValue;}// Approach 3: Use streams to sum top/bottom rows (2ND SLOWEST)privatestaticintsolve3(int[][]arr){intmaxValue=Integer.MIN_VALUE;intwidth=arr[0].length-2;for(introw=0;row<arr.length-2;row++){for(intj=0;j<width;j++){maxValue=Math.max(maxValue,Arrays.stream(arr[row],j,j+3).sum()+arr[row+1][j+1]+Arrays.stream(arr[row+2],j,j+3).sum());}}returnmaxValue;}// Approach 4: Use stream to loop/sum rather than for loops (SLOWEST)privatestaticintsolve4(int[][]arr){intwidth=arr[0].length-2;returnIntStream.range(0,arr.length-2).map((row)->IntStream.range(0,width).map(col->Arrays.stream(arr[row],col,col+3).sum()+arr[row+1][col+1]+Arrays.stream(arr[row+2],col,col+3).sum()).max().getAsInt()).max().getAsInt();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 2D Array
You are viewing a single comment's thread. Return to all comments →
There are some really good solutions in this thread, but I wanted to throw out some variations in case anyone else was curious about how Java performed using different constructs.
I tried 4 approaches, the first 2 using conventional summation. As expected, both of these were the fastest, but what I didn't expect to see was that solution 1 had a distinct advantage over #2. (The delta was small but consistently ~ 2x). Despite this, I personally favor #2 unless performance is a major concern - strictly because it would be the easiest to maintain.
The 3rd and 4th approaches both used the Java8 stream/lambda functionality (mostly for my curiousity). As expected both approaches were measurably slower than #1 (100-150x and 110-200x slower respectively). I would caution anyone to take too much from this as overhead in this problem(and my inexperience with streams) overwhelmed any benefit from using streams.