Hello Programmers In this post, you will know how to solve The Smallest Pair CodeChef Solution. The Problem Code is SMPAIR.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Problem
You are given a sequence a1, a2, …, aN. Find the smallest possible value of ai + aj, where 1 ≤ i < j ≤ N.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each description consists of a single integer N.
The second line of each description contains N space separated integers – a1, a2, …, aN respectively.
Output
For each test case, output a single line containing a single integer – the smallest possible sum for the corresponding test case.
Constraints
- T = 105, N = 2 : 13 points.
- T = 105, 2 ≤ N ≤ 10 : 16 points.
- T = 1000, 2 ≤ N ≤ 100 : 31 points.
- T = 10, 2 ≤ N ≤ 105 : 40 points.
- 1 ≤ ai ≤ 106
Example
Sample Input
1 4 5 1 3 4
Sample Output
4
Explanation
Here we pick a2 and a3. Their sum equals to 1 + 3 = 4.
The Smallest Pair CodeChef Solution In Python
T = int(input()) for i in range(T): n = int(input()) s = list(map(int, input().split())) s.sort() print(s[0] + s[1])
The Smallest Pair CodeChef Solutions In Cpp
#include <iostream> using namespace std; int main(){ int a; long b,c,d,low1,low2; cin >> a; while(a--){ low1=low2=100000000; cin >> b; while(b--){ cin >> c; if(c<low1){ low2=low1; low1=c; } else if(c<low2) low2=c; } cout << low1+low2 << endl; } }
The Smallest Pair CodeChef Solutions In JAva
import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int a; int m1, m2; while(t-- > 0) { int n = sc.nextInt(); m1 = Integer.MAX_VALUE; m2=Integer.MAX_VALUE; for (int i = 0; i < n; i++) { a = sc.nextInt(); if(a < m1) { m2 = m1; m1 = a; } else if(a >= m1 && a < m2) { m2 = a; } } System.out.println(m1 + m2); } sc.close(); } }
Disclaimer: The above Problem (The Smallest Pair) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.