Accenture Company Coding Questions Set 2| Calculate Sum with Twist | Sum of second largest element from even and odd list

Hello friends, I hope you all are doing great. In this post today we will learn to solve the 2 coding questions which is being asked by the Accenture company recently. If you are preparing for selection in any company then this post will help you definitely.

Lets Start

1.Calculate Sum with Twist

Problem Statement

You are required to implement the following function:

Int Calculate(int m, int n);

The function accepts 2 positive integer ‘m’ and ‘n’ as its arguments. You are required to calculate the sum of numbers divisible both by 3 and 5, between ‘m’ and ‘n’ both inclusive and return the same.
Note
0 < m <= n

Example

Input:

m : 12

n : 50

Output

90

Explanation:
The numbers divisible by both 3 and 5, between 12 and 50 both inclusive are {15, 30, 45} and their sum is 90.
Sample Input
m : 100
n : 160
Sample Output
405

/*Solution in JAVA*/
class CalculateSumWithTwist 
{
    static int Calculate(int m, int n)
    {
       int sum=0;
        for (int i = m; i <=n; i++) {
          if(i%3==0 && i%5==0)
              sum+=i;
        }
       return sum;
    }
  public static void main(String[] args) 
  {
      System.out.println(Calculate(12, 50));
  }
  
}
/*
OUTPUT
90
*/

2.Sum of second largest element from even and odd list

Problem Statement 

You are required to input the size of the matrix then the elements of matrix, then you have to divide the main matrix in two sub matrices (even and odd) in such a way that element at 0 index will be considered as even and element at 1st index will be considered as odd and so on. then you have sort the even and odd matrices in ascending order then print the sum of second largest number from both the matrices

Example

  • enter the size of array : 5
  • enter element at 0 index : 3
  • enter element at 1 index : 4
  • enter element at 2 index : 1
  • enter element at 3 index : 7
  • enter element at 4 index : 9

Sorted even array : 1 3 9
Sorted odd array : 4 7

Output : 3+4=7

/*Solution in Java*/
import java.util.*;
class SecondLargestSum 
{
   public static void main(String[] args) {
       int size,sum=0;
       ArrayList<Integer> even=new ArrayList<Integer>();
       ArrayList<Integer> odd=new ArrayList<Integer>();       
       Scanner in=new Scanner(System.in);
       System.out.println("Enter size of array:");
       size=in.nextInt();
       int ar[]=new int[size];
       System.out.println("Enter "+size+" elements:");
       for (int i = 0; i < size; i++) {
           ar[i]=in.nextInt();           
       }
       for (int i = 0; i < size; i++) {
           if(i%2==0)
               even.add(ar[i]);
           else
               odd.add(ar[i]);           
       }
       Collections.sort(even);
       Collections.sort(odd);
       System.out.println("Sorted Even Elements: "+even);
       System.out.println("Sorted Odd Elements: "+odd);
       System.out.println("Sorted Largest even Elements: "+even.get(even.size()-2));       
       System.out.println("Sorted Largest odd Elements: "+odd.get(odd.size()-2));
      
       
       if(even.size()==0 ||even.size()==1)
           sum=sum+odd.get(odd.size()-2);
       else if(odd.size()==0 ||odd.size()==1)
           sum=sum+even.get(even.size()-2);
       else
           sum=sum+even.get(even.size()-2)+odd.get(odd.size()-2);
       System.out.println("Sum Of Second Largest Element Of Odd and Even List:"+sum);         
       
   
}
 
}
/*
OUTPUT
Enter size of array:
5
Enter 5 elements:
3 4 1 7 9
Sorted Even Elements: [1, 3, 9]
Sorted Odd Elements: [4, 7]
Sorted Largest even Elements: 3
Sorted Largest odd Elements: 4
Sum Of Second Largest Element Of Odd and Even List:7
*/

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks

Post a Comment

0 Comments