Prime Palindrome in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:27:11

1K+ Views

Suppose we have to find the smallest prime palindrome that is greater than or equal to N. So if the N is 13, then the smallest palindrome will be 101.To solve this, we will follow these steps −If N is in range 8 to 11, then return 11for i in range 1 to 99999s := i as a stringr := sreverse rnum := concatenate s and substring of r from index 1, then convert to numberif num >= N and num is prime, then return numreturn 0Let us see the following implementation to get better understanding −Example Live Demo#include using ... Read More

Find Bottom Left Tree Value in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:25:32

246 Views

Suppose we have a binary tree. We have to find the left most value of the last row of that tree. So if the tree is like −Then the output will be 7, as the last row is [7, 4], and left most element is 7.To solve this, we will follow these steps −initially define ans and lvl variable as 0define one method called solve(), this will take the tree node, and level, the level is initially 0. This will act as follows −if node is null, then returnif level > lvl, then ans := value of node and lvl ... Read More

Most Frequent Subtree Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:24:15

333 Views

Suppose we have the root of a tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all the node values formed by the subtree rooted at that node (including the node itself). The most frequent subtree sum is actually If there is a tie, return all the values with the highest frequency in any order. So if the tree is like [5, 2, -5], then it will return [2]. This is because 2 happens twice, however -5 only occurs once.To solve this, we will follow these steps −Define ... Read More

Score After Flipping Matrix in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:22:45

188 Views

Suppose we have a two dimensional matrix A where each value is 0 or 1. Here a move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s. Now after making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. So our task is to find the highest possible score. If the input is like −001110101100The output will be 39 as after toggling, the matrix will ... Read More

Circular Array Loop in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:16:09

3K+ Views

Suppose we have a circular array nums of positive and negative integer values. If a number k at an index is a positive number, then move forward k steps. Otherwise, if it's negative (-k), move backward k steps. Since the array is circular, we can assume that the next element of the last element is the first element, and the previous element of the first element is the last element. We have to check whether if there is a loop (or a cycle) in nums. A cycle must start and end at the same index and the cycle's length > ... Read More

Minimum Number of Arrows to Burst Balloons in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:15:00

317 Views

Suppose there are few spherical balloons spread in two-dimensional space. For each balloon, there are the start and end coordinates of the horizontal diameter. Start is always smaller than end. There will be at most 104 balloons. One arrow can be shot up exactly vertically from different points along the x-axis. A balloon whose position is xstart to xend bursts by an arrow shot at x if xstart = x = xend. There is no limit to the number of arrows that can be shot. Assume that an arrow once shot keeps travelling up infinitely. We have to find the ... Read More

Delete Node in a BST in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:05:03

4K+ Views

Suppose we have a binary search tree. We will take one key k, and we have to delete the given key k from the BST, and return the updated BST. So if the tree is like −And the key k = 3, then the output tree will be −To solve this, we will follow these steps −Define a method called deleteRoot() to delete the root node, this will work as followsif root is null, then return nullif root has no right subtree, then return left of rootx := inorder successor of rootset left of x as left := left of ... Read More

Non-Overlapping Intervals in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:58:31

607 Views

Suppose we have a collection of intervals; we have to find the minimum number of intervals we need to remove to make the rest of the intervals non-overlapping. So if the intervals are [[1, 2], [2, 3], [3, 4], [1, 3]], then the output will be 1, as we have to remove [1, 3] to make all others are non-overlapping.To solve this, we will follow these steps −n := size of arrayif n is 0, then return 0count := 1sort the array based on the end time of the intervalsend := end date of the first intervalfor i in range ... Read More

N-ary Tree Level Order Traversal in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:56:58

473 Views

Suppose we have an n-ary tree, we have to return the level order traversal of its nodes' values. The Nary-Tree input serialization is represented in their level order traversal. Here each group of children is separated by the null value (See examples). So the following tree can be represented as [1, null, 3, 2, 4, null, 5, 6]The output will be [[1], [3, 2, 4], [5, 6]]To solve this, we will follow these steps −make one matrix ansif root is null, return ansmake one queue q and insert rootwhile q is not emptysize := size of the queuemake one array ... Read More

Battleships in a Board in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:55:40

832 Views

Suppose we have an 2D board, we have to count how many battleships are in it. The battleships are represented with the symbol 'X', empty slots are represented with '.'s. We can assume these rules −You receive a valid board, made of only battleships or empty slots.Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.So if ... Read More

Advertisements