
- 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 who cannot give sufficient candies
Suppose we have two numbers a and b. There are a and b number of candies in Amal's and Bimal's hand. Amal offered 1 candy to Bimal and Bimal gave two candies to Amal, in the next turn Amal gave 3 candies and Bimal gave 4 and so on. This continued until the moment when one of them couldn’t give the right amount of candy. They don’t consider the candies they have got from the opponent, as their own. We have to find, who is the first can’t give the right amount of candy.
So, if the input is like a = 7; b = 6, then the output will be Amal, because initially Amal gave 1, Bimal gave 2, then Amal gave 3 and Bimal gave 4, now in this turn Amal has to give 5 candies but he has only 4.
Steps
To solve this, we will follow these steps −
x := square root of a if x * (x + 1) > b, then: return "Bimal" Otherwise return "Amal"
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; string solve(int a, int b){ int x = sqrt(a); if (x * (x + 1) > b) return "Bimal"; else return "Amal"; } int main(){ int a = 7; int b = 6; cout << solve(a, b) << endl; }
Input
7, 6
Output
Amal
- Related Articles
- C++ code to find out who won an n-round game
- C++ code to count who have declined invitation
- Distribute Candies in C++
- C++ code to count children who will get ball after each throw
- C++ code to find minimum arithmetic mean deviation
- C++ code to find minimal tiredness after meeting
- C++ code to find center of inner box
- C++ code to find answers by vowel checking
- C++ code to find winner of math contest
- Distribute Candies to People in Python
- C++ code to find out number of battery combos
- C++ code to find minimum difference between concerts durations
- C++ code to find xth element after removing numbers
- C++ code to find tree height after n days
- C++ code to find total elements in a matrix
