Programming Articles

Page 1456 of 2547

Find if it is possible to reach the end through given transitions in C++

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

Suppose we have n points on x-axis and the list of allowed translation between the points. Find if it is possible to reach the end from starting point through these transactions only. So if there is a translation between points x1 and x2, then we can move from point x to any intermediate points between x1 and x2, or directly to x2. So if n = 5. And transactions are 0 to 2, 2 to 4, and 3 to 5. Then output will be YES. There is a path from 0→2→3→5.We have to sort the list according to the first ...

Read More

Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array in C++

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

Suppose, we have an array of N elements. These elements are either 0 or 1. Find the position of 0 to be replaced with 1 to get longest contiguous sequence of 1s. Suppose the array is like arr = [1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1], the output index is 9. Replacing 0 with 1 at index 9 cause the maximum contiguous sequence of 1sWe have to keep track of three indexes. current index(curr), previous zero index(pz), and previous to previous zero index (ppz). Now traverse the array when array element is 0, then ...

Read More

Find Largest Special Prime which is less than or equal to a given number in C++

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

Suppose we have a number n. We have to find the largest special prime which is less than or equal to N. The special prime is a number, which can be created by placing digits one after another, so all the resultant numbers are prime.Here we will use Sieve Of Eratosthenes. We will create the sieve array up to the number n. Then start iteratively back from the number N, by checking if the number is prime. When this is prime, check whether this is special prime or not.Example#include using namespace std; bool isSpecialPrime(bool sieve[], int num) {    while ...

Read More

Find length of longest subsequence of one string which is substring of another string in C++

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

Suppose, we have two strings X and Y, and we have to find the length of longest subsequence of string X, which is substring in sequence Y. So if X = “ABCD” and Y = “BACDBDCD”, then output will be 3. As “ACD” is the longest sub-sequence of X, which is substring of Y.Here we will use the dynamic programming approach to solve this problem. So if the length of X is n, and length of Y is m, then create DP array of order (m+1)x(n+1). Value of DP[i, j] is maximum length of subsequence of X[0…j], which is substring ...

Read More

Sort in C++ Standard Template Library (STL)

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

Here we will see how to use the sort() function of C++ STL to sort an array So if the array is like A = [52, 14, 85, 63, 99, 54, 21], then the output will be [14 21 52 54 63 85 99]. To sort we will use the sort() function, that is present in the header file . The code is like below −Example#include #include using namespace std; int main() {    int arr[] = {52, 14, 85, 63, 99, 54, 21};    int n = sizeof(arr) / sizeof(arr[0]);    cout

Read More

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 406 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 339 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 251 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 502 Views

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

Read More
Showing 14551–14560 of 25,466 articles
Advertisements