Server Side Programming Articles - Page 1827 of 2650

Operator overloading in C++ to print contents of vector, map, pair ..

Ayush Gupta
Updated on 14-Apr-2020 12:02:26

616 Views

In this tutorial, we will be discussing a program to understand operator overloading in C++ to print contents of vector, map and pair.Operator overloading is the function of operators which gives them the ability to act on User defined objects and work accordingly in a similar fashion.ExampleVector Live Demo#include #include using namespace std; template ostream& operator

Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), inplace_merge

Ayush Gupta
Updated on 14-Apr-2020 11:58:44

155 Views

In this tutorial, we will be discussing a program to understand the various merge operations using STL in C++.The merge() function is used to merge two sorted containers in a way that the new container is also sorted. Further includes() is used to check if the elements from first container are present in the second one.Example Live Demo#include #include #include using namespace std; int main(){    vector v1 = {1, 3, 4, 5, 20, 30};    vector v2 = {1, 5, 6, 7, 25, 30};    //initializing resultant vector    vector v3(12);    merge(v1.begin(), v1.end(), v2.begin(),    v2.end(), v3.begin());    cout

Remove Zero Sum Consecutive Nodes from Linked List in C++

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

2K+ Views

Suppose we have given the head of a linked list; we have to repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. So after doing so, we have to return the head of the final linked list. So if the list is like [1, 2, -3, 3, 1], then the result will be [3, 1].To solve this, we will follow these steps −Create a node called dummy, and store 0 into it, set next of dummy := headcreate one map m, store dummy for the key 0 into m, set sum = 0while ... Read More

Swap For Longest Repeated Character Substring in C++

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

572 Views

Suppose we have a string text, so we are allowed to swap two of the characters in the string. We have to find the length of the longest substring with repeated characters. So if the input is like “ababa”, then the result will be 3, as if we swap first b with last a, or the last b with first a, then the longest repeated character will be “aaa”, so the length is 3.To solve this, we will follow these steps −Define a map cnt, set ret := 1, j := 0, n := size of text, v := 0, ... Read More

Longest Common Subsequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 15:41:22

470 Views

Suppose we have two strings text1 and text2, we have to return the length of their longest common subsequence. The ubsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.To solve this, we ... Read More

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

Advertisements