Accenture Coding Question 2
Coding Question 2
You are required to implement the following Function def LargeSmallSum(arr).
The function accepts an integers arr of size ’length’ as its arguments you are required to return the sum of second largest largest element from the even positions and second smallest from the odd position of given ‘arr’.
Assumption:
- All array elements are unique
- Treat the 0th position a seven
NOTE
- Return 0 if array is empty
- Return 0, if array length is 3 or less than 3
Example:-
Input
arr:3 2 1 7 5 4
Output
7
Explanation
- Second largest among even position elements(1 3 5) is 3
- Second largest among odd position element is 4
- Thus output is 3+4 = 7
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
8
Python
C
C++
Java
Python
length = int(input()) arr = list(map(int, input().split())) even_arr = [] odd_arr = [] for i in range(length): if i % 2 == 0: even_arr.append(arr[i]) else: odd_arr.append(arr[i]) even_arr = sorted(even_arr) odd_arr = sorted(odd_arr) print(even_arr[len(even_arr)-2] + odd_arr[len(odd_arr)-2])
Input:
7
1 8 0 2 3 5 6
Output:
8
C
#include <stdio.h> int largeSmallSum(int *array, int n) { int answer, i, j, temp;; int even[n], odd[n]; int evencount = 0, oddcount = 0; if(n<=3) { answer = 0; } else { even[0] = array[0]; evencount = 1; for(i=1; i<n; i++) //creating two array even and odd { if(i%2==0) { even[evencount] = array[i]; evencount++; } else { odd[oddcount] = array[i]; oddcount++; } } for(i=0; i<evencount; i++) //sorting of even array { for(j=i+1; j<evencount; j++) { if(even[i]>even[j]) { temp = even[i]; even[i] = even[j]; even[j] = temp; } } } for(i=0; i<oddcount; i++) //sorting of odd array { for(j=i+1; j<oddcount; j++) { if(odd[i]>odd[j]) { temp = odd[i]; odd[i] = odd[j]; odd[j] = temp; } } } answer = even[evencount-2] + odd[1]; } return answer; } int main() { int n, result, i; scanf("%d",&n); int array[n]; for(i=0; i<n; i++) { scanf("%d",&array[i]); } result = largeSmallSum(array, n); printf("%d",result); return 0; }
C++
#include <bits/stdc++.h>
using namespace std;
int largeSmallSum(int *array, int n)
{
int answer, i, j, temp;;
int even[n], odd[n];
int evencount = 0, oddcount = 0;
if(n<=3)
{
answer = 0;
}
else
{
even[0] = array[0];
evencount = 1;
for(i=1; i<n; i++)
{
if(i%2==0)
{
even[evencount] = array[i];
evencount++;
}
else
{
odd[oddcount] = array[i];
oddcount++;
}
}
sort(even, even + evencount);
sort(odd, odd+oddcount);
answer = even[evencount-2] + odd[oddcount-2];
}
return answer;
}
int main()
{
int n, result, i;
cin>>n;
int array[n];
for(i=0; i<n; i++)
cin>>array[i];
result = largeSmallSum(array, n);
cout<<result;
return 0;
}
Java
import java.util.*;
class Solution
{
public static int largeSmallSum (int[]arr)
{
ArrayList < Integer > even = new ArrayList < Integer > ();
ArrayList < Integer > odd = new ArrayList < Integer > ();
even.add (arr[0]);
for (int i = 1; i < arr.length; i++)
{
if (i % 2 == 0)
even.add (arr[i]);
else
odd.add (arr[i]);
}
Collections.sort (even);
Collections.sort (odd);
return even.get (even.size () - 2) + odd.get (1);
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt ();
System.out.println (largeSmallSum (arr));
}
}