Found 7347 Articles for C++

Check if a key is present in a C++ map or unordered_map

Arnab Chakraborty
Updated on 17-Dec-2019 12:46:43

326 Views

In C++ the Maps and unordered maps are hash tables. They use some keys and their respective key values. Here we will see how to check whether a given key is present in the hash table or not. The code will be like below −Example Live Demo#include #include using namespace std; string isPresent(map m, string key) {    if (m.find(key) == m.end())    return "Not Present";    return "Present"; } int main() {    map my_map;    my_map["first"] = 4;    my_map["second"] = 6;    my_map["third"] = 6;    string check1 = "fifth", check2 = "third";    cout

What is Array Decay in C++? How can it be prevented?

Arnab Chakraborty
Updated on 17-Dec-2019 12:43:45

108 Views

Here we will see what is Array Decay. The loss of type and dimensions of an array is called array decay. It occurs when we pass the array into a function by pointer or value. The first address is sent to the array which is a pointer. That is why, the size of array is not the original one.Let us see one example of array decay using C++ code,Example Live Demo#include using namespace std; void DisplayValue(int *p) {    cout

What are the differences between bitwise and logical AND operators in C/C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:37:17

6K+ Views

As we know the bit-wise AND is represented as ‘&’ and the logical operator is represented as ‘&&’. There are some fundamental differences between them. These are as follows −The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data.Example Live Demo#include using namespace std; int main() {    int x = 3; //...0011    int y = 7; //...0111    if (y > 1 && y > x)       cout

Find the number of players who roll the dice when the dice output sequence is given in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:33:12

91 Views

Suppose we have a string S and a number X. There are M different players who roll the dice. one player keeps on rolling the dice until he gets a number other than X. Here in the string S, S[i] represents the number at ith roll of a dice. We have to find the value of M. One constraint is that the last character in S will never be X. So for example, if string is “3662123” and X = 6, the output will be 5. This can be described as follows −First player rolls and got 3Second player rolls, ... Read More

Find the Number of Maximum Product Quadruples in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:28:18

143 Views

Suppose we have one integer array with n elements. We have to find the maximum product of a quadruple in the array. So if the array is like [3, 5, 20, 6, 10], then final product is 6000, and elements in quadruple is 10, 5, 6, 20To solve this, we will follow these steps −Sort the array in ascending orderSuppose x be the product of last four elements, y be the product of first four elements, and z be the product of first two and second two elementsReturn the max of x, y and z.Example Live Demo#include #include using namespace std; ... Read More

Find the node with minimum value in a Binary Search Tree in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:20:33

2K+ Views

Suppose we have one binary search tree. We have to find the minimum element in binary search tree. So if the BST is like below −The minimum element will be 1.As we know that the left subtree always holds the smaller elements. So if we traverse through the left subtree again and again until left is null, we can find the smallest element.Example Live Demo#include using namespace std; class node{    public:       node *left;       int val;       node *right; }; node *bst = NULL; node *getNode(){    node *newNode;    newNode = new ... Read More

Find the node whose absolute difference with X gives maximum value in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:13:47

65 Views

Suppose we have a tree, and the weights of all the nodes and an integer x. We have to find the node i, such that |weight[i] - x| is minimum. If the graph is like below, and x = 15Output will be 3. Now for different nodes, it will be like belowNode 1, |5 – 15| = 10Node 2, |10 – 15| = 5Node 3, |11 – 15| = 4Node 4, |8 – 15| = 7Node 5, |6 – 15| = 9The idea is simple. We will perform the DFS on the tree, and keep track of the node, whose ... Read More

Find the minimum value to be added so that array becomes balanced in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:08:13

153 Views

Suppose we have an array A with n elements. And the n is even. We have to find the value that needs to balance the array. As the size of the array is even, then we can make two halves. Sum of the left half and sum of the right half needs to be balanced. So if the array is like A = [1, 2, 3, 2, 5, 3] The sum of left half is 6, and sum of right half is 10. so we need 4 to balance the array.The task is simple, we will find the sum of ... Read More

Find the mean vector of a Matrix in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:05:50

123 Views

Suppose we have a matrix of order M x N, we have to find the mean vector of the given matrix. So if the matrix is like −123456789Then the mean vector is [4, 5, 6] As the mean of each column is (1 + 4 + 7)/3 = 4, (2 + 5 + 8)/3 = 5, and (3 + 6 + 9)/3 = 6From the example, we can easily identify that if we calculate the mean of each column will be the mean vector.Example Live Demo#include #define M 3 #define N 3 using namespace std; void calculateMeanVector(int mat[M][N]) {    cout

Find the maximum element in an array which is first increasing and then decreasing in C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:02:44

215 Views

Suppose we have one array, which is initially increasing then decreasing. We have to find the max value in the array. So if the array elements are like A = [8, 10, 20, 80, 100, 250, 450, 100, 3, 2, 1], then output will be 500.We can use the binary search to solve this. There are three conditions −When mid is greater than both of its adjacent elements, then mid is maximumIf mid is greater than the next element, but smaller than previous element, then max lies on the left side of mid.If mid element is smaller than the next ... Read More

Advertisements