Found 7347 Articles for C++

Minimum Bracket Addition in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:21:17

214 Views

Suppose we have a string s containing only '(' and ')', we have to find the minimum number of brackets that can be inserted to make the string balanced.So, if the input is like "(()))(", then the output will be 2 as "(()))(", this can be made balanced like "((()))()".To solve this, we will follow these steps −:= 0, cnt := 0for initialize i := 0, when i < size of s, update (increase i by 1), do −if s[i] is same as '(', then −(increase o by 1)Otherwiseif o is non-zero, then −(decrease o by 1)Otherwise(increase cnt by 1)return ... Read More

Linked List Jumps in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:19:02

253 Views

Suppose we have a singly linked list node containing positive numbers. We have to find the same linked list where every node's next points to the node val nodes ahead. If we cannot find such node, next will be null.So, if the input is like [2, 3, 10, 5, 9], then the output will be [2, 3, 15, ]To solve this, we will follow these steps −Define an array vwhile node is not null, do −insert value of node into vnode := next of noderet = new list node with value 0temp = reti := 0while i < size of ... Read More

Inverted Subtree in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:16:09

111 Views

Suppose we have two binary trees called source and target; we have to check whether there is some inversion T of source such that it is a subtree of the target. So, it means there is a node in target that is identically same in values and structure as T including all of its descendants.As we know a tree is said to be an inversion of another tree if either −Both trees are emptyIts left and right children are optionally swapped and its left and right subtrees are inversions.So, if the input is like sourceTargetthen the output will be TrueTo ... Read More

Counting Maximal Value Roots in Binary Tree in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:12:17

115 Views

Suppose we have a binary tree root; we have to count the number of nodes where its value is greater than or equal to the values of all of its descendants.So, if the input is likethen the output will be 4 as all nodes except 3, it meets the criteria.To solve this, we will follow these steps −Define a function dfs(), this will take node, if node is not null, then −return 0l := dfs(left of node)r := dfs(right of node)if val of node >= maximum of l and r, then −(increase ret by 1)x := maximum of val of ... Read More

Compress String in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:09:05

2K+ Views

Suppose we have a string s, we have to eliminate consecutive duplicate characters from the given string and return it. So, if a list contains consecutive repeated characters, they should be replaced with a single copy of the character. The order of the elements will be same as before.So, if the input is like "heeeeelllllllloooooo", then the output will be "helo"To solve this, we will follow these steps −ret := a blank stringfor initialize i := 0, when i < size of s, update (increase i by 1), do −if size of ret is non-zero and last element of ret ... Read More

Cha Cha Slide C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:07:11

141 Views

Suppose we have two strings s and t. we have to check whether s is rotation of t or not, in other words, can we get t after rotating s?So, if the input is like s = "helloworld" and t = "worldhello", then the output will be True.To solve this, we will follow these steps −if size of s0 is not equal to size of s1, then −return falses := s0 concatenate s0return true when s1 is in s, otherwise 0Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {   ... Read More

Minimum String in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:05:42

880 Views

Suppose we have two strings s and t of same length, and both are in lowercase letters. Consider we have rearranged s at first into any order, then count the minimum number of changes needed to turn s into t.So, if the input is like s = "eccynue", t = "science", then the output will be 2 as if we rearrange "eccynue" to "yccence", then replace y with s and second c with i, it will be "science".To solve this, we will follow these steps −ret := 0Define two arrays cnt1 to hold frequency of s and cnt2 to hold ... Read More

TV Shows in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:04:01

359 Views

Suppose we have a list of TV shows, and another list of duration, and an integer k, here shows[i] and duration[i] shows the name and duration watched by the ith person, we have to find the total duration watched of the k most watched shows.So, if the input is like shows: ["Castle Play", "Fairy Tale Series", "Castle Play", "Jerry Mouse", "Rich Boy"], duration: [6, 4, 6, 14, 5] and k = 2, then the output will be 26.To solve this, we will follow these steps −Define one map mn := size of vfor initialize i := 0, when i < ... Read More

Unique Fractions in C++

Arnab Chakraborty
Updated on 02-Sep-2020 12:01:58

189 Views

Suppose we have a list of fractions where each fraction contains [numerator, denominator] (numerator / denominator). We have ti find a new list of fractions such that the numbers in fractions are −In their most reduced terms. (20 / 14 becomes 10 / 7).Any duplicate fractions (after reducing) will be removed.Sorted in ascending order by their actual value.If the number is negative, the '-' sign will be with the numerator.So, if the input is like {{16, 8}, {4, 2}, {7, 3}, {14, 6}, {20, 4}, {-6, 12}}, then the output will be [[-1, 2], [2, 1], [7, 3], [5, 1]]To ... Read More

Revolving Door in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:59:33

219 Views

Suppose we have a list of requests, where requests[i] contains [t, d] indicating at time t, a person arrived at the door and either wanted to go inside (inside is indicating using 1) or go outside (outside is indicating using 0).So if there is only one door and it takes one time unit to use the door, there are few rules that we have to follow −The door starts with 'in' position and then is set to the position used by the last participant.If there's only one participant at the door at given time t, they can use the door.If ... Read More

Advertisements