Found 7347 Articles for C++

Practice Questions on Time Complexity Analysis in C++

sudhir sharma
Updated on 02-Aug-2021 12:05:47

8K+ Views

Time complexity of any algorithm is the time taken by the algorithm to complete. It is an important metric to show the efficiency of the algorithm and for comparative analysis. We tend to reduce the time complexity of algorithm that makes it more effective.Example 1Find the time complexity of the following code snippetsfor(i= 0 ; i < n; i++){    cout

Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

sudhir sharma
Updated on 04-Feb-2020 07:37:45

7K+ Views

Precision of floating point numbers is the accuracy upto which a floating point number can hold the values after decimal.For example 10/6 = 1.6666666… these have recurring decimals which can take infinite memory spaces to be stored.So to avoid memory overflow in such cases the compiler set a precision limit to the number. For float values in C++ this precision is set to 6-7 digit after that if the decimal recurs it will discard the value.So, to avoid any major losses when this discarding takes place there are methods and libraries that support the precision is float values. Here, we ... Read More

Predict the winner in Coin Game in C++

sudhir sharma
Updated on 04-Feb-2020 07:23:59

242 Views

In this game, there are two players X and Y. our task is to predict who will win the game if both play optimally and X starts the game.GameIn the coin game, there are two piles with N and M number of coins. One of the players chooses any one of the piles for the game. Then the task is to divide the piles into two halves till any one player cannot further divide the piles.Let’s take an example to understand the problem, Input: M = 2 , N = 2 Output:XExplanation - X starts the game and choose M pile(both ... Read More

Predict the winner of the game on the basis of the absolute difference of sum by selecting numbers in C++

sudhir sharma
Updated on 04-Feb-2020 07:20:06

94 Views

In this problem, we are given an array of n numbers. And there are two players X and Y. Our task is to predict the winner of the game.For player X to win the absolute difference of the sum of numbers by X and Y should be a multiple of 4. If it is not divisible by 4, then Y wins. Player X starts the game.Let’s take an example to understand the problem, Input: a[] = {3 6 9 12} Output: X Explaination: X selects 3 and 6 Y selects 12 and 9 |3+6 - 12+9| = 12, 12 is ... Read More

Prefix Sum of Matrix (Or 2D Array) in C++

sudhir sharma
Updated on 04-Feb-2020 07:13:34

3K+ Views

In this problem, we are given a 2D array of integer values mat[][]. Our task is to print the prefix sum matrix of mat.Prefix sum matrix: every element of the matrix is the sum elements above and left of it. i.eprefixSum[i][j] = mat[i][j] + mat[i-1][j]...mat[0][j] + mat[i][j-1] +... mat[i][0].Let’s take an example to understand the problemInput: arr =[    [4   6   1]    [5   7   2]    [3   8   9] ] Output:[    [4   10   11]    [9   22   25]    [12   33   45] ]To solve this problem, ... Read More

Prefix to Infix Conversion in C++

sudhir sharma
Updated on 03-Feb-2020 12:13:21

1K+ Views

In this problem, we are given a prefix expression. Our task is to print the infix conversion of the given expression.Prefix expression is those expressions which have operators before the operands.Example: +AB.Infix expressions are those expressions which have operators between the operands.Example: A+BInfix expression are information for human understanding, but the computer does computations on prefix or postfix expressions (generally postfix).Let’s take an example to understand the problemInput: prefix : /+LM/NX Output: infix : (L+M) / (N/X)To solve this problem, we will be using the stack data structure. We will traverse the prefix expression in reverse order of the expression. ... Read More

Prefix to Postfix Conversion in C++

sudhir sharma
Updated on 03-Feb-2020 12:11:32

3K+ Views

In this problem, we are given a prefix expression. Our task is to print the postfix conversion of the given expression.Prefix expression is those expressions which have operators before the operands.Example: +AB.Postfix expressions are those expressions which have operators after operands in the expressions.Example: AB/The conversion of prefix to postfix should not involve the conversion to infix.Let’s take an example to understand the problem, Input: /+XY+NM Output: XY+NM+/ Explanation: infix -> (X+Y)/(N+M)To solve this problem, we will first traverse the whole postfix expression in an reverse order. And we will be using the stack data structure for our processing. And do ... Read More

Prefixes with more a than b in C++

sudhir sharma
Updated on 03-Feb-2020 12:08:14

85 Views

In this problem, we are given string str containing only a and b and an integer N such that a string is created by appending str n times. Our task is to print the total number of substring in which the count of a’s is more than the count of b.Let’s take an example to understand the problemInput: aab 2 Output: 9 Explanation: created string is aabaab. Substrings with count(a) > count(b) : ‘a’ , ‘aa’, ‘aab’, ‘aaba’, ‘aabaa’, ‘aabaab’, ‘aba’, ‘baa’, ‘abaa’.To solve this problem, we will have to check if the string contains the required prefix subsets. Here, ... Read More

Preorder from Inorder and Postorder traversals in C++

sudhir sharma
Updated on 03-Feb-2020 12:04:00

1K+ Views

In this problem, we are given the inorder and postorder traversal of a binary tree. Our task is to print the postorder traversal of the tree.Let’s take an example to understand the problemInput:inorder: 16 7 21 12 1 5 9 postorder: 16 21 7 1 9 5 12 Output: preorder: 12 7 16 21 5 1 9 Explanation: the binary tree is :To solve this problem, a simple solution could be creating a tree using the given traversals and then finding the preorder traversal of the tree. But this method will be more complex for the system.A more effective solution ... Read More

Preorder predecessor of a Node in Binary Tree in C++

sudhir sharma
Updated on 03-Feb-2020 12:00:04

327 Views

In this problem, we are given a binary tree and a node value. Our task is to print the preorder predecessor of the node.Binary Tree is a special type of tree in which each root node can have maximum 2 child nodes.Preorder Traversal is a way to traverse nodes of the tree. In this we will first traverse root node then left child and then the right child.Preorder predecessor node is the node that comes before the node in the preorder traversal of the node.Let’s take an example to understand the problemInput: 1 Output: 9To solve this problem, a navie approach will ... Read More

Advertisements