Problem no 75: returning sum of left diagonal elements in 2d array and max of 1d array in Arraylist.

 problem:

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:

​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.


​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.






code:


  public static ArrayList<Integer> array(int a[][], int b[], int n)

    {

        // Complete the function

    int sum=0;

    int max=Integer.MIN_VALUE;

       for(int i=0;i<n;i++)

       {

           

           for(int j=0;j<n;j++)

           {

               if(i==j)

               {

                   sum=sum+a[i][j];

               }

           }

           

       }

      for(int i=0;i<n;i++)

      {

          if(max<b[i])

          {

              max=b[i];

          }

      }

      

      ArrayList<Integer> alist=new ArrayList<Integer>(2);

      alist.add(sum);

      alist.add(max);

      return alist;

        

    }

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.