2D Array - DS HACKKERANK
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'hourglass sum function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as a parameter.
*/
//Time O(1) as we know every time we have to iterate the loop
//through value 4 as array size will be fixed as 6.
//Space O(N) as we are using ArrayList for storing of values and then
//find out the max value from it.
public static int hourglassSum(int[][] arr) {
ArrayList<Integer>aa=new ArrayList<Integer>();
int sum=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
sum=arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j+1]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2];
aa.add(sum);
sum=0;
}
}
System.out.println(Collections.max(aa));
return Collections.max(aa);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
int arr[][]=new int[6][6];
Scanner sc=new Scanner(System.in);
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
arr[i][j]=sc.nextInt();
}
}
Result obj=new Result();
obj.hourglassSum(arr);
}
}
Comments
Post a Comment