- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to find ways to get a and b numbers of chores by brothers

Suppose we have an array A with n elements and two values a and b. Amal and Bimal are two brothers. Their parents left them home alone and commissioned them to do n chores. Each chore has its complexity. The complexity of the i-th chore equals A[i]. Amal is older, he wants to take the chores with complexity larger than some value x (A[i] > x) to leave to Bimal the chores with complexity less than or equal to x (A[i] ≤ x). Amal will do exactly a number of chores and Bimal will do exactly b number of chores (a + b = n). We have to find the number of ways x can be chosen, so that Amal got exactly a number of chores and Bimal got exactly b number of chores?

## 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 = [6, 2, 3, 100, 1]; a = 2; b = 3, then the output will be 3, because the possible values of x are 3, 4 or 5.

## Steps

To solve this, we will follow these steps −

sort the array A return A[b] - A[b - 1]

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int a, int b){ sort(A.begin(), A.end()); return A[b] - A[b - 1]; } int main(){ vector<int> A = { 6, 2, 3, 100, 1 }; int a = 2; int b = 3; cout << solve(A, a, b) << endl; }

## Input

{ 6, 2, 3, 100, 1 }, 2, 3

## Output

3

- Related Articles
- C++ Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
- Program to find number of ways we can get a number which is sum of nth power of unique numbers in Python
- Python program to find ways to get n rupees with given coins
- Program to find number of ways we can arrange symbols to get target in Python?
- C program to find sum and difference of two numbers
- Bell Numbers - Number of ways to Partition a Set in C++
- C++ Program to Find the GCD and LCM of n Numbers
- C++ Program to Find Factorial of Large Numbers
- Program to find Nth term divisible by a or b in C++
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- Program to find number of ways to split a string in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- C# program to find the maximum of three numbers
- C++ Program to Add Complex Numbers by Passing Structure to a Function