Server Side Programming Articles

Page 1439 of 2109

Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 407 Views

Suppose we have an array of n elements. We have to find the maximum number of elements to select from the array, such that the absolute difference between any two of the chosen elements is less than or equal to 1. So if the array is like [2, 2, 3, 4, 5], then the element will be 3, so the sequence with maximum count is 2, 2, 3.The absolute difference of 0 and 1 means, that the number can be of type x and x + 1. So the idea is to store the frequencies of array elements. So if ...

Read More

Find maximum number that can be formed using digits of a given number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 585 Views

Suppose we have a number of n digits. We have to find the maximum number that can be obtained using all digits of digits of that number. So if the number is say 339625, then maximum number can be 965332.From the problem, we can see that we can easily sort the digits in non-increasing order, then print them. But we can solve this using more efficient way. We can create one array of size 10 to store the frequency of each digit, then print the numbers from 9 to 0 accordingly.Example#include #include using namespace std; int maxNumFromNum(int num) ...

Read More

Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 340 Views

Suppose we have two integers N and K. We have to find a permutation of integers from the range [1 to N] such that the number of indices (1 – base indexing) where gcd(P[i], i) > 1 is exactly K. So if N = 4 and K = 3, then output will be [1, 2, 3, 4], as gcd(1, 1) = 1, gcd(2, 2) = 2, gcd(3, 3) = 3, gcd(4, 4) = 4If we observe it carefully, we can find that gcd(i, i+1) = 1, gcd(1, i) = 1 and gcd(i, i) = i. As the GCD of any ...

Read More

Find maximum value of x such that n! % (k^x) = 0 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 254 Views

Suppose we have two integers n and k. We have to find the maximum value of x, such that n! mod (k^x) = 0. So when n = 5, and k = 2, then output will be 3. As n! = 120, now for different values of x, it will be −120 mod 2^0 = 0, 120 mod 2^1 = 0, 120 mod 2^2 = 0, 120 mod 2^3 = 0, 120 mod 2^4 = 8, 120 mod 2^5 = 24, 120 mod 2^6 = 56, 120 mod 2^7 = 120. As the max value of x = 3, the ...

Read More

Find all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 503 Views

Suppose we have a number k. Find all possible combinations of k- bit numbers with n set-bits where 1

Read More

Find minimum cost to buy all books in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 355 Views

Suppose we have an array of n elements. These are the ratings of them. Find the minimum cost to buy all books, with the following condition −Cost of each book would be at-least 1 dollarA book has higher cost than an adjacent (left or right) if rating is more than the adjacent.So for example, if the rating array is like [1, 3, 4, 3, 7, 1], Then the output is 10, As 1 + 2 + 3 + 1 + 2 + 1 = 10To solve this, we have to make two arrays called LtoR, and RtoL, and fill them ...

Read More

Find minimum shift for longest common prefix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 594 Views

Consider we have two strings A and B. The length of A and B are same. In a single shift we can rotate the string B one element. We have to find minimum required shift to get common prefix of maximum length from A and B. So if A = “computerprogramming”, and B = “programminglanguage” So the minimum shift is 8, and prefix is “programming”.Suppose we add the string B at the end of B, so B = B + B, then there is no-need to find prefix of each shift separately. So we have to find the longest prefix ...

Read More

Find count of Almost Prime numbers from 1 to N in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 462 Views

Suppose we have a number N. We have to find almost prime numbers in range 1 to N. A number is called almost prime when it has exactly two distinct factors. The numbers can have any number of non-prime factors, but should be two prime factors. So if N is 2, then output will be 2. There are two numbers 6 and 10.Here we will use the Sieve of Eratosthenes approach. Please check the following implementation to get better idea.Example#include #define N 100005 using namespace std; bool prime[N]; void SieveOfEratosthenes() {    for(int i = 0; i

Read More

Find minimum sum such that one of every three consecutive elements is taken in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 439 Views

Suppose we have an array of n elements. The task is to find the minimum sum of elements from the array. Such that at least one element one element is picked out of every three consecutive elements in that array. So if the array is like [1, 2, 3, 6, 7, 1]. The output is 4. So if we pick 3 and 1, then (3 + 1 = 4). So there are some subarrays of consecutive elements like [1, 2, 3], [2, 3, 6], [3, 6, 7], [6, 7, 1], we have picked one element from every subarray.Consider sum(i) will ...

Read More

Find duplicates in a given array when elements are not limited to a range in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have an array of N integers. Here we will print the duplicates of the given array. If no such duplicates are present, then return -1. So if the array is like [12, 15, 12, 3, 6, 12, 3, 48, 56, 8, 48], then duplicates are: [12, 3, 48]Here we will use the unordered map in C++. So at first when one element is taken, check whether that is present in the map or not, if this is present, then simply print that as duplicate, otherwise just add that into map.Example#include #include using namespace std; void displayDuplicates(int arr[], int ...

Read More
Showing 14381–14390 of 21,090 articles
Advertisements