Server Side Programming Articles

Page 2048 of 2109

Binary Indexed Tree or Fenwick Tree in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Jan-2020 530 Views

In case of comparing with a flat array of numbers, the Fenwick tree results a much better balance between two operations: element update and prefix sum computation. In case of a flat array of m numbers, we can either store the elements, or the prefix sums. In case of first instance, calculating prefix sums needs linear time; in case of second instance, modifying or updating the array elements needs linear time (in both instances, the other operation can be accomplished in constant time). Fenwick trees permit both operations to be accomplished in O(log m) time. This is obtained by representing ...

Read More

Converting one string to other using append and delete last operations in C++

Ayush Gupta
Ayush Gupta
Updated on 29-Jan-2020 185 Views

In this tutorial, we will be discussing a program to convert one string to other using append and delete last operations.For this we will be provided with two strings. Our task is to calculate whether the first string can be converted into the second one by performing k operations of append and delete last element.Example#include using namespace std; //checking if conversion between strings is possible bool if_convert(string str1, string str2, int k){    if ((str1.length() + str2.length()) < k)    return true;    //finding common length of both string    int commonLength = 0;    for (int i = ...

Read More

Else and Switch Statements with initializers in C++17

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Jan-2020 354 Views

In many cases, we need to verify the value of something returned by a function and perform conditional operations based on this value. So our code is given below −// Some method or function return_type foo(Params) // Call function with Params and // store return in var1 auto var1 = foo(Params); if (var1 == /* some value */) {    //Perform Something } else {    //Perform Something else }Just follow the general format in all conditional if-else blocks. Firstly there is exist an optional initial statement which sets up the variable, followed by the if-else block. So the general ...

Read More

2-Satisfiability(2-SAT) Problem in C/C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Jan-2020 440 Views

Let f = (x1 ∨ y1) ∧ (x2 ∨ y2) ∧ ... ∧ (xn ∨ yn).Problem: Is f satisfiable?xi ∨ yi and and   are all equivalent. So we are converting each of (xi ∨ yi) s into those two statements.Now assume a graph with 2n vertices. In case of each of (xi∨yi) s two directed edges are addedFrom ¬xi to yiFrom ¬yi to xif is not treated as satisfiable if both ¬xi and xi are in the same SCC (Strongly Connected Component)Assume that f is treated as satisfiable. Now we want to provide values to each variable in order to satisfy ...

Read More

Print all nodes that are at distance k from a leaf node in C++

sudhir sharma
sudhir sharma
Updated on 22-Jan-2020 276 Views

In this problem, we are given a binary tree and a number K. We have to print all nodes of the tree that are at k distance from the leaf node.Binary Tree is a special tree whose each node has at max two nodes (one/two/none).The leaf node of a binary tree is the node at end of the tree.In this problem, distance from the leaf node is the node at a higher level than the leaf node. Suppose, the node at distance 2 from the leaf node at level 4 will be at level 2.Let’s take an example to understand ...

Read More

Convert given time into words in C++

Ayush Gupta
Ayush Gupta
Updated on 22-Jan-2020 384 Views

In this tutorial, we will be discussing a program to convert given time into words. For this we will be provided with a specific time in the digital format.Our task is to convert that particular time into wordsExample#include using namespace std; //printing time in words void convert_time(int h, int m){    char nums[][64] = {       "zero", "one", "two", "three", "four",       "five", "six", "seven", "eight",       "nine", "ten", "eleven", "twelve",       "thirteen", "fourteen", "fifteen",       "sixteen", "seventeen", "eighteen",       "nineteen", "twenty", "twenty       one", ...

Read More

Elements present in first array and not in second using STL in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 20-Jan-2020 255 Views

We have two arrays, the task is to compare the two arrays and find the numbers present in first array but not in the second array, using Standard Template Library (STL) in C++.ExampleInput: array1[ ] = {1, 2, 3, 4, 5, 7} array2[ ] = {2, 3, 4, 5, 6, 8} Output: 1, 7 Input: array1[ ] = {1, 20, 33, 45, 67} array2[ ] = {1, 12, 13, 114, 15, 13} Output: 20, 33, 45, 67Approach used in the below program is as follows −In this program we want to find the elements which are present in the first ...

Read More

Print all sequences starting with n and consecutive difference limited to k in C++

sudhir sharma
sudhir sharma
Updated on 17-Jan-2020 184 Views

In this problem, we are given three variables n, s, and k and we have to print all the possible sequences that start with the number n and length s having the absolute difference between consecutive elements less than k. Let's take an example to understand the topic better − Input: n = 3, s = 3 , k = 2 Output: 3 3 3 3 3 4 3 3 2 3 4 4 3 4 5 3 4 3 3 2 2 3 2 3 3 2 1 In this problem, we need to obtain the ...

Read More

Print all subsequences of a string using ArrayList in C++

sudhir sharma
sudhir sharma
Updated on 17-Jan-2020 192 Views

In this problem, we are given a string and we have to print all subsequences of the string. The substring is formed by deleting elements. Also, the order of string should not be altered.Let’s take an example to understand the problem better −Input: string = “xyz” Output: x y xy z xz yz xyzTo solve this problem, we will find all substring starting from freezing the first character of the string and find subsequence accordingly, then going for the next character in string and subsequence.Examplepublic class Main {    public static void printSubString(String sub, String subSeq){       if ...

Read More

Print all subsequences of a string in C++

sudhir sharma
sudhir sharma
Updated on 17-Jan-2020 424 Views

In this problem, we are given a string and we have to print all the subsequences of the string. The substring generated is created by deleting the elements of the string but the order remains the same(i.e. Order cannot be changed).Let’s take an example to understand the topic better −Input: xyz Output: x, y, z, xy, yz, xz, xyzExplanation − In the above example, we can see the only characters are deleted to create substring. No, rearranging takes place.There can be multiple methods to solve this problem, here we will discuss a few of them to understand methods.One is by ...

Read More
Showing 20471–20480 of 21,090 articles
Advertisements