Second Largest Codechef Solution

Three numbers AB and C are the inputs. Write a program to find second largest among them.

Input 

The first line contains an integer T, the total number of testcases. Then T lines follow, each line contains three integers AB and C.

Output 

For each test case, display the second largest among AB and C, in a new line.

Constraints

  • 1 <= T <= 1000
  • 1 <= A, B, C <= 1000000

Example

Input:

3 
120 11 400
10213 312 10
10 3 450

Output:

120
312
10

 Second Largest – CodeChef Solution in Python

N = int(input())
for i in range(N):
    x = list(map(int, input().split()))
    x.sort()
    print(x[1])

 Second Largest – CodeChef Solution in CPP

#include <iostream>

using namespace std;

int main(){
	int a;
	cin >> a;
	while(a--){
		long long a,b,c;
		cin >> a >> b >> c;
		if(a>=b && b>=c || a<=b && b<=c)
			cout << b << endl;
		else if(a>=c && c>=b || a<=c && b>=c)
			cout << c << endl;
		else
			cout << a << endl;
	}
}

  Second Largest – CodeChef Solution in JAVA

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int A = sc.nextInt();
			int B = sc.nextInt();
			int C = sc.nextInt();

			System.out.println(solve(A, B, C));
		}

		sc.close();
	}

	static int solve(int A, int B, int C) {
		return Arrays.asList(A, B, C).stream().sorted().collect(Collectors.toList()).get(1);
	}
}

Disclaimer: The above Problem (Second Largest) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *