Accenture Coding Question 11

Coding Question 11

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   10

#include <stdio.h>

int main()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];

printf("enter the length of array : ");
scanf("%d",&length);

for(i=0;i<length;i++)
{
printf("Enter element at %d index : ",i);
scanf("%d",&arr[i]);
}

if(length%2==0)
{
oddlen = length/2;
evenlen = length/2;
}
else
{
oddlen = length/2;
evenlen = (length/2) + 1;
}

for(i=0;i<length;i++) // seperation of even and odd array
{
if(i%2==0)
{
even[i/2] = arr[i];
}
else
{
odd[i/2] = arr[i];
}
}

for(i=0; i<evenlen-1; i++) // sorting of even array
{
for(j=i+1; j<evenlen; j++)
{
temp = 0;
if(even[i]>even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}

for(i=0; i<oddlen-1; i++) // sorting of odd array
{
for(j=i+1; j<oddlen; j++)
{
temp = 0;
if(odd[i]>odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}

printf("\nSorted even array : "); // printing even array
for(i=0;i<evenlen;i++)
{
printf("%d ",even[i]);
}

printf("\n");

printf("Sorted odd array : "); // printing odd array
for(i=0;i<oddlen;i++)
{
printf("%d ",odd[i]);
}

printf("\n\n%d",even[evenlen-2] + odd[1]); // printing final result
}

#include <iostream>
using namespace std;


int main()
{
int arr[100];
int length, i, j, oddlen, evenlen, temp, c, d;
int odd[50], even[50];

cout<<"enter the length of array : ";
cin>>length;

for(i=0;i<length;i++)
{
cout<<"Enter element at index : ",i;
cin>>arr[i];
}

if(length%2==0)
{
oddlen = length/2;
evenlen = length/2;
}
else
{
oddlen = length/2;
evenlen = (length/2) + 1;
}

for(i=0;i<length;i++) // seperation of even and odd array
{
if(i%2==0)
{
even[i/2] = arr[i];
}
else
{
odd[i/2] = arr[i];
}
}

for(i=0; i<evenlen-1; i++) // sorting of even array
{
for(j=i+1; j<evenlen; j++)
{
temp = 0;
if(even[i]>even[j])
{
temp = even[i];
even[i] = even[j];
even[j] = temp;
}
}
}

for(i=0; i<oddlen-1; i++) // sorting of odd array
{
for(j=i+1; j<oddlen; j++)
{
temp = 0;
if(odd[i]>odd[j])
{
temp = odd[i];
odd[i] = odd[j];
odd[j] = temp;
}
}
}

cout<<"\nSorted even array : "; // printing even array
for(i=0;i<evenlen;i++)
{
cout<<even[i];
}

cout<<"\n";

cout<<"Sorted odd array : "; // printing odd array
for(i=0;i<oddlen;i++)
{
cout<<odd[i];
}

cout<<even[evenlen-2] + odd[1]; // printing final result
}
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array : ");
int arrsize = sc.nextInt();
int[] main = new int[arrsize];
ArrayList<Integer> even = new<Integer>ArrayList();
ArrayList<Integer> odd = new<Integer>ArrayList();

System.out.println("Enter "+arrsize+" Elements");
for (int i = 0; i < arrsize; i++)
main[i] = sc.nextInt();
for (int i = 0; i < arrsize; i++) {
if(i%2==0)
even.add(main[i]);
else
odd.add(main[i]);
}
Collections.sort(even);
Collections.sort(odd);

System.out.println("Even position index");
for (int e : even)
System.out.println(e);
System.out.println("Odd position index");
for (int e : odd)
System.out.println(e);

int evenlargest=0,evensec=0,oddlargest=0,oddsec=0;
for (int i = 0; i < even.size(); i++) { // In this section we're finding largest element in odd and even List
if(even.get(i)>evenlargest)
evenlargest = even.get(i);
for (int j = 0; j < odd.size(); j++)
if(odd.get(j)>oddlargest)
oddlargest = odd.get(i);
}
for (int i = 0; i < even.size(); i++) // here we're finding 2nd largest element in off and
if((even.get(i)>evensec) && even.get(i) < evenlargest) {
evensec=even.get(i);
}
for (int i = 0; i < odd.size(); i++) {
if((odd.get(i)>oddsec) && odd.get(i)<oddlargest)
oddsec=odd.get(i);
}
System.out.println("Second Largest Element in Even List is:"+evensec);
System.out.println("Second Largest Element in Odd List is:"+oddsec);
System.out.println("Sum Of Second Largest Element Of Odd and Even List:"+(evensec+oddsec));
}

}
array = []
evenArr = []
oddArr = []
n = int(input("Enter the size of the array:"))
for i in range(0,n):
number = int(input("Enter Element at {} index:".format(i)))
array.append(number)
if i % 2 == 0:
evenArr.append(array[i])
else:
oddArr.append(array[i])

evenArr = sorted(evenArr)
print("Sorted Even Array:", evenArr[0:len(evenArr)])
oddArr = sorted(oddArr)
print("Sorted Odd Array:", oddArr[0:len(oddArr)])
print(evenArr[1] + oddArr[1])