Server Side Programming Articles

Page 1093 of 2109

Largest N digit number divisible by given three numbers in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 357 Views

In this tutorial, we are going to write a program that finds the largest n-digit number that is divisible by the given three numbers.Let's see the steps to solve the problem.Initialise three numbers along with n.Find the LCM of three numbers.Store the largest number with n-digits.If the largest number is divisible by n, then return it.Else check for the number obtained from subtracting remainder in the above step.ExampleLet's see the code.#include using namespace std; int LCM(int x, int y, int z) {    int ans = ((x * y) / (__gcd(x, y)));    return ((z * ans) / (__gcd(ans, ...

Read More

Largest number less than N with digit sum greater than the digit sum of N in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 463 Views

In this tutorial, we are going to write a program that finds the number less than N with digit sum greater than the digit sum of n.Let's see the steps to solve the problem.Write a function to find the digits sum.Initialise n.Write a loop that iterates from n - 1 to 1.Check the digits sum of current number with the digits sum of n.If the digits sum of current number is greater than n, then return the current number.Move to the next number.ExampleLet's see the code.#include using namespace std; int sumOfDigits(int n) {    int digitsSum = 0;   ...

Read More

Largest number less than X having at most K set bits in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 167 Views

In this tutorial, we are going to write a program that finds the largest number which is less than given x and should have at most k set bits.Let's see the steps to solve the problem.Initialise the numbers x and k.Find the set bits in the number x.Write a loop that iterates set bits count of x - k.Update the value of x with x & (x - 1).Return x.ExampleLet's see the code.#include using namespace std; int largestNumberWithKBits(int x, int k) {    int set_bit_count = __builtin_popcount(x);    if (set_bit_count

Read More

Largest number smaller than or equal to N divisible by K in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 251 Views

In this tutorial, we are going to write a program that finds the number that is smaller than or equal to N and divisible by k.Let's see the steps to solve the problem.Initialise the numbers n and k.Find the remainder with modulo operator.If the remainder is zero, then return n.Else return n - remainder.ExampleLet's see the code.#include using namespace std; int findLargerNumber(int n, int k) {    int remainder = n % k;    if (remainder == 0) {       return n;    }    return n - remainder; } int main() {    int n = 33, k = 5;    cout

Read More

Largest number with binary representation is m 1's and m-1 0's in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 259 Views

In this tutorial, we are going to write a program that finds the largest number with m 1's and m - 1 0's.Let's see the steps to solve the problem.Initialise two variables bits and result with 2 and 1 respectively.Write a loop that iterates from 1 to n.Update the iterating variable value with pow(2, bits) - 1) * (pow(2, bits - 1).If the iterative variable is less than n, then update the result with i.Increment the bits count.Return resutl.ExampleLet's see the code.#include using namespace std; long long getTheNumber(long long n) {    long bits = 2;    long long ...

Read More

Largest number with one swap allowed in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 556 Views

In this tutorial, we are going to write a program that finds the largest number with a single swap.Let's see the steps to solve the problem.Initialise the number n.Convert the integer to string.Write a loop that iterates from the ending of the string.Find the max digit and index.If the current digits is less than the max digit, then update start index with current index and end index with max digit index.If the start index is -1, then return n.Else swap the digits in the start and end indexes.Return the integer by converting.ExampleLet's see the code.#include using namespace std; int ...

Read More

Largest number with prime digits in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 453 Views

In this tutorial, we are going to write a program that finds the largest number with prime digits that is less than n.Let's see the steps to solve the problem.Write a loop that iterates from 0 to n.If the current digit is not prime.While the digit is less 2, decrement the i value. If the i value is negative, then make it 0.Update the current index value with the next smallest prime digit.From the next index, make every digit to 7.Return n.ExampleLet's see the code.#include using namespace std; bool isPrime(char c) {    return c == '2' || c ...

Read More

Largest permutation after at most k swaps in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 710 Views

In this tutorial, we are going to write a program that finds the largest permutation after at most k swaps.Let's see the steps to solve the problem.Initialise the array.Initialise an array to store the index with size n + 1.Iterate over the array and store the index of each element in the position array.Write a loop that iterate till n and k is greater than 0.Store the position of n - i element in a temp variable.Update position of current element arr[i] with position[n - i].Update the position position[n - i] with current index i.Swap the current element arr[i] with ...

Read More

Largest set with bitwise OR equal to n in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 153 Views

In this tutorial, we are going to write a program that finds the largest set with bitwise OR is equal to the given number n.Let's see the steps to solve the problem.Initialise the number n.Write a loop that iterates from 0 to n.If the i | n is equal to n, then add i to the result.Return the result.ExampleLet's see the code.#include using namespace std; void printBitWiseOrSet(int n) {    vector v;    for (int i = 0; i

Read More

Largest subarray with equal number of 0s and 1s in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 262 Views

Let's see the steps to complete the program.Initialise the array.Make all zeroes in the array to -1.Have a map an empty map to store the previous indexes.Initialise sum to 0, max length to 0 and ending index to -1.Write a loop that iterates till n.Add current element to sum.If the sum is equal to 0.Update the max length with i + 1.And ending index to i.If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.Update the max length and ending index.Else add the sum to the previous indexes map.Print the starting index ...

Read More
Showing 10921–10930 of 21,090 articles
Advertisements