Java 1-d and 2-d Array | Practice | GeeksforGeeks

Given a integer n. We have n*n values of a 2-d array, and  n values of 1-d array. Task is to find the sum of the left diagonal values of the 2-d array and the max element of the 1-d array and print them with space in between.
Example 1:
â€‹Input : arr[][] = {{1,2,3}, {4,5,6}, {7, 8,9}} 
        and N = 3
brr[] = {3, 6, 9}
Output : 15 9
Explanation:
1 2 3
4 5 6
7 8 9
So, this sum of left diagonal (1+ 5 + 9) = 15
The maximum element in an array brr is 9
So, will return {15, 9} as an answer.


â€‹Example 2:
Input : arr[][] = {{1,2}, {1, 2}} and N = 2
brr[] = {10, 1} 
Output :  3 10 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function array() that takes a two-dimension array (a), another one dimension array (b), sizeOfArray (n), and return the ArrayList which is having the sum of the diagonal elements of the array a and the maximum number of the array b. The driver code takes care of the printing.
Expected Time Complexity: O(N2).
Expected Auxiliary Space: O(1).

 
Constraints
1 ≤ n ≤ 100
1 ≤ a[i][j], b[i] ≤ 103
+1
prashantfp32 days ago
class Complete{   public static ArrayList<Integer> array(int a[][], int b[], int n)   {       // Complete the function    int sum=0;      for(int i=0;i<n;i++)      {          for(int j=0;j<n;j++)          {              if(i==j)              {                  sum=sum+a[i][j];              }          }      }
      // Finding Max element from 1D array      int max=b[0];      for(int i=1;i<b.length;i++)      {          if(b[i]>max)          {              max=b[i];          }      }
      // created ArrayList and stored sum of diagonal elements and max of 1D array       ArrayList<Integer> li=new ArrayList<Integer>();      li.add(sum);      li.add(max);      return li;  }}
0
singhmanjeetsk753 days ago
In java solution
class Complete{   public static ArrayList<Integer> array(int a[][], int b[], int n)   {       ArrayList<Integer> c = new ArrayList<Integer>();       int max=Arrays.stream(b).max().getAsInt();       int sum=0;       for(int i=0;i<n;i++){         sum=sum+a[i][i];         }         c.add(sum);         c.add(max);         return c;   }} 
0
dostdedhoka2 weeks ago
int sum=0;       for(int i=0;i<n;i++){           sum+=a[i][i];       }       int max=-1;       for(int i=0;i<n;i++){           max=Math.max(max,b[i]);       }       ArrayList<Integer> ob=new ArrayList<>();       ob.add(sum);       ob.add(max);       return ob;
 
0
priobratamalik2 months ago
/* O(n) Java Code */
ArrayList<Integer> ans = new ArrayList<>();       int first=0, second=0;       for(int i=0; i<n; i++)       {               first+=a[i][i];                      if(second<b[i])               second=b[i];       }       ans.add(first);       ans.add(second);       return ans;
0
skpuntri2 months ago
li.add(sum);       li.add(max);       return li;
+1
jagtapshubham1913 months ago
class Complete{   public static ArrayList<Integer> array(int a[][], int b[], int n)   {       // Complete the function              // Addition of left diagonal element of 2D array       int sum=0;       for(int i=0;i<n;i++)       {           for(int j=0;j<n;j++)           {               if(i==j)               {                   sum=sum+a[i][j];               }           }       }
       // Finding Max element from 1D array       int max=b[0];       for(int i=1;i<b.length;i++)       {           if(b[i]>max)           {               max=b[i];           }       }
       // created ArrayList and stored sum of diagonal elements and max of 1D array        ArrayList<Integer> li=new ArrayList<Integer>();       li.add(sum);       li.add(max);       return li;   }}
0
arvind16yadav3 months ago
/*JAVA SOLUTION*/
public static ArrayList<Integer> array(int a[][], int b[], int n)   {       // Complete the function              int sum_of_left_diagonal = 0;       int max_number = b[0];              ArrayList<Integer> ans = new ArrayList<Integer>();              for(int i=0; i<n; i++){           sum_of_left_diagonal += a[i][i];       }              ans.add(sum_of_left_diagonal);              for(int i=1; i<n; i++){           if(max_number < b[i]){               max_number = b[i];           }       }              ans.add(max_number);
       return ans;           }
0
prateekkasaudhan1233 months ago
// JAVA SOLUTION
ArrayList<Integer> ans = new ArrayList<>();       int ansA = 0;       int ansB = 0;       for(int i=0;i<n;i++){           for(int j=0;j<n;j++){               if(i==j){                   ansA += a[i][j];               }           }           if(b[i]>ansB){               ansB = b[i];           }       }              ans.add(ansA);       ans.add(ansB);       return ans;
0
natraj20023 months ago
//O(n) Simple
 ArrayList<Integer> lst=new ArrayList<>();       int dsum=0;       int max=Integer.MIN_VALUE;       for(int i=0;i<n;i++){           dsum+=a[i][i];       }       for(int i=0;i<n;i++){           max=Math.max(b[i],max);       }       lst.add(dsum);       lst.add(max);       return lst;
0
zentalc7413 months ago
class Complete{   public static ArrayList<Integer> array(int a[][], int b[], int n)   {       int sum=0;       ArrayList<Integer> lis=new ArrayList<Integer>();      for(int i=0;i<a.length;i++){          for(int j=0;j<a[i].length;j++){              if(i==j){                  sum+=a[i][j];              }          }      }      int max=b[0];      for(int g=0;g<b.length;g++){          if(b[g]>max){              max=b[g];          }      }      lis.add(sum);      lis.add(max);      return lis;         }} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.