Found 7197 Articles for C++

Nim Game in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:30:49

1K+ Views

Suppose we are playing a game called, Nim Game with another player. There is a heap of stones, each time one player takes turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. Player1 will take the first turn to remove the stones. Both of the players are very clever and have optimal strategies for the game. We have to devise an algorithm to determine whether player1 can win the game given the number of stones in the heap.So, if the input is like 5, then the output will be true, as ... Read More

Word Pattern in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:28:29

545 Views

Suppose we have a pattern and a string str, find if str follows the same pattern. Here follow means there is a bijection between a letter in pattern and a non-empty word in str.So, if the input is like pattern = "cbbc", str = "word pattern pattern word", then the output will be True.To solve this, we will follow these steps −strcin := strDefine an array of wordsfor each word in strcininsert word at the end of wordsDefine one map p2ii := 0pat := empty stringfor c in pattern −if c is not member of p2i, then −(increase i by ... Read More

Implement Stack using Queues in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:26:38

3K+ Views

Suppose we want to implement one stack using a queue. We have to define these methods for the stack.push(x) − Push x onto stack.pop() − Delete and return top element from stacktop() − Return the top element from stack.empty() − Return whether the stack is empty or not.So, if we call the functions push(10), push(20), then call pop(), pop(), then the output will be 20, 10To solve this, we will follow these steps −Define one deque qDefine a function push(), this will take x, insert x at the beginning of qDefine a function pop()k := first element of qdelete front ... Read More

Contains Duplicate II in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:24:55

240 Views

Suppose we have an array and an integer k, we have to check whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.So, if the input is like [1, 2, 4, 1] and k = 3, then the output will be TrueTo solve this, we will follow these steps −Define an array nn of pairsfor initialize i := 0, when i − size of nums, update (increase i by 1), do −insert {nums[i], i} at the end of nnsort the array ... Read More

Isomorphic Strings in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:23:07

4K+ Views

Suppose we have two strings s and t; we have to check whether they are isomorphic or not. Two strings are said to be isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.So, if the input is like s = "egg", t = "add”, then the output will be true, as e can map to a, and g can map to d.To solve this, we ... Read More

Excel Sheet Column Title in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:21:25

849 Views

Suppose we have a positive integer; we have to find its corresponding column title as appear in an Excel sheet. So [1 : A], [2 : B], [26 : Z], [27 : AA], [28 : AB] etc.So, if the input is like 28, then the output will be AB.To solve this, we will follow these steps −while n is non-zero, do −n := n - 1res := res + n mod 26 + ASCII of 'A'n := n / 26reverse the array resreturn resExample Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; ... Read More

Pascal's Triangle II in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:19:06

341 Views

Suppose we have a non-negative index k where k ≤ 33, we have to find the kth index row of Pascal's triangle.So, if the input is like 3, then the output will be [1,3,3,1]To solve this, we will follow these steps −Define an array pascal of size rowIndex + 1 and fill this with 0for initialize r := 0, when r

Minimum Depth of Binary Tree in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:17:28

154 Views

Suppose we have a binary tree; we have to find the minimum depth of that tree. As we know the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.So, if the input is likethen the output will be 2To solve this, we will follow these steps −Define an array aa of tree nodesinsert root at the end of aaDefine another array ak of tree nodeslevel := 0if root is null, then −return 0while size of aa is not equal to 0, do −clear the array ak(increase level by ... Read More

Same Tree in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:11:41

398 Views

Suppose we have two binary trees; we have to define a function to check whether they are the same or not. We know that the binary trees are considered the same when they are structurally identical and the nodes have the same value.So, if the input is like [1, 2, 3], [1, 2, 3], then the output will be TrueTo solve this, we will follow these steps −Define a function called isSameTree, this will take two tree nodes p and qif p is the same as NULL and q is same as NULL, then −return trueif p is the same ... Read More

Remove Duplicates from Sorted List in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:08:35

1K+ Views

Suppose we have a sorted linked list; we have to delete all duplicates such that each element appears only once.So, if the input is like [1, 1, 2, 3, 3, 3, 4, 5, 5], then the output will be [1, 2, 3, 4, 5]To solve this, we will follow these steps −dummy := make a new node with value -infnext of dummy := headcurr = dummywhile curr is non-zero, do −next = next of currwhile (next is not null and val of next is same as val of curr), do −next := next of nextnext of curr := nextcurr := ... Read More

Advertisements