C++ Articles

Page 243 of 597

Find number of magical pairs of string of length L in C++.

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

Suppose we have two strings str1 and str2, we have to find a number of magical pairs of length L. Two strings will be magical if for every index I, the str1[i] < str2[i]. We have to count a number of pairs since the number is very large, then return the answer using modulo 109. The strings will hold only lowercase letters.The approach is simple. As we can see, if the length is L = 1, and index i = 1 is holding ‘a’, in str1 then index i = 1 of str2 will hold from ‘b’ to ‘z’ so ...

Read More

Find one extra character in a string using C++.

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

Suppose we have two strings S and T, the length of S is n, and the length of T is n + 1. The T will hold all characters that are present in S, but it will hold one extra character. Our task is to find the extra character using some efficient approach.To solve this problem, we will take one empty hash table, and insert all characters of the second string, then remove each character from the first string, the remaining character is an extra character.Example#include #include using namespace std; char getExtraCharacter(string S, string T) {    unordered_map char_map;   ...

Read More

Find original array from encrypted array (An array of sums of other elements) using C++.

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

Let us consider we have an array of integers, that array is encrypted array, Suppose the array is A = [10, 14, 12, 13, 11], the original array is B = [5, 1, 3, 2, 4], we can see that each element at index I of A follows this rule: A[i] = sum of all elements at position j in B[j], where I ≠ j. Our task is to find an original array from the encrypted one.The task is based on arithmetic observation. Suppose the array is of size 4, original array B has four elements B = [a, b, ...

Read More

Find sum of even factors of a number using C++.

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

In this section, we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factors of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.Now the number must be odd. So we will ...

Read More

Find the factorial of a number in pl/sql using C++.

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

In this section, we will see how to find factorial of a number using the PL/SQL. In PL/SQL code, some group of commands is arranged within a block of the related declaration of statements.Factorial of a number is basically multiplication of all integers from 1 to n, where n is the given number. So n! = n * (n – 1) * (n – 2) * … * 2 * 1.Example (PL/SQL)DECLARE    fact number :=1;    n number := &1; BEGIN    while n > 0 loop       fact:=n*fact;       n:=n-1;    end loop;    dbms_output.put_line(fact); END;OutputConsider 5 has given 120

Read More

Find the frequency of a digit in a number using C++.

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

Here we will see how to get the frequency of a digit in a number. Suppose a number is like 12452321, the digit D = 2, then the frequency is 3.To solve this problem, we take the last digit from the number, then check whether this is equal to d or not, if so then increase the counter, then reduce the number by dividing the number by 10. This process will be continued until the number is exhausted.Example#include using namespace std; int countDigitInNum(long long number, int d) {    int count = 0;    while(number){       if((number % ...

Read More

Find the value of ln(N!) using Recursion using C++.

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

Suppose we have a number N, our task is to find ln(N!) using recursion. ln() is basically log base e. To solve this we can use this formula −$$\ln\lgroup N!\rgroup=\ln\lgroup N*\lgroup N-1\rgroup *\lgroup N-2\rgroup *\dotsm*2*1\rgroup=\ln\lgroup N\rgroup+\ln\lgroup N+1\rgroup+\dotsm+\ln\lgroup 1\rgroup$$Example#include #include using namespace std; double factLog(int n) {    if (n

Read More

Toggle all characters in the string in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 986 Views

This program translates the characters of a string into uppercase. However, this task can be easily achieved by using the toUpper() method of the c++ class library. But in this program, we perform this by calculating the ASCII value of characters in uppercase. The Algorithm is as follows;AlgorithmSTART    Step-1: Declare the array of char    Step-2: Check ASCII value of uppercase characters which must grater than A and lesser than Z    Step-3: Check ASCII value of lower characters which must grater than A and lesser than Z ENDThe toggleChar() method gets the array of characters as an input. ...

Read More

Swap Upper diagonal with Lower in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 388 Views

This tutorial is designed to swap the upper row of a three-diagonal array to its lower one using c++ code. Moreover, if a three-diagonal array is an input, the coveted results must be something like that as;For this, the course of action is briefed in the algorithm as follows;AlgorithmStep-1: Input a diagonal array Step-2: Pass it to Swap() method Step-3: Traverse the outer loop till 3 Step-4: increment j= i+ 1 in the inner loop till 3 Step-5: put the array value in a temp variable Step-6: interchange the value arr[i][j]= arr[j][i] Step-7: put the temp data to arr[j][i] Step-8: ...

Read More

Replacing space with a hyphen in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 2K+ Views

In this C++ program, the space in the string will be replaced with the hyphen. Firstly, the length of the string is determined by the length() function of the cstring class, then hyphen is filled into the space of the sentence by traversing the string as follows.Example#include #include using namespace std; int main(){    // raw string declaration    string str = "Coding in C++ programming";    cout

Read More
Showing 2421–2430 of 5,961 articles
« Prev 1 241 242 243 244 245 597 Next »
Advertisements