
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to count minimum problems to be solved to make teams
Suppose we have an array A with n elements. There are n students in a university, n is even. The i-th student has programming skill equal to A[i]. The team leader wants to form n/2 teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal. Students can solve problems to increase their skill. If they solve one problem, their skill will be increased by 1. We have to find the minimum total number of problems students should solve to form exactly n/2 teams
Problem Category
This problem falls under sorting problems. Sorting is a very common problem while we are talking about different problem solving algorithms in computer science. As the name suggests, sorting indicates arranging a set of data into some fashion. We can arrange them in nondecreasing order or non-increasing order in general. Otherwise sorting can be also takes place in a pre-defined manner. For the string based problems, sometimes we refer lexicographical sorting to arrange letters in dictionary fashion. There are plenty of different sorting techniques with certain variations and their time and space complexity. To date, the lower-bound of the time complexity for comparison based sorting techniques is O(n*log n). However there are some mechanical sorting techniques like bucket sort, radix sort, counting sorts are there whose time complexity is linear O(n) in time. For further reading, please follow the link below −
https://www.tutorialspoint.com/data_structures_algorithms/sorting_algorithms.htm
So, if the input of our problem is like A = [5, 10, 2, 3, 14, 5], then the output will be 5, because the teams can be (2,3), (0,5) and (1,4). Then, to form the first team the third student should solve 1 problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve 4 problems.
Steps
To solve this, we will follow these steps −
n := size of A cnt := 0 sort the array A for initialize i := 0, when i < n, update i := i + 2, do: cnt := cnt + A[i + 1] - A[i] return cnt
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ int n = A.size(); int cnt = 0; sort(A.begin(), A.end()); for (int i = 0; i < n; i += 2) cnt += A[i + 1] - A[i]; return cnt; } int main(){ vector<int> A = { 5, 10, 2, 3, 14, 5 }; cout << solve(A) << endl; }
Input
{ 5, 10, 2, 3, 14, 5 }
Output
5
- Related Articles
- C++ program to count number of problems can be solved from left or right end of list
- Program to count minimum invalid parenthesis to be removed to make string correct in Python
- C++ Program to count number of teams can be formed for coding challenge
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to count minimum number of operations to flip columns to make target in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to count number of minimum swaps required to make it palindrome in Python
- Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Program to find minimum number of characters to be added to make it palindrome in Python
- Amplifier Gains – Solved Problems on Power & Voltage Ratios
- Wireless Channel Noise: Solved Problems on Noise Power
- Path Loss - Solved Numerical Problems from Wireless Communications
- C++ Program to count operations to make filename valid
- Program to find minimum operations to make array equal using Python
