Programming Articles

Page 507 of 2544

Integer Replacement in C++

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

Suppose we have a positive integer n and we can do these operations as follow −If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.We have to find the minimum number of replacements needed for n to become 1?So if the number is 7, then the answer will be 4, as 7 → 8 → 4 → 2 → 1 or 7 → 6 → 3 → 2 → 1To solve this, we will follow these steps −ret := 0, n := xwhile n > 1if ...

Read More

Binary Tree Pruning in C++

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

Suppose we have the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null ...

Read More

Longest String Chain in C++

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

Suppose we have a list of words, here each word consists of lowercase letters. So one word word1 is a predecessor of another word word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For the example of the predecessor is like, "abc" is a predecessor of "abac". Now a word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. We have to find the longest possible length of ...

Read More

Pierpont Prime in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 184 Views

In this problem, we are given a number n. Our task is to print all Pierpont prime numbers less than n.Pierpont Prime number is a special type of prime number that is of the form, p= 2i . 3k + 1.Where p is a prime number, and i and k are some integers.Let’s take an example to understand the problem, Input − n = 50Output − 2, 3, 5, 7, 13, 17, 19, 37To solve this problem, we have to find all the prime numbers that follow the condition. For this, we will find a number with factors of powers ...

Read More

Random Pick Index in C++

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

Suppose we have an array of integers with possible duplicates, we have to pick the index randomly of a given target number. We can assume that the given target number must exist in the array. So if the array is like [1, 2, 3, 3, 3], then pick(3), may return 2, 3, 4 randomly.To solve this, we will follow these steps −ret := - 1, cnt := 1for i in range 0 to size of vif v[i] = target, thenif random number mod cnt = 0, then ret = icnt := cnt + 1return retExample (C++)Let us see the following ...

Read More

Does C++ compiler create default constructor when we write our own?

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 234 Views

In this tutorial, we will be discussing a program to understand if the C++ compiler creates a default constructor when we write our own.Generally, the C++ compiler uses the default constructor when no one is defined, but always uses the one defined by the user if any.Example#include using namespace std; class myInteger{ private:    int value;    //other functions in class }; int main(){    myInteger I1;    getchar();    return 0; }OutputCompiles successfullyExample#include using namespace std; class myInteger{    private:       int value;    public:       myInteger(int v) //user-defined constructor    { value = v; ...

Read More

Last Stone Weight II in C++

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

Suppose we have a collection of rocks, now each rock has a positive integer weight. In each turn, we choose any two rocks and smash them together. If the stones have weights x and y with x = 0, decrease j by 1dp[j] := false when (dp[j] and dp[j – stones[i]]) both are false, otherwise trueif dp[j] is true, then reach := max of reach and jreturn total – (2 * reach)Let us see the following implementation to get better understanding −Example#include using namespace std; class Solution {    public:    int lastStoneWeightII(vector& stones) {       int ...

Read More

Shortest Way to Form String in C++

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

Suppose we have a string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). So if there is two strings source and target, we have to find the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, then return -1. So if source is “abc” and target is “abcbc”, then the output will be 2.To solve this, we will follow these steps −Define a string called possible, this will take s and t as inputcreate a map mfor each character c in s mark ...

Read More

Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 332 Views

In this problem, we are given array arr[] and two integers M and K. our task is to create an Array using elements of the given array. The size of the new array should M and any sub-array of size greater than K cannot have all elements the same. we have to print the maximum sum possible by the created array.Let’s take an example to understand the problemInput − arr[] = {1, 2, 4, 5, 7 }, M = 5, K = 2Explanation − array created that satisfies the condition {7, 7, 5, 7, 7}. Here, no sub-array with size ...

Read More

Unique Substrings in Wraparound String in C++

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

Suppose we have the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so the value s will look like this − "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".Now we have another string p. Our job is to find out how many unique non-empty substrings of p are present in s. In particular, our input is the string p and we need to output the number of different non-empty substrings of p in the string s.So if the input is like “zab” the output will be 6. There are 6 substrings “z”, “a”, “b”, “za”, “ab”, “zab” of the string “zab” in the string sTo ...

Read More
Showing 5061–5070 of 25,433 articles
« Prev 1 505 506 507 508 509 2544 Next »
Advertisements