Found 7347 Articles for C++

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

Arnab Chakraborty
Updated on 30-Oct-2019 06:33:53

2K+ 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 Live Demo#include using namespace std; int countDigitInNum(long long number, int d) {    int count = 0;    while(number){       if((number ... Read More

Find the first repeated character in a string using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:32:11

1K+ Views

Suppose we have a string; we have to find the first character that is repeated. So is the string is “Hello Friends”, the first repeated character will be l. As there are two l’s one after another.To solve this, we will use the hashing technique. Create one hash table, scan each character one by one, if the character is not present, then insert into a hash table, if it is already present, then return that character.Example Live Demo#include #include using namespace std; char getFirstRepeatingChar(string &s) {    unordered_set hash;    for (int i=0; i

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

Arnab Chakraborty
Updated on 30-Oct-2019 06:29:26

7K+ 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) Live DemoDECLARE    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

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

Arnab Chakraborty
Updated on 30-Oct-2019 06:27:31

262 Views

In this section, we will see how we can get the sum of all odd prime factors of a number in an efficient way. There is a number say n = 1092, we have to get all factors of this. The prime factors of 1092 are 2, 2, 3, 7, 13. The sum of all odd factors is 3+7+13 = 23. To solve this problem, we have to follow this rule −When the number is divisible by 2, ignore that factor, and divide the number by 2 repeatedly.Now the number must be odd. Now starting from 3 to square root ... Read More

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

Arnab Chakraborty
Updated on 30-Oct-2019 06:22:47

246 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 original array from encrypted array (An array of sums of other elements) using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:13:30

491 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 one extra character in a string using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:11:33

332 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 Live Demo#include #include using namespace std; char getExtraCharacter(string S, string T) {    unordered_map char_map; ... Read More

Find number of pairs in an array such that their XOR is 0 using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:09:40

114 Views

Suppose we have an array of n elements; we have to find a number of pairs in the array whose XOR will be 0. The pair (x, y) whose XOR is 0, then x = y. To solve it we can sort the array, then if two consecutive elements are the same, increase the count. If all elements are the same, then the last count may not be counted. In that case, we will check whether the last and first elements are the same or not, if the same, then increase the count by 1.Example#include #include using namespace std; int ... Read More

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

Arnab Chakraborty
Updated on 30-Oct-2019 06:07:35

107 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 N Geometric Means between A and B using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:04:44

113 Views

Suppose we have three integers A, B and N. We have to find N geometric means between A and B. If A = 2, B = 32, and N = 3, then the output will be 4, 8, 16The task is simple we have to insert N number of elements in the geometric Progression where A and B are the first and last term of that sequence. Suppose G1, G2, …. Gn are n geometric means. So the sequence will be A, G1, G2, …. Gn, B. So B is the (N + 2)th term of the sequence. So we ... Read More

Advertisements