C++ Articles

Page 220 of 597

Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

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

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Read More

Find the count of Strictly decreasing Subarrays in C++

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

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       if ...

Read More

Find the count of substrings in alphabetic order in C++

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

Suppose we have a string of length n. It contains only uppercase letters. We have to find the number of substrings whose character is occurring in alphabetical order. Minimum size of the substring will be 2. So if the string is like: “REFJHLMNBV”, and substring count is 2, they are “EF” and “MN”.So to solve this, we will follow these steps −Check whether str[i] + 1 is same as the str[i+1], if so, then increase the result by 1, and iterate the string till next character which is out of alphabetic order, otherwise continue.Example#include using namespace std; int countSubstr(string main_str) ...

Read More

Find the fractional (or n/k – th) node in linked list in C++

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

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ...

Read More

Find the next identical calendar year in C++

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

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ...

Read More

Find the second last node of a linked list in single traversal in C++

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

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) { ...

Read More

Minimum sum of two numbers formed from digits of an array in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 613 Views

DescriptionGiven an array of digits which contains values from 0 to 9. The task is to find the minimum possible sum of two numbers formed from digits of the array. Please note that we have to use all digits of given arrayExampleIf input array is {7, 5, 1, 3, 2, 4} then minimum sum is 382 as, we can create two number 135 and 247.AlgorithmSort the array in ascending orderCreate two number by picking a digit from sorted array alternatively i.e. from even and odd indexExample#include using namespace std; int getMinSum(int *arr, int n) {    sort(arr, arr + ...

Read More

Minimum sum path between two leaves of a binary trees in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 279 Views

Problem statementGiven a binary tree in which each node element contains a number. The task is to find the minimum possible sum from one leaf node to another.ExampleIn above tree minimum sub path is -6 as follows: (-4) + 3 + 2 + (-8) + 1AlgorithmThe idea is to maintain two values in recursive calls −Minimum root to leaf path sum for the subtree rooted under current nodeThe minimum path sum between leavesFor every visited node X, we have to find the minimum root to leaf sum in left and right sub trees of X. Then add the two values ...

Read More

Minimum Sum Path in a Triangle in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 281 Views

Problem statementGiven a triangular structure of numbers, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.ExampleIf input is −   5   7 3  8 1 2 9 6 4 5Then minimum sum is 13 as follows −5 + 3 + 1 + 4AlgorithmUse memorization technique of dynamic programmingCreate 1-D array for memorization namely memorizationFor each K row use below formula −memorization[i] = min( memorization[i], memorization[i+1]) + A[k][i];Example#include using namespace std; int getMinSum(vector &arr) {    int memorization[arr.size()];    int n = arr.size() - 1;    for (int ...

Read More

Minimum swaps required to bring all elements less than or equal to k together in C++

Narendra Kumar
Narendra Kumar
Updated on 11-Mar-2026 593 Views

Problem statementGiven an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together.ExampleIf input array is = {1, 5, 4, 7, 2, 10} and k = 6 then 1 swap is required i.e. swap element 7 with 2.AlgorithmCount all elements which are less than or equals to ‘k’. Let’s say the count is ‘cnt’Using two pointer technique for window of length ‘cnt’, each time keep track of how many elements in this range are greater than ‘k’. Let’s say the total count ...

Read More
Showing 2191–2200 of 5,962 articles
« Prev 1 218 219 220 221 222 597 Next »
Advertisements