Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 274 of 377

Letter Case Permutation in C++

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

Suppose we have a string with letters and numbers. We have to generate all possible combinations of that string by taking uppercase and lowercase versions of letters that are present in the string. So if one string has only numbers, only that will be returned. Suppose the string is like “1ab2”, then the strings will be [“1ab2”, “1Ab2”, “1aB2”, “1AB2”]To solve this problem, we will use recursive approach. It takes the index parameter to start work from that index. It also takes a temp string up to which the result is created. When the index is same as the string ...

Read More

Rotate String in Python

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

Suppose we have two strings, A and B. We will rotate the string A and check whether it matches with B at any position of rotating, if so then return true, otherwise false. For example, if A = 'abcde', and B = 'bcdea' So answer will be true, as A can be converted to B after rotating it.To solve this, we will follow these steps −When both A and B are empty, then return true, when both are of different length then return falseA := concatenate A after Ai := 0, and j := 0while i < length of Aif ...

Read More

Find N integers with given difference between product and sum in C++

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

Suppose we have two integers N and D. We have to find a set of N integers, where the difference between their sum and product is the same as D. Suppose the N = 3, and D = 5, then the output will be 1, 2, 8. Here the sum is 1 + 2 + 8 = 11, and product is 1 * 2 * 8 = 16, the difference between 16 and 11 is 5.We have to solve this problem; we will use one tricky method. Here we will try to find N–2 number of 1s, one 2, and ...

Read More

Find n-th element from Stern's Diatomic Series in C++

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

Here we will see how to find the nth term in Stern’s Diatomic series. The series is like 0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, … This is also known as fusc function. This series can be defined as −𝑝(𝑛)=$p\lgroup\frac{n}{2}\rgroup$ 𝑤ℎ𝑒𝑛 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛𝑝(𝑛)=$p\lgroup\frac{n-1}{2}\rgroup+p\lgroup\frac{n+1}{2}\rgroup$ 𝑤ℎ𝑒𝑛 𝑛 𝑖𝑠 𝑜𝑑𝑑𝑝(0)=0 𝑎𝑛𝑑 𝑝(1)=1Here we will use the Dynamic programming approach to reduce the number of computations. After saving the base case for p(0) and p(1), we will iterate from index i = 2 to n, and compute p(i)Example#include using namespace std; int findTerm(int n) ...

Read More

Goat Latin in Python

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

Suppose we have a set of strings (Sentence), in that set there are few words. Each words are consists of lowercase and uppercase letters. Our task is to convert the sentence into Goat-Latin form. The Goat Latin is similar to the Pig Latin. There are some few conditions.If the word starts with a vowel, then append ‘ma’ with the wordIt the word starts with a consonant, then remove that from beginning, and append it at the end, then add ‘ma’ at the end.Add one letter ‘a’ to the end of each word per its word index in the sentence, starting ...

Read More

Fair Candy Swap in Python

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

Suppose A and B are two friends. They have candy bars of different sizes. Here A[i] is the size of the i-th bar of candy owned by A, and B[j] is the size of the j-th bar of candy owned by B.Since they are friends, they want to exchange one candy bar each so that after the exchange, both A and B have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.) We have to return an integer array suppose ans, where ans[0] is ...

Read More

Reverse Only Letters in Python

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

Suppose we have a string S, we have to find the reversed string where all characters that are not a letter will not change their positions and all letters reverse their positions. So if the given string is "a-bC-dEf-ghIj", then output will be "j-Ih-gfE-dCba"To solve this, we will follow these steps −We will use the regular expression library to solve thisif S is empty, then return Sstr := an empty string, index1 := 0 and index2 := length of S – 1while index1 < length of Sif index2 >= 0 and S[index1] is alphabet and S[index2] is alphabetstr := str ...

Read More

Long Pressed Name in C++

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

Suppose a man is typing some name on keyboard. Sometimes some buttons are long-pressed by mistake. So it may type one or more extra character. So we will take two strings, and check whether the second string is long-pressed name or not. So if the name is “Amit”, and second string is “Ammittt” is longpressed name. But “Ammttt” is not, because character i is not present here.To solve this, we will follow these steps −let j := 0for i := 0, i < second.size, increase i by 1 −if j < actual_name.size and actual_name[j] = second[i], thenincrease j by 1return ...

Read More

Find sum of digits in factorial of a number in C++

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

Suppose, we have a number n, our task is to find the sum of digits in then!. Consider n = 5, then n! = 120. So the result will be 3.To solve this problem, we will create a vector to store factorial digits and initialize it with 1. Then multiply 1 to n one by one to the vector. Now sum all the elements in the vector and return the sumExample#include #include using namespace std; void vectorMultiply(vector &v, int x) {    int carry = 0, res;    int size = v.size();    for (int i = 0 ; i ...

Read More

Find sum of sum of all sub-sequences in C++

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

Consider we have an array A with n elements. We have to find the total sum of the sum of all the subsets of the array. So if the array is like A = [5, 6, 8], then it will be like −SubsetSum5566885, 6116, 8145, 8135, 6, 819Total Sum76As the array has n elements, then we have a 2n number of subsets (including empty). If we observe it clearly, then we can find that each element occurs 2n-1 timesExample#include #include using namespace std; int totalSum(int arr[], int n) {    int res = 0;    for (int i = 0; ...

Read More
Showing 2731–2740 of 3,768 articles
« Prev 1 272 273 274 275 276 377 Next »
Advertisements