In this post we will see off campus coding questions asked in TATA ELXSI off campus recruitments. Various coding problems asked in tata elxsi off campus recruitment.
TATA ELXSI Coding Question
Question
Given an integer array and integer N denoting array length as input, your task is to return sum of 3rd largest element and 2nd minimum element from array of elements.
Note: Input array can have negative elements as well
Example:
Input1: 9
input2: {8,10,8,4,15,9,6,3,17}
Output: 14
Explanation: from input2 we can see first highest number is 17, second highest number is 15, third highest number is 10
Also first minimum number is 3, second minimum number is 4.
Therefore “3rd highest number” + “2nd minimum number“= 10+4= 14
Algorithm For Solving TATA ELXSI problem
- Get the first input from user, that is value of N indicating number of elements in array
- Get second input from user indicating different values inside array.
- Sort the array in Ascending order or Decending order
- If sorted in ascending order select element at index position 2 for getting 3rd largest number.
- Then select element at index (last index-1) to get 2nd minimum element
- If sorted in Decending order decide accordingly for getting 3rd and 2nd elements from array.
- So now add up both selected elemets ehich is our answer
JAVA CODE
import java.util.*; public class problem33 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements "); int N=sc.nextInt(); int array1[]=new int[N]; int array2[]=new int[N]; int k=0; System.out.println("Enter array elements "); for(int i=0;i<N;i++){ array1[k]=sc.nextInt(); k=k+1; } for (int j = 0; j < array1.length - 1; j++) { if (array1[j] < array1[j + 1]) { int temp = array1[j]; array1[j] = array1[j + 1]; array1[j + 1] = temp; j = -1; } } int res=0; res=res+array1[2]+array1[array1.length-2]; System.out.println("sum of 3rd largest and 2nd minimum element is : "+res); } }
If you want step by step explanation for above code, watch our video below