The Minimum Number Of Moves | CodeChef Solution

Hello coders, today we are going to solve The Minimum Number Of Moves CodeChef Solution whose Problem Code is SALARY.

The Minimum Number Of Moves


Little chief has his own restaurant in the city. There are N workers there. Each worker has his own salary. The salary of the i-th worker equals to Wi (i = 12, …, N). Once, chief decided to equalize all workers, that is, he wants to make salaries of all workers to be equal. But for this goal he can use only one operation: choose some worker and increase by 1 salary of each worker, except the salary of the chosen worker. In other words, the chosen worker is the loser, who will be the only worker, whose salary will be not increased during this particular operation. But loser-worker can be different for different operations, of course. Chief can use this operation as many times as he wants. But he is a busy man. That’s why he wants to minimize the total number of operations needed to equalize all workers. Your task is to find this number.

Input Format

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 workers. The second line contains N space-separated integers W1W2, …, WN denoting the salaries of the workers.

Output Format

For each test case, output a single line containing the minimum number of operations needed to equalize all workers.


  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100
  • 0 ≤ Wi ≤ 10000 (104)


Sample Input

1 2 3
42 42

Sample Output



Example Case 1. Chief can equalize all salaries in 3 turns:

Turn IDIDs of involved workersSalaries after the move
11 22 3 3
21 23 4 3
31 34 4 4

Example Case 2. All salaries are already equal. He doesn’t need to do anything.

Solution – The Minimum Number Of Moves


#include <iostream>
#include <vector>
using namespace std;

int main() {
	// input number of tests
	int T;
	cin >> T;

	// loop over tests
	for (int t = 0; t < T; ++t) {

		// input number of workers
		int N;
		cin >> N;

		// input their salaries
		vector<int> W(N);
		for (int i = 0; i < N; ++i) {
			cin >> W[i];

		int moves = 0;

		while (true) {
			// checking whether all salaries are equal
			bool equal = true;
			for (int i = 1; i < N ; ++i) {
				if (W[i] != W[0]) {
					equal = false;
			// and stop the process in this case
			if(equal) {

			// otherwise we find maximum salary
			int maxW = 0;
			for (int i = 0; i < N; ++i) {
				maxW = max(maxW, W[i]);

			// and decrease by 1 all maximum salaries
			// increasing by 1 the answer after each decreased salary
			for (int i = 0; i < N; ++i) {
				if (W[i] == maxW) {

		cout << moves << endl;


t = int(input())
  print(sum(salaries) - (N * min(salaries)))


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.util.Collections;
import java.util.Arrays;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
	public static void main (String[] args) throws java.lang.Exception
	    Scanner s=new Scanner(;
	    int i=0;
		int ans;
	    int cases=s.nextInt();
		while(cases-- > 0)
		  int workers = s.nextInt();
		  int values[] = new int[workers];
			values[i] = s.nextInt();
			ans+= (values[i]-values[i-1])*(workers-i);

Disclaimer: The above Problem (The Minimum Number Of Moves) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

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