C++ Articles - Page 409 of 719

Maximum of Absolute Value Expression in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:36:17

791 Views

Suppose we have two arrays of integers with equal lengths, we have to find the maximum value of: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|. Where the maximum value is taken over all 0

Brace Expansion in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:32:58

372 Views

Suppose we have a string S that represents a list of words. Here each letter in the word has 1 or more options. If there is only one option, the letter is represented as is. If there is more than one option, then curly braces delimit the options. So for example, "{a, b, c}" will represent the options ["a", "b", "c"]. Now for example, if the input is like "{a, b, c}d{e, f}" this will represent the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].Return all words that can be formed in this manner, in lexicographical order.To solve this, we will ... Read More

Longest Repeating Substring in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:28:46

2K+ Views

Suppose we have a string S, we have to find the length of the longest repeating substring(s). We will return 0 if no repeating substring is present. So if the string is like “abbaba”, then the output will be 2. As the longest repeating substring is “ab” or “ba”.Return all words that can be formed in this manner, in lexicographical order.To solve this, we will follow these steps −n := size of Sset S := one blank space concatenated with Sset ret := 0create one matrix dp of size (n + 1) x (n + 1)for i in range 1 ... Read More

Lexicographically Smallest Equivalent String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:29:20

772 Views

Suppose we have strings A and B of the same length, now we can say A[i] and B[i] are equivalent characters. So for example, if A = "abc" and B = "cde", then we have 'a' = 'c', 'b' = 'd' and 'c' = 'e'. The equivalent characters follow the usual rules of any equivalence relation:Reflexivity: 'a' = 'a'Symmetry: 'a' = 'b' implies 'b' = 'a'Transitivity: 'a' = 'b' and 'b' = 'c' implies 'a' = 'c'Now for example, given the equivalency information from A and B above, S = "eed", "acd", and "aab" are equivalent strings, and "aab" is ... Read More

Missing Element in Sorted Array in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:18:32

453 Views

Suppose we have a sorted array A of unique numbers, we have to find the K-th missing number starting from the leftmost number of the array. So if the array is like [4, 7, 9, 10], and k = 1, then the element will be 5.To solve this, we will follow these steps −n := size of the array, set low := 0 and high := n – 1if nums[n - 1] – nums[0] + 1 – n < k, thenreturn nums[n - 1] + (k – (nums[n - 1] – nums[0] + 1 – n))while low < high – ... Read More

Minimize Rounding Error to Meet Target in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:09:51

452 Views

Suppose we have an array of prices P [p1, p2..., pn] and a target value, we have to round each price Pi to Roundi(Pi) so that the rounded array [Round1(P1), Round2(P2)..., Roundn(Pn)] sums to the given target value. Here each operation Roundi(pi) could be either Floor(Pi) or Ceil(Pi).We have to return the string "-1" if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which will be (as a string with three places after the decimal) defined as −$\displaystyle\sum\limits_{i-1}^n |Round_{i} (???? ) - ????$So if the input is like [“0.700”, “2.800”, “4.900”], and the ... Read More

Shortest Way to Form String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:04:56

336 Views

Suppose we have a string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). So if there is two strings source and target, we have to find the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, then return -1. So if source is “abc” and target is “abcbc”, then the output will be 2.To solve this, we will follow these steps −Define a string called possible, this will take s and t as inputcreate a map mfor each character c in s mark ... Read More

Last Stone Weight II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:01:51

1K+ Views

Suppose we have a collection of rocks, now each rock has a positive integer weight. In each turn, we choose any two rocks and smash them together. If the stones have weights x and y with x = 0, decrease j by 1dp[j] := false when (dp[j] and dp[j – stones[i]]) both are false, otherwise trueif dp[j] is true, then reach := max of reach and jreturn total – (2 * reach)Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int lastStoneWeightII(vector& stones) {       ... Read More

Longest String Chain in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:57:50

549 Views

Suppose we have a list of words, here each word consists of lowercase letters. So one word word1 is a predecessor of another word word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For the example of the predecessor is like, "abc" is a predecessor of "abac". Now a word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. We have to find the longest possible length of ... Read More

Robot Bounded In Circle C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:54:21

324 Views

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ... Read More

Advertisements