A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

2D Array - DS

2D Array - Ds

2D Array - DS

  1. The 2D array is planned as matrices.
  2. It can be signified as the collection of rows and columns.
  3. Example of 2D arrays is a database table.
  4. It is used to implement a relational database.

Problem Statement based on 2D Array - DS

Let’s assume that you have a 2D array with dimensions 6*6. An hourglass in an array is defined as a portion shaped like this:




a  b  c

  d
  
e  f  g


 

Assume that above hourglass will be replaced with number 2 and array will be full of zeros. Now, it may look like this:





2  2  2  0  0  0

0 2 0 0 0 0

2 2 2 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0


 
When, we think dipper and more about this hourglass, there are many hourglasses in the array above. The possible three leftmost hourglasses are the following:




2 2 2    2 2 0   2 0 0
  2        0       0
2 2 2    2 2 0   2 0 0


 
The sum of an hourglass:
it is the sum of all the numbers within it. The sum for the hourglasses above are 14, 8, and 4, respectively. The largest sum among the all will be the output. Therefore, sample-output will be 14. Since, in this problem, you have to print the largest sum among all the hourglasses in the array.

Example 2.

Input Format There will be exactly 6 lines of input, each containing 6 integers separated by spaces. Each integer will be between -9 and 9, inclusively. Output Format Print the answer to this problem on a single line. Sample Input




2 2 2 0 0 0

0 2 0 0 0 0

2 2 2 0 0 0

0 0 4 8 8 0

0 0 0 4 0 0

0 0 1 4 8 0


 
Sample Output 37 Explanation The hourglass possessing the largest sum is:




4 8 8

  4

1 4 8


 

LET’S WE ARE USING JAVA FOR CREATING THE PROGRAM:-

import java.util.Scanner;
public class HourGlass {
       //main method from where program execution will start
       public static void main(String[] args) {
             //to get user input
             Scanner in = new Scanner(System.in);
             //create array 6*6 size
             int arr[][] = new int[6][6];
             //iterate the for loop to store full array
             for (int arr_i = 0; arr_i < 6; arr_i++) {
                    for (int arr_j = 0; arr_j < 6; arr_j++) {
                          arr[arr_i][arr_j] = in.nextInt();
                    }
             }
             //lets take an count
             int count = Integer.MIN_VALUE;
             //create logic using iterating array
             for (int i = 0; i < arr.length - 1; i++) {
                    for (int j = 0; j < arr.length; j++) {
                          //check array i and j places and ignore the logic
                          if (i >= arr.length - 2 || j >= arr.length - 2) {
                          } else {//create the logic, take first, second and third row of array
                                 int sum_temp = 0;
                                 //get sum of first row
                                 int row_firstTemp = arr[i][j] + arr[i][j + 1] + arr[i][j + 2];
                                 //get sum of 2nd row
                                 int row_secoundTemp = arr[i + 1][j + 1];
                                 //get sum of 3rd row
                                 int row_thirdTemp = arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
                                 //add all rows of array
                                 sum_temp = row_firstTemp + row_secoundTemp + row_thirdTemp;
                                 //check with count
                                 if (sum_temp > count) {
                                       count = sum_temp;
                                 }
                          }
                    }
             }
             //final output
             System.out.println("Output: " + count);
       }
}
Input: 2 2 2 0 0 0 0 2 0 0 0 0 2 2 2 0 0 0 0 0 4 8 8 0 0 0 0 4 0 0 0 0 1 4 8 0 Output: 37

LEARN TUTORIALS

.

.