-
Notifications
You must be signed in to change notification settings - Fork 0
/
HourGlassSum.java
77 lines (70 loc) · 1.88 KB
/
HourGlassSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
/*
Name: Frequency Queries
Source: HackerRank
Link: https://www.hackerrank.com/challenges/2d-array/problem
*/
public class HourGlassSum {
// Complete the hourglassSum function below.
static int hourglassSum(int[][] arr) {
int sum =0;
int flag=1;
int largestSum=0;
int row = arr.length;
int col = arr[0].length;
for(int i =0; i<row-2 ; i++)
{
for (int j =0; j<col-2; j++)
{
for(int a =j; a<j+3; a++)
{
sum += arr[i][a];
System.out.println("After"+sum);
sum += arr[i+2][a];
System.out.println("Later"+sum);
if(a==j+2)
{
System.out.println("Entered if");
sum+= arr[i+1][a-1];
if(flag==1)
{
largestSum=sum;
flag = 0;
}
if(largestSum <sum){
System.out.print(largestSum +" "+sum);
largestSum = sum;
System.out.println(largestSum);}
sum = 0;
}
}
}
}
return largestSum;
}
// private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(-2>-9){System.out.println("Good work");}
int[][] arr = { { -1, -1, 0, -9, -2, -2},
{ -2 ,-1, -6, -8, -2, -5 },
{ -1, -1, -1, -2, -3, -4},
{-1, -9, -2, -4, -4, -5 },
{ -7, -3, -3, -2, -9, -9 },
{ -1, -3, -1, -2, -4, -5 }};
//System.out.println("Enter values ");
/* for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
arr[i][j]=sc.nextInt();
}
}*/
int result = hourglassSum(arr);
System.out.println(result);
}
}