XOR of All Elements in Given Range in C++

sudhir sharma
Updated on 20-Apr-2020 11:54:06

692 Views

In this problem, we are given two integer L and R denoting a range. Our task is to find xor of all elements within the range [L, R].Let’s take an example to understand the problem, Input − L=3, R = 6Explanation − 3^4^5^6 =To solve this problem, we will find the MSB of R. the MSB of the answer will not be greater than R. Now, we will find the parity of count of the number of bits from 0 to MSB.Now, to find the parity count for an ith bit, we can see that the state of an ith ... Read More

XOR of All Nodes in the Sub-Tree of a Given Node in C++

sudhir sharma
Updated on 20-Apr-2020 11:50:50

326 Views

In this problem, we are given an n tree and there are some queries that are nodes of the tree. Our task is to print the XOR of all nodes of the sub-tree formed by the given node.Let’s take an example to understand the problem, Queries − {1, 6, 5}Output −0 0 5Explanation −1^6^3^2^4^7^5 6^2^4 5To solve this problem, we will compute the xor of all nodes of the sub-tree by traversing the tree once and store it. Now, we will compute the xor of all nodes of the sub-tree if the child nodes and then computing for all given ... Read More

XOR of Numbers Appearing Even Times in Given Range in C++

sudhir sharma
Updated on 20-Apr-2020 11:48:19

190 Views

In this problem, we are given an array of n elements and there are some queries which are range from a start point to endpoint in the array. Our task is two finds the XOR of elements that appeared even a number of times in the range.Let’s take an example to understand the problem, Input −array = {1, 2, 3, 1, 1, 2, 2, 3} querries = 2 R = 4 L = 2, R = 5 L = 2, R = 7Output −0 1 0The solution to this problem is quite easy, we will find the sum of XOR ... Read More

XOR of the Path Between Any Two Nodes in a Binary Tree in C++

sudhir sharma
Updated on 20-Apr-2020 11:45:21

357 Views

In this problem, we are given a binary tree and two nodes of the binary tree. Our task is to print the XOR of all nodes that come in the path between the two nodes.Let’s take an example to understand the problem, We need to find the xor of all nodes between 2 and 3.The path from 2 to 3, 2 → 6 → 1 → 3.We will find 2^3^1^3.Output −To solve this problem, we need to find the paths from one to another node. For this, we will find the XOR of all nodes in the path from the ... Read More

XOR of Prime Frequencies of Characters in a String in C++

sudhir sharma
Updated on 20-Apr-2020 11:41:56

228 Views

In this problem, we are given a string of characters, our task is to print the XOR of frequencies of characters of the string whose frequency of occurrence is a prime number.Let’s take an example to understand the problem, Input − TutorialsPointOutput −Here, we will check the frequency of occurrence of each character of the string and then find XOR of all the characters whose frequency is a prime number. For this will create an array of prime frequencies. Then we will store frequencies of characters of a string in a map and then matching with prime frequency array. If ... Read More

XOR of Sum of Every Possible Pair of an Array in C++

sudhir sharma
Updated on 20-Apr-2020 11:39:02

296 Views

In this problem, we are given an array of n elements. Our task is to generate a sequence of size n*n whose elements are the sum of a pair of all elements of A with itself. And print the xor elements of this sum array formed.Let’s take an example to understand the problem, Input − A (1, 4, 5)Output − 0Explanation −B (1+1, 1+4, 1+5, 4+1, 4+4, 4+5, 5+1, 5+4, 5+5) B(2, 5, 6, 5, 8, 9, 6, 9, 10) Xor of all values = 2^5^6^5^8^9^6^9^10 = 0.To solve this problem, we need to know some properties of Xor. The ... Read More

Get Nth Node in a Linked List in C++

sudhir sharma
Updated on 20-Apr-2020 11:27:28

736 Views

Here, we are given a linked list and an index. We have to write a function to get Nth node in a linked list.Let’s take an example to understand the problem, Inputlinked list = 34 -> 4 -> 9 -> 1 , n = 2Output9To go to the node specified by n. We will go node by node in the linked list and increase the index count until the required nth position is reached.Program to illustrate the program, Example Live Demo#include using namespace std; class Node{    public:    int data;    Node* next; }; void insertNode(Node** head_ref, int new_data) ... Read More

Program Producing Different Results in C and C++

sudhir sharma
Updated on 20-Apr-2020 11:24:53

202 Views

Write a program that compiler and runs both in c and c++ and produces different results.There are multiple types of programs that give different results when compiled in c and c++.i. Using character literals− c and c++ both treat characters differently. In C, they are treated as integer literals whereas, in C++, they are treated as characters.Example Live Demo#include int main(){    printf("%d", sizeof('a'));    return 0; }OutputC : 4 C++: 1ii. Use of binary number − binary values are not considered as binary in c, instead treat it as integer. But in c++, they are treated as binary.Example Live Demo#include int ... Read More

Calculate Power of X to N in C++

sudhir sharma
Updated on 20-Apr-2020 11:22:37

416 Views

In this problem, we are given two integers x and n. Our task is to write a program to calculate the pow(x,n). Let's take an example to understand the problem, Input: x = 5 , n = 3 Output: 125 Example Program to calculate the pow(x,n): #include using namespace std; float myPow(float x, int y) { if (y == 0) return 1; float temp = myPow(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } int main() { float x = 5; int n = 7; cout

Calculate Size of a Tree Recursion in C++

sudhir sharma
Updated on 20-Apr-2020 11:19:20

2K+ Views

In this problem, we are given a tree and our task is to create a program to calculate the size of the tree using recursion.The size of a tree is the total number of nodes present in the tree.Let’s take an example to understand the problem, The size of the above tree is 5.To find the size of the tree, we will have to add the size of left subtree and right subtree and then increment it by 1. The recursive function will be called for both left and right subtrees of the tree. And if no subtree is found ... Read More

Advertisements