
- 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 out which number can be greater
Suppose, we are given two k-digit numbers m and n. The digits of the numbers are randomly shuffled and then compared. We have to find out which number has a higher probability to be greater.
So, if the input is like n = 231, m = 337, k = 3, then the output will be ‘Second’, or the second number has a higher probability to be greater.
Steps
To solve this, we will follow these steps −
s1 := convert n to string s2 := convert m to string f := 0, s = 0 for initialize i := 0, when i < k, update (increase i by 1), do: if s1[i] > s2[i], then: (increase f by 1) otherwise when s1[i] < s2[i], then: (increase s by 1) if f > s, then: print("First") otherwise when s > f, then: print("Second") Otherwise print("Equal")
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, int m, int k) { string s1 = to_string(n); string s2 = to_string(m); int f = 0, s = 0; for(int i = 0; i < k; i++){ if(s1[i] > s2[i]) f++; else if(s1[i] < s2[i]) s++; } if(f > s) cout<<"First"<<endl; else if(s > f) cout<<"Second"<<endl; else cout<<"Equal"<<endl; } int main() { int n = 231, m = 337, k = 3; solve(n, m, k); return 0; }
Input
231, 337, 3
Output
Second
- Related Articles
- C++ code to find greater number whose factor is k
- C++ code to find out number of battery combos
- Python Program to find out the number of rooms in which a prize can be hidden
- Program to find out number of blocks that can be covered in Python
- C++ program to find out the maximum number of cells that can be illuminated
- C++ program to find out the number of coordinate pairs that can be made
- C++ code to count number of times stones can be given
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- C++ code to check phone number can be formed from numeric string
- Python Program to find out the number of sets greater than a given value
- Can I find out the next auto_increment to be used?
- Which MySQL function can be used to find out the length of the string in bits?
- C++ program to find out the number of ways a grid with boards can be colored
- C++ code to find the number of refill packs to be bought
- C++ Program to find out the number of unique matrices that can be generated by swapping rows and columns

Advertisements