Found 7197 Articles for C++

Guess Number Higher or Lower II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:25:59

309 Views

Suppose we are playing the Guess Game. The rules of the game is as follows −Player1 pick a number from 1 to n. player2 have to guess which number is picked by player1.Every time player2 guess wrong, player1 will tell whether the number that is picked is higher or lower.However, when a player guess a particular number x, and another player guess wrong, another player has to pay $x. The game will end, when player2 got the correct answer.For example if n = 10, and the player1 has taken 8In the first round, player2 tells the number is 5, that ... Read More

Find K Pairs with Smallest Sums in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:15:57

203 Views

Suppose we have two sorted arrays A1 and A2, and another value k. We have to define a pair (u, v) which is consists of one element from A1 and another element from A2. We have to find the k pairs like [(u1, v1), (u2, v2), …, (uk, vk)]. So if A1 = [1, 7, 11] and A2 = [2, 4, 6], and k = 3, then output will be [(1, 2), (1, 4), (1, 6)]To solve this, we will follow these steps −Define one data type, that will take two values a and b, and index.create one priority queue, ... Read More

Populating Next Right Pointers in Each Node II in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:04:39

143 Views

Suppose we have a binary tree, where each node has following fields: (data, left, right, next), the left will point to left subtree, right will point to right subtree, and the next pointer will point to the next node. If there is no node in the right hand side, then that will be null. So initially each next pointer is set to null, we have to make the links. Suppose the tree is like the first one, it will be converted to the next node −To solve this, we will follow these steps −set pre := root, nextPre := null ... Read More

Populating Next Right Pointers in Each Node in C++

Arnab Chakraborty
Updated on 29-Apr-2020 14:10:34

277 Views

Suppose we have a complete binary tree, where each node has following fields: (data, left, right, next), the left will point to left subtree, right will point to right subtree, and the next pointer will point to the next node. If there is no node in the right hand side, then that will be null. So initially each next pointer is set to null, we have to make the links. Suppose the tree is like the first one, it will be converted to the next node −To solve this, we will follow these steps −set pre := root, nextPre := ... Read More

Insertion Sort List in C++

Arnab Chakraborty
Updated on 04-May-2020 06:42:29

295 Views

Suppose we have a linked list, we have to perform the insertion sort on this list. So if the list is like [9, 45, 23, 71, 80, 55], sorted list is [9, 23, 45, 55, 71, 80].To solve this, we will follow these steps −dummy := new Node with some random valuenode := given listwhile node is not null, newNode = next of node, dummyHead := next of dummy, prevDummyHead := dummywhile true −if dummyHead is not present, value of dummyHead > value of nodenext of node := dummyHeadnext of prevDummyHead := nodebreak the loopprevDummyHead := dymmyHead, and dummyHead = ... Read More

Triangle in C++

Arnab Chakraborty
Updated on 28-Apr-2020 07:14:50

364 Views

Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step, we can move to adjacent numbers on the row below.For example, if the following triangle is like[       [2],      [3, 4],     [6, 5, 7],    [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the steps −Create one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for ... Read More

List in C++(4.5)

Sunidhi Bansal
Updated on 24-Apr-2020 12:15:00

158 Views

List are the type of containers that stores data in sequential manner and allocate noncontiguous memory to the elements. In C++, lists are considered as doubly linked list where insertion and deletion of elements can be done from both the ends and therefore, it is also possible to traverse the list from both the end. For using singly linked list we use the forward list available in C++ STL.Advantage of using List over vector is thatList are faster in insertion and deletion of elements available in list container if the iterator is positioned to the correct element.Disadvantage of using List ... Read More

Stack in C++ STL(3.5)

Sunidhi Bansal
Updated on 24-Apr-2020 11:55:29

445 Views

In C++ STL, stack is used as container which is implemented as LIFO structure. LIFO means Last In First Out. Stack can view as a pile of books in which the books are arranged one above the another and the last inserted book will be the first one to be removed that’s why it is called as a LIFO structure.The operations associated with the stack are -Top() - This function returns the reference to the topmost element of a stack.Syntax - name_of_stack.top()Parameters - No ParameterReturn Value - Reference to the topmost element of a stack containerPush() - This function is ... Read More

Functions in C/C++(3.5)

Sunidhi Bansal
Updated on 24-Apr-2020 11:55:57

249 Views

Functions are like a machine as they perform some functionality and produces a result of some type. Like, machine takes some input, process that input and produce an output similarly, function takes some value, operates on those value and produces the output. Manually a person passes the input to the machine then only the machine will start its functionality in the same manner when the programmer calls the function it will start executing.Functions can be different in name in various languages, but they share two common characteristics like −They contain sequence of instructions that needs to be processedThose instructions are ... Read More

Raw string literal in C++ program

Sunidhi Bansal
Updated on 22-Apr-2020 12:47:24

618 Views

In this article, we will be discussing the raw string literal in C++, its meaning, and examples.There are escape characters in C++ like “” or “\t”. When we try to print the escape characters, it will not display on the output. To show the escape characters on the output screen we use a raw string literal by using R”(String with escape characters)”. After using R in the front of the string the escape character will be displayed on the output.ExampleLet’s understand this with the help of an example Live Demo#include using namespace std; int main(){    string str = "tutorialspoint" ... Read More

Advertisements