- 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++ code to find minimal tiredness after meeting

Suppose we have two numbers a and b. Two friends are at position x = a and x = b on OX axis. Each of the friends can move by one along the line in any direction unlimited number of times. By moving, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2 and so on. Two of them want to meet one integer point on OX axis. We have to find the minimum total tiredness they should gain.

So, if the input is like a = 5; b = 10, then the output will be 9, because one of the optimal ways is the following. The first friend should move three steps to the right, and the second friend two steps to the left. So, the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.

## Steps

To solve this, we will follow these steps −

ans := |a - b| sum := ans / 2 return (sum + (ans mod 2)) * (sum + 1)

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(int a, int b){ int ans = abs(a - b); int sum = ans / 2; return (sum + (ans % 2)) * (sum + 1); } int main(){ int a = 5; int b = 10; cout << solve(a, b) << endl; }

## Input

5, 10

## Output

9

- Related Articles
- C++ code to find two substrings with one minimal substring
- How to use Net Meeting and what is the code to start and join the meeting online?
- C++ code to find xth element after removing numbers
- C++ code to find tree height after n days
- C++ code to find minimum stones after all operations
- C++ code to find position of students after coding contest
- C++ code to find money after buying and selling shares
- C++ code to find corrected text after double vowel removal
- C++ code to find final number after min max removal game
- Program to Find Out the Minimal Submatrices in Python
- C++ Program to find minimal sum of all MEX of substrings
- C++ code to get minimum sum of cards after discarding
- Find the minimal data type of a scalar value in Python
- Find the minimal data type of an array-like in Python
- C++ code to count children who will get ball after each throw