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.
- Prepare
- Data Structures
- Arrays
- 2D Array - DS
- Discussions
2D Array - DS
2D Array - DS
+ 0 comments C++
// stores the sums, in order std::set<int> hourglass_sums; /* A core is the middle point of the glasshour a b c d e f g in the above, the core is d */ // finds the sum of the three neighbors above the core int sumOfUpperNeighbors(int core_y, int core_x, vector<vector<int>> arr) { int left_most_cordinate = core_x - 1; int right_most_cordinate = core_x + 1; int upper_coordinate = core_y - 1; int sum =0; sum += arr[upper_coordinate][left_most_cordinate]; sum += arr[upper_coordinate][core_x]; sum += arr[upper_coordinate][right_most_cordinate]; return sum; } // finds the sum of the three neighbors above the core int sumOfLowerNeighbors(int core_y, int core_x, vector<vector<int>> arr) { int left_most_cordinate = core_x - 1; int right_most_cordinate = core_x + 1; int lower_coordinate = core_y + 1; int sum =0; sum += arr[lower_coordinate][left_most_cordinate]; sum += arr[lower_coordinate][core_x]; sum += arr[lower_coordinate][right_most_cordinate]; return sum; } int hourglassSum(vector<vector<int>> arr) { int total_sum = 0; for(int i = 1; i < 5; i++){ for(int j = 1; j < 5; j++){ total_sum += arr[i][j]; total_sum += sumOfUpperNeighbors(i,j,arr); total_sum += sumOfLowerNeighbors(i,j,arr); hourglass_sums.insert(total_sum); total_sum = 0;
} } return *hourglass_sums.rbegin();
}
+ 0 comments Here is the solution of 2D Array - ds Solution Click Here
+ 0 comments Simple Java Solution
public static int hourglassSum(List<List<Integer>> arr) { int biggest = Integer.MIN_VALUE; for(int i = 0; i < arr.size() - 2; i++){ for(int j = 0; j < arr.get(i).size() - 2; j++){ int count = 0; count += arr.get(i).get(j); count += arr.get(i).get(j + 1); count += arr.get(i).get(j + 2); count += arr.get(i + 1).get(j + 1); count += arr.get(i + 2).get(j); count += arr.get(i + 2).get(j + 1); count += arr.get(i + 2).get(j + 2); biggest = count >= biggest ? count : biggest; } } return biggest; }
+ 1 comment include
using namespace std;
int main(){ int arr[6][6]; for(int i=0;i<6;i++){ for(int b=0;b<6;b++){ cin>>arr[i][b]; } }
int a=1;
int c; for(int e=0;e<4;e++){ for(int f=0;f<4;f++){ int b=b+arr[e][f]+arr[e+2][f]+arr[e+1][f+1]+arr[e][f+1]+arr[e+2][f+1]+arr[e][f+2]+arr[e+2][f+2]; while(a==1){c=b;a++;}c=max(c,b); } }
cout<
return 0;}
+ 0 comments public static int hourglassSum(List<List<Integer>> arr) { // Write your code here int maxInteger = Integer.MIN_VALUE; for (int row = 0; row < arr.size() - 2; row++) { for (int col = 0; col < arr.get(row).size() - 2; col++) { int topRow = arr.get(row).get(col) + arr.get(row).get(col + 1) + arr.get(row).get(col + 2); int bottomRow = arr.get(row + 2).get(col) + arr.get(row + 2).get(col + 1) + arr.get(row + 2).get(col + 2); int middleRow = arr.get(row + 1).get(col + 1); maxInteger = Math.max(maxInteger, topRow + bottomRow + middleRow); } } return maxInteger; }
Load more conversations
Sort 3484 Discussions, By:
Please Login in order to post a comment