Server Side Programming Articles - Page 1465 of 2650

Restore IP Addresses in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:55:26

466 Views

Suppose we have a string containing only digits, we have to restore it by returning all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single points.So, if the input is like "25525511135", then the output will be ["255.255.11.135", "255.255.111.35"]To solve this, we will follow these steps −Define a function convertToNum(), this will take s, start, end, num := 0for initialize i := start, when i 255, then −return 10000return numDefine a function addDots(), this will take positions, res := blank ... Read More

Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:49:21

236 Views

Suppose we have a binary tree where each path going from the root to any leaf form a valid sequence, we have to check whether a given string is a valid sequence in such binary tree or not.We will get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequenceSuppose we have a binary tree like.So, if arr = [0, 1, 0, 1], then the output will be True, as the path 0 -> 1 -> 0 -> 1 is a valid ... Read More

First Unique Number in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:45:29

627 Views

Suppose we have a queue of integers, we need to retrieve the first unique integer in that queue. We have to implement the class called FirstUnique: It will be initialized by the numbers in the queue. Define one function showFirstUnique(), this will return the value of the first unique integer of the queue and returns -1 if there is no such integer. Another method is add(value) this will insert value to the queue.So, if the input is likeInitialize with [2, 3, 4] then call the functions as follows −showFirstUnique()add(5)showFirstUnique()add(2)showFirstUnique()add(3)showFirstUnique(), then the output will be 2, 2, 3, -1 respectively.To solve ... Read More

Sentence Similarity II in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:43:12

594 Views

Suppose we have Given two arrays words1, words2 these are considered as sentences, and a list of similar word pairs, we have to check whether two sentences are similar or not. So if the input is like words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] these two are similar, if the similar word pairs are like = [["great", "good"], ["fine", "good"], ["acting", "drama"], ["skills", "talent"]].The similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are also similar. And the similarity is also symmetric. So, ... Read More

Insert into a Sorted Circular Linked List in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:40:07

407 Views

Suppose we have a node from a Circular Linked List which is sorted in increasing order, we have to define a function to insert a value insertVal into the list such that it remains a sorted circular list.The node can be a reference to any single node in the list, and may not be necessarily the first value of the circular list. If there are multiple suitable places for insertion, we can choose any place to insert the new value. If the list is empty, then we have to create a new single circular list and return the reference to ... Read More

Search in a Sorted Array of Unknown Size in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:36:59

379 Views

Suppose we have an array and that is sorted in ascending order, we have to define a function to search target in nums. If the target is present, then return its index, otherwise, return -1.The array size is not known. We can only access the array using an ArrayReader interface. There is a get function like ArrayReader.get(k) this will return the element of the array at index k.So, if the input is like array = [-1, 0, 3, 5, 9, 12], target = 9, then the output will be 4 as 9 exists in nums and its index is 4To ... Read More

Number of Distinct Islands in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:35:04

287 Views

Suppose we have a binary 2D array grid, here an island is a group of 1's (land) connected 4- directionally (horizontal or vertical.) We can assume all four edges of the grid are surrounded by water. We have to count the number of distinct islands.An island is considered to be the same as another when one island can be translated (and not rotated or reflected) to equal the other.So, if the input is like11011100000000111011then the output will be 3To solve this, we will follow these steps −Define a function dfs(), this will take x, y, grid, temp, c, if x ... Read More

Next Closest Time in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:44:10

383 Views

Suppose we have a time represented in the format "HH: MM", we have to generate the next closest time by reusing the current digits. We can use the digit an unlimited number of times.So, if the input is like "19:34", then the output will be "19:39" as the next closest time choosing from digits 1, 9, 3, 4, is 19:39. It is not 19:33, because this occurs 23 hours and 59 minutes later.To solve this, we will follow these steps −Define a function eval(), this will take x, a := convert x[0] to stringa := a + x[1]b := convert ... Read More

Path Sum IV in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:42:17

303 Views

Suppose we have a list of integers that is representing a binary tree with a depth smaller than 5. If the depth of a tree is less than 5, then this tree can be represented by a list of three-digit integers. For each integer in this list −The hundreds digit is representing the depth D of this node, 1

Equal Tree Partition in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:38:49

473 Views

Suppose we have a binary tree with n nodes, our task is to check whether it's possible to partition the tree to two trees which have the equal sum of values after deleting exactly one edge on the original tree.So, if the input is likethen the output will be true.To solve this, we will follow these steps −Define one stack stDefine a function solve(), this will take node, if node is null, then −return 0leftSum := solve(left of node)rightSum := solve(right of node)curr := val + leftSum + rightSum of nodeinsert curr into streturn currFrom the main method do the ... Read More

Advertisements