if(a >= b && a >= c)
cout << a;
else if(b >= a && b >= c)
cout << b;
else
cout << c;
cout << max(a, max(b, c));
cout << max({a, b, c});
Note: max() needs #include <algorithm>.
if(a >= b && a >= c)
cout << a;
else if(b >= a && b >= c)
cout << b;
else
cout << c;
cout << max(a, max(b, c));
cout << max({a, b, c});
Note: max() needs #include <algorithm>.
Formula: (n * (n + 1)) / 2
int n = 100;
cout << (n * (n + 1)) / 2; // 5050
Note: If N is big, use long long (n * (n + 1) can overflow int).
long long n = 100000;
cout << (n * (n + 1)) / 2; // 5000050000
int n = 100, sum = 0;
for(int i = 1; i <= n; i++)
sum += i;
cout << sum;
int sum(int n){
if(n == 0) return 0;
return n + sum(n - 1);
}
Viva te 'swap without 3rd variable' khub jigges kore. Real code e std::swap use korbe.
int a = 10, b = 20;
int temp = b;
b = a;
a = temp;
a = a + b;
b = a - b;
a = a - b;
a = a ^ b;
b = a ^ b;
a = a ^ b;
int a = 10, b = 20;
swap(a, b);
Note: swap() needs #include <utility> and #include <algorithm>.
Note:
0 and 1 are not prime numbers.
2 is the smallest prime number.
A prime number is a number that can be divided only by 1 and itself.
bool isPrime(int n){
if(n < 2) return false;
for(int i = 2; i < n; i++){
if(n % i == 0) return false;
}
return true;
}
#include <iostream>
using namespace std;
int main() {
int n = 5;
bool prime = true;
if (n < 2)
prime = false;
// i*i means i × i.
// We check only up to √n.
// Example: n = 5
// i = 2 → 2×2 = 4 <= 5 → check 5 % 2
// i = 3 → 3×3 = 9 > 5 → stop
//
// We don't need to check after √n,
// because the factors after √n are already covered.
//
// Advantage: Fewer checks and faster execution.
// Time Complexity: O(√n)
// Space Complexity: O(1)
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (prime)
cout << "Prime";
else
cout << "Not Prime";
return 0;
}
Note:
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two or more numbers is the largest number that divides all of them exactly (with no remainder).
It is found using the Euclidean Algorithm, which is very important for interviews.
int a = 12, b = 18;
while(b != 0){
int temp = b;
b = a % b;
a = temp;
}
cout << a; // 6
int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
cout << gcd(12, 18); // <numeric>, C++17
cout << __gcd(12, 18);
Note: gcd() needs #include <numeric>.
int gcd(int a, int b){
while(b != 0){
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main(){
int arr[] = {12, 18, 24, 30, 36};
int len = sizeof(arr) / sizeof(arr[0]);
int result = arr[0];
for(int i = 1; i < len; i++){
result = gcd(result, arr[i]);
}
cout << result; // 6
}
Note:
LCM (Least Common Multiple) is the smallest number that is a multiple of both numbers.
Example: LCM of 12 and 18 is 36.
Formula: LCM(a, b) = (a × b) / GCD(a, b)
int lcm(int a, int b){
int x = a, y = b;
while(y != 0){
int temp = y;
y = x % y;
x = temp;
}
int gcd = x;
return (a / gcd) * b;
}
int main(){
cout << lcm(12, 18); // 36
}
int gcd(int a, int b){
while(b != 0){
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int lcm(int a, int b){
return (a / gcd(a, b)) * b;
}
int main(){
int arr[] = {5, 10, 15, 30};
int len = sizeof(arr) / sizeof(arr[0]);
int result = arr[0];
for(int i = 1; i < len; i++){
result = lcm(result, arr[i]);
}
cout << "LCM " << result; // 30
}
Note:
Let number = abcd and total digits = n.
If aⁿ + bⁿ + cⁿ + dⁿ = original number, then it is an Armstrong number.
Example 1: 153 → 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
Example 2: 1634 → 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1 + 1296 + 81 + 256 = 1634
int main() {
int n = 1634;
int len = to_string(n).length();
int temp = n;
int sum = 0;
while(temp != 0){
int digit = temp % 10;
int power = 1;
for(int i = 0; i < len; i++){
power = power * digit;
}
sum = sum + power;
temp = temp / 10;
}
if(sum == n) cout << "Armstrong";
else cout << "Not Armstrong";
}
int digit = temp % 10;
sum += (int)round(pow(digit, len));
Note: pow() returns a double, so a direct int cast can give a wrong answer (for example 124.99999 becomes 124). Use round() to be safe.
Definition:
A perfect number is a number whose sum of all divisors (excluding the number itself) is equal to the number.
Example: 6 = 1 + 2 + 3
int n = 28, sum = 0;
for(int i = 1; i <= n / 2; i++){
if(n % i == 0) sum += i;
}
if(sum == n) cout << "Perfect";
else cout << "Not Perfect";
Definition:
Two strings are anagrams if they have the same characters (same count) but in a different order.
Example: listen / silent
string a = "listen", b = "silent";
sort(a.begin(), a.end());
sort(b.begin(), b.end());
cout << (a == b ? "Anagram" : "Not Anagram");
Note: sort() needs #include <algorithm>.
A number is even if it is divisible by 2 (n % 2 == 0), otherwise it is odd.
int n = 7;
if(n % 2 == 0) cout << "Even";
else cout << "Odd";
// The last bit of an odd number is always 1
if(n & 1) cout << "Odd";
else cout << "Even";
Rule: a year is a leap year if it is divisible by 400, or divisible by 4 but not by 100.
Example: 2000 and 2024 are leap years; 1900 is not.
int y = 2024;
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
cout << "Leap Year";
else
cout << "Not Leap Year";
Formula: n! = n × (n-1) × ... × 1, and 0! = 1.
Example: 5! = 120
int n = 5;
long long fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
cout << fact; // 120
long long fact(int n){
if(n <= 1) return 1;
return n * fact(n - 1);
}
Note: 20! is the largest factorial that fits in long long.
Each term is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, 13 ...
Formula: F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1
int n = 10, a = 0, b = 1;
for(int i = 0; i < n; i++){
cout << a << " ";
int next = a + b;
a = b;
b = next;
}
// 0 1 1 2 3 5 8 13 21 34
int fib(int n){
if(n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
long long memo[100];
long long fib(int n){
if(n <= 1) return n;
if(memo[n]) return memo[n];
return memo[n] = fib(n - 1) + fib(n - 2);
}
Take the last digit with % 10, add it to the result, then drop it with / 10.
int n = 1234, rev = 0;
while(n != 0){
int digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}
cout << rev; // 4321
A number is a palindrome if it reads the same backwards. Example: 121, 1331
int n = 121, temp = n, rev = 0;
while(temp != 0){
rev = rev * 10 + temp % 10;
temp /= 10;
}
cout << (rev == n ? "Palindrome" : "Not Palindrome");
string s = "madam";
bool ok = true;
for(int i = 0, j = s.size() - 1; i < j; i++, j--)
if(s[i] != s[j]) { ok = false; break; }
cout << (ok ? "Palindrome" : "Not Palindrome");
string s = "hello";
reverse(s.begin(), s.end());
cout << s; // olleh
string s = "hello";
int i = 0, j = s.size() - 1;
while(i < j){
swap(s[i], s[j]);
i++; j--;
}
Note: reverse() needs #include <algorithm>.
string s = "Programming";
int count = 0;
for(char c : s){
c = tolower(c);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
cout << count; // 3
int n = 1234, sum = 0;
while(n > 0){
sum += n % 10;
n /= 10;
}
cout << sum; // 10
Divide by 2 repeatedly and read the remainders from bottom to top. Example: 13 = 1101
int n = 13;
string bin = "";
while(n > 0){
bin = char('0' + n % 2) + bin;
n /= 2;
}
cout << bin; // 1101
#include <bitset>
cout << bitset<8>(13); // 00001101
string b = "1101";
int dec = 0;
for(char c : b)
dec = dec * 2 + (c - '0');
cout << dec; // 13
int arr[] = {12, 35, 1, 10, 34, 1};
int first = INT_MIN, second = INT_MIN;
for(int x : arr){
if(x > first){
second = first;
first = x;
} else if(x > second && x != first){
second = x;
}
}
cout << second; // 34
vector<int> v = {1, 2, 2, 3, 4, 4, 5};
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
// 1 2 3 4 5
set<int> s(v.begin(), v.end()); // keeps one of each, sorted
int arr[] = {1,2,3,4,5};
int len = sizeof(arr) / sizeof(arr[0]);
cout << len; // 5
A do-while loop is used so that the number 0 is counted as 1 digit.
int main() {
int num = 1234;
int len = 0;
do{
num = num / 10;
len++;
}while(num != 0);
cout << len; // 4
}
int num = 1234;
int len = to_string(num).length();
cout << len;
int x = 2, n = 5;
long long result = 1;
for(int i = 0; i < n; i++){
result = result * x;
}
cout << result; // 32
// STL: cout << pow(2, 5);
int num = 234515;
int lrg = 0;
do{
int digit = num % 10;
if(digit > lrg) lrg = digit;
num = num / 10;
}while(num != 0);
cout << lrg; // 5
int num = 234515;
int small = 9;
do{
int digit = num % 10;
if(digit < small) small = digit;
num = num / 10;
}while(num != 0);
cout << small; // 1
bool isPow2(int n){
return n > 0 && (n & (n - 1)) == 0;
}
int n = 13, count = 0;
while(n){
count += n & 1;
n >>= 1;
}
// or: __builtin_popcount(13)
string s = "madam";
int l = 0, r = s.length() - 1;
bool ok = true;
while(l < r){
if(s[l] != s[r]){ ok = false; break; }
l++; r--;
}
cout << (ok ? "Palindrome" : "Not Palindrome");
string s = "madam";
string r = s;
reverse(r.begin(), r.end());
cout << (s == r ? "Palindrome" : "Not Palindrome");
int arr[] = {10, 2, 4, 2, 10, 1};
int len = sizeof(arr) / sizeof(arr[0]);
int lrg = arr[0];
int small = arr[0];
for(int i = 1; i < len; i++){
if(arr[i] > lrg) lrg = arr[i];
if(arr[i] < small) small = arr[i];
}
cout << lrg << " " << small; // 10 1
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int l = 0, r = n - 1;
while(l < r){
swap(arr[l], arr[r]);
l++; r--;
}
for(int i = 0; i < n; i++) cout << arr[i] << " ";
reverse(arr, arr + n);
int arr[] = {1, 2, 4, 5, 6};
int n = 6; // range 1..6
int expected = n * (n + 1) / 2;
int actual = 0;
for(int i = 0; i < 5; i++) actual += arr[i];
cout << expected - actual; // 3