Average Number Codechef Solution|Problem Code: AVG

Average Number Codechef Solution: Hello coders, today we are going to solve Average Number Codechef Solution.

Codechef solutions

Problem

Chef had a sequence of positive integers with length N+K. He managed to calculate the arithmetic average of all elements of this sequence (let’s denote it by V), but then, his little brother deleted K elements from it. All deleted elements had the same value.

Chef still knows the remaining N elements — a sequence A1,A2,…,AN. Help him with restoring the original sequence by finding the value of the deleted elements or deciding that there is some mistake and the described scenario is impossible.

Note that the if it is possible for the deleted elements to have the same value, then it can be proven that it is unique. Also note that this value must be a positive integer.

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains three space-separated integers N, K and V.
  • The second line contains NN space-separated integers A1,A2,…,AN.

Output

For each test case, print a single line containing one integer — the value of the deleted elements, or −1−1 if there is a mistake.

Constraints

  • 1≤T≤1001≤T≤100
  • 1≤N,K≤1001≤N,K≤100
  • 1≤V≤1051≤V≤105
  • 1≤Ai≤1051≤Ai≤105 for each valid i

Example

Sample Input 1 

3
3 3 4
2 7 3
3 1 4
7 6 5
3 3 4
2 8 3

Sample Output 1 

4
-1
-1

Average Number – CodeChef Solution in JAVA

import java.util.Arrays;
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 K = sc.nextInt();
			int V = sc.nextInt();
			int[] A = new int[N];
			for (int i = 0; i < A.length; i++) {
				A[i] = sc.nextInt();
			}
			System.out.println(solve(A, K, V));
		}
		sc.close();
	}
	static int solve(int[] A, int K, int V) {
		int extra = V * (A.length + K) - Arrays.stream(A).sum();
		int value = extra / K;
		if (value > 0 && value * K == extra) {
			return value;
		} else {
			return -1;
		}
	}
}

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

Leave a Comment