Found 26504 Articles for Server Side Programming

Find the number of ways to divide number into four parts such that a = c and b = d in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:42:06

203 Views

Suppose we have a number n. We have to find number of ways to divide a number into parts (a, b, c and d) such that a = c, and b = d. So if the number is 20, then output will be 4. As [1, 1, 9, 9], [2, 2, 8, 8], [3, 3, 7, 7] and [4, 4, 6, 6]So if N is odd, then answer will be 0. If the number is divisible by 4, then answer will be n/4 – 1 otherwise n/4.Example Live Demo#include using namespace std; int countPossiblity(int num) {    if (num % 2 == 1)       return 0;    else if (num % 4 == 0)       return num / 4 - 1;    else       return num / 4; } int main() {    int n = 20;    cout

Find distance between two nodes of a Binary Tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:40:31

252 Views

Consider we have a binary tree with few nodes. We have to find the distance between two nodes u and v. suppose the tree is like below −Now the distance between (4, 6) = 4, path length is 4, length between (5, 8) = 5 etc.To solve this problem, we will find the LCA (Lowest Common Ancestor), then calculate distance from LCA to two nodes.Example Live Demo#include using namespace std; class Node {    public:       int data;    Node *left, *right; }; Node* getNode(int data) {    Node* node = new Node;    node->data = data;    node->left ... Read More

Find the number of jumps to reach X in the number line from zero in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:38:56

273 Views

Suppose we have an integer X. We have to find minimum number of jumps required to reach X from 0. The first jump made can be of length one unit and each successive jump will be exactly one unit longer than the previous jump in length. It is allowed to go either left or right in each jump. So if X = 8, then output is 4. 0 → -1 → 1→ 4 → 8 are possible stages.If we observe carefully, then we can say thatIf you have always jumped in the right direction, then after n jumps you will ... Read More

Find count of common nodes in two Doubly Linked Lists in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:37:15

273 Views

Suppose we have two doubly-linked lists. We have to find the total number of common nodes in both the doubly linked list. So if two lists are like [15, 16, 10, 9, 7, 17], and [15, 16, 40, 6, 9], there are three common nodes.Traverse both lists up to end of the list using two nested loops, for every node in the list, check if it is matched with any node of the second list or not. If a match is found, then increase the counter, and finally return the count.Example Live Demo#include using namespace std; class Node {    public: ... Read More

Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:35:29

159 Views

Suppose, we have an integer N, we have to find the number of integers 1 < x < N, for which x and x + 1 has same number of positive divisors. So if N = 3, then output will be 1, as divisor of 1 is 1, divisor of 2 is 1 and 2, and divisor of 3 is 1 and 3.To solve this, we will find the number of divisors of all numbers below N, and store them in an array. Then count number of integers x such that x, such that x + 1 have the same ... Read More

Find consecutive 1s of length >= n in binary representation of a number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:33:51

251 Views

Suppose, we have two integers x and n, our task is to search for the first consecutive stream of 1s (32-bit binary) which is greater than or equal to the value of n in length and return its position. If no such string exists, then return -1. For example, if x = 35, and n = 2, then result will be 31. The binary representation of 35 in a 32-bit integer is like −00000000000000000000000000100011. So two consecutive 1s are present at index 31, so the answer is 31.To solve this problem, we have to find the number of leading zeros, ... Read More

Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:32:10

193 Views

Suppose, we have a number n. Our task is to find the number of integers from 1 to n, which contains digits 0s and 1s only. So if n = 15, then output will be. As the numbers are 1, 10, 11To solve this, we will create integers using 0s and 1s using recursive function. Following code will help us to understand this better.Example Live Demo#include using namespace std; int numberOfValues(int p, int n) {    if (p > n)       return 0;    return 1 + numberOfValues(p * 10, n) + numberOfValues(p * 10 + 1, n); } int main() {    int n = 120;    cout

Find common elements in three sorted arrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:27:05

717 Views

Suppose we have three arrays with some elements. We have to find all the common elements that are present in these three arrays. Suppose these elements are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three arrays are 10, 12 and 15.Suppose current element traversed in array A1 be x, A2 be y and A3 be z. We can have the following cases for them −If x, y, and z are same, then we will print any of them, and increase each array elements by 1When ... Read More

Find the number of binary strings of length N with at least 3 consecutive 1s in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:24:56

379 Views

Suppose, we have an integer N, We have to find the number of all possible distinct binary strings of the length N, which have at least three consecutive 1s. So if n = 4, then the numbers will be 0111, 1110, 1111, so output will be 3.To solve this, we can use the Dynamic programming approach. So DP(i, x) indicates number of strings of length i with x consecutive 1s in position i + 1 to i + x. Then the recurrence relation will be like −DP(i, x) = DP(i – 1, 0) + DP(i – 1, x + 1).The ... Read More

Find common elements in three linked lists in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:23:38

229 Views

Suppose we have three linked lists. We have to find all the common elements that are present in these three linked lists. Suppose these lists are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three lists are 10, 12 and 15.We will use the hashing technique to solve this problem. To solve this, we have to follow these steps −Create an empty hash table, and go through each element in the first table, and insert the elements, and mark the frequency as 1Iterate through the second ... Read More

Advertisements