Magic Pairs Codechef Solution

Hello Programmers In this post, you will know how to solve the Magic Pairs Codechef Solution. Problem Code: ALEXNUMB

Magic Pairs Codechef Solution
Magic Pairs Codechef Solution

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

Alexandra has some distinct integer numbers a1,a2…an. Count number of pairs (i,j) such that:1≤ i ≤ n1≤ j ≤ nai < aj

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 test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1a2, …, an denoting these numbers.

Output

For each test case, output a single line containing number of pairs for corresponding test case.

Constraints

  • 1 ≤ T ≤ 4
  • 1 ≤ n ≤ 100000
  • 0 ≤ ai ≤ 109
  • All the ai are distinct

Sample Input 1 

2
2
2 1
3
3 1 2

Sample Output 1 

1
3

Explanation

Case 1: Only one such pair: (2,1)

Case 2: 3 possible pairs: (2,1), (2,3), (3,1)

Ezoicreport this adMagic Pairs CodeChef Solution in JAVA

import java.util.Scanner;
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 n = sc.nextInt();
			int[] a = new int[n];
			for (int i = 0; i < a.length; i++) {
				a[i] = sc.nextInt();
			}
			System.out.println(solve(a));
		}
		sc.close();
	}
	static long solve(int[] a) {
		return (long) a.length * (a.length - 1) / 2;
	}
}

Disclaimer: The above Problem (Magic Pairs) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

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.

Next: Airline Restrictions Codechef Solution

Sharing Is Caring

Leave a Comment