Programming Articles - Page 2042 of 3363

Print Immutable Linked List in Reverse in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:19:35

491 Views

Suppose we have an immutable linked list, we have to print out all values of each node in reverse with the help of the following interface −ImmutableListNode − This is an interface of an immutable linked list, we are given the head of the list.We have to use the following functions to access the linked list −ImmutableListNode.printValue() − This will print value of the current node.ImmutableListNode.getNext() −This will return the next node.So if the list is like: [0, -4, -1, 3, -5], then the output will be [-5, 3, -1, -4, 0]To solve this, we will follow these steps −Define ... Read More

Get Equal Substrings Within Budget in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:16:56

226 Views

Suppose we have given two strings s and t of the same length. We want to change s to t. Changing the i-th character of s to i-th character of t will assign cost as |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters. We have also given an integer maxCost. We have to find the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost.So if the input is like s = “abcd” ... Read More

Smallest String With Swaps in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:15:52

436 Views

Suppose we have given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. We can swap the characters at any pair of indices in the given pairs any number of times as we want. We have to find the lexicographically smallest string that s can be changed to after using the swaps. So if the input is like s = “dcab” and pairs = [[0, 3], [1, 2]], then the output will be “bacd”. Exchange s[0] and s[3], s = "bcad", then exchange s[1] ... Read More

Ugly Number III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:38:57

259 Views

Suppose we have to write a program to find the n-th ugly number. The Ugly numbers are positive integers which are divisible by a or b or c. So for example, if n = 3 and a = 2, b = 3 and c = 5, then the output will be 4, as the ugly numbers are [2, 3, 4, 5, 6, 8, 9, 10], the third one is 4.To solve this, we will follow these steps −make a method called ok(), this will take x, a, b, c, this will act like below −return (x/a) + (x/b) + (x/c) ... Read More

Find Smallest Common Element in All Rows in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:35:19

240 Views

Suppose we have a matrix mat where every row is sorted in non-decreasing order, we have to find the smallest common element in all rows. If there is no common element, then return -1. So if the matrix is like −1234524581035791113579The output will be 5To solve this, we will follow these steps −Define a map m, n := row count of matrix, if n is not 0, then x = column size, otherwise 0for i in range 0 to n – 1for j in range 0 to x – 1if m[mat[i, j]] + 1 = i + 1, then increase ... Read More

Minimum Knight Moves in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:32:23

921 Views

Suppose we have an infinite chessboard with coordinates from -infinity to +infinity, and we have a knight at square [0, 0]. A knight has 8 possible moves it can make, as shown below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.We have to find the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.So if the input is like x = 5 and y = 5, then the output will be 4. This will be like [0, 0] → [2, 1] ... Read More

Reverse Substrings Between Each Pair of Parentheses in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:28:04

405 Views

Suppose we have a string s that consists of lower case letters and brackets. We have to reverse the strings in each pair of matching parentheses, starting from the innermost one. And the result should not contain any brackets. So if the input is like "(hel(lowo)rld)", then the output will be "dlrlowoleh", so from the beginning, it is changed like: "(hel(lowo)rld)" → "(helowolrld)" → “dlrowoleh”.To solve this, we will follow these steps −n := size of string, make an array called par whose length is n, define a stack stfor i in range 0 to n – 1if s[i] is ... Read More

Shortest Distance to Target Color in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:23:16

276 Views

Suppose we have an array color, in which there are three colors: 1, 2 and 3. We have given some queries. Each query consists of two integers i and c, we have to find the shortest distance between the given index i and the target color c. If there is no solution, then return -1. So if the colors array is like [1, 1, 2, 1, 3, 2, 2, 3, 3], and the queries array is like [[1, 3], [2, 2], [6, 1]], the output will be [3, 0, 3]. This is because the nearest 3 from index 1 is ... Read More

Before and After Puzzle in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:18:40

185 Views

Suppose we have a list of phrases, generate a list of Before and After puzzles. Here a phrase is a string that consists of lowercase letters and spaces only. No space will be there at the starting and ending positions. There are no consecutive spaces in a phrase.The before and after puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. We have to find the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] ... Read More

Convex Polygon in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:12:32

2K+ Views

Suppose we have a list of points that form a polygon when joined sequentially, we have to find if this polygon is convex (Convex polygon definition). We have to keep in mind that there are at least 3 and at most 10, 000 points. And the coordinates are in the range -10, 000 to 10, 000.We can assume the polygon formed by given points is always a simple polygon, in other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other. So if the input is like: [[0, 0], [0, 1], ... Read More

Advertisements