Programming Articles - Page 1253 of 3363

Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:48:33

255 Views

In this tutorial, we are going to write a program that finds the largest number formed from the array that is divisible by 2, 3, and 5.Let's see the steps to solve the problem.Initialise the array.The number must end with 0 and the sum of all the numbers should be divisible by 3 to be divisible by 2, 3, and 5.Check for the 0 in the array and print not possible if it's not present in the array.Sort the array in descending order.Find the remainder for sum % 3.If the remainder is not 1, then delete all the digits from ... Read More

Largest number with prime digits in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:44:56

420 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. Live Demo#include using namespace std; bool isPrime(char c) {    return c == '2' || ... Read More

Largest number with one swap allowed in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:42:25

505 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. Live Demo#include using namespace std; ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:40:36

229 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. Live Demo#include using namespace std; long long getTheNumber(long long n) {    long bits = 2;    long ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:40:18

218 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. Live Demo#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

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:34:07

135 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. Live Demo#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 less than N with digit sum greater than the digit sum of N in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:32:49

421 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. Live Demo#include using namespace std; int sumOfDigits(int n) {    int digitsSum = 0; ... Read More

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

Hafeezul Kareem
Updated on 09-Apr-2021 13:29:56

320 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. Live Demo#include using namespace std; int LCM(int x, int y, int z) {    int ans = ((x * y) / (__gcd(x, y)));    return ((z * ans) / ... Read More

Largest K digit number divisible by X in C++

Hafeezul Kareem
Updated on 09-Apr-2021 13:29:35

203 Views

In this tutorial, we are going to write a program that finds the largest k-digit number that is divisible by x.Let's see the steps to solve the problem.Initialise the x and k.Find the value of pow(10, k) - 1 which is largest k-digit number.Now, remove the remainder value from the above value to get the largest k-digit number that is divisible by x.ExampleLet's see the code. Live Demo#include using namespace std; int answer(int x, int k) {    int max = pow(10, k) - 1;    return max - (max % x); } int main() {    int x = 45, k = 7;    cout

Largest gap in an array in C++

Hafeezul Kareem
Updated on 03-Dec-2024 21:59:29

325 Views

In this tutorial, we are going to write a program that finds the largest difference between the two elements in the given array. Approach Let's look at the approach we will follow to solve this problem. Firstly, we will initialize the array. Then we will find or search for the max and min elements in the array. And will return max - min. Example Here is the following code for finding the largest gap in an array in C++. #include using namespace std; ... Read More

Advertisements