Server Side Programming Articles - Page 2197 of 2646

C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2

Arnab Chakraborty
Updated on 20-Aug-2019 07:15:14

192 Views

Here we will see how to get the sum of the series with n-th term as n2 – (n-1)2. The recurrence relation is like below −Tn = n2 − (n−1)2So the series is −We need to find S mod (109 + 7), where S is the sum of all terms of the given series.Example#include #define X 1000000007 using namespace std; long long getSum(long long n) {    return ((n % X) * (n % X)) % X; } int main() {    long long n = 56789;    cout

Hidden tricks of C++ related to STL

Arnab Chakraborty
Updated on 20-Aug-2019 07:07:31

277 Views

Here we will see some hidden tricks of C++ related to STL.Assign value of pairs using braces ‘{}’. We can use them to assign into tuples also.pair my_pair = make_pair(10, 20); pair my_pair2 = { 10, 20 }; //using braces pair my_pair3 = { 10, { 'A', 20 } }; //complex pairSometimes we do not remember to include lots of headers, or sometimes we forget the names of the headers, in that time we can follow this trick to include all headers.#include C++ has inbuilt GCD function. That function is not so popular so we do not know about it. ... Read More

Add all greater values to every node in the given BST

Arnab Chakraborty
Updated on 20-Aug-2019 07:04:27

185 Views

Here we will see one interesting problem, where we will add greater values to every node in one given binary search tree. So the initial and final tree will be look like below −AlgorithmbstUpdate(root, sum) −Begin    if root is null, then stop    bstUpdate(right of room, sum)    sum := sum + value of root    update root value using sum    bstUpdate(left of room, sum) EndExample#include using namespace std; class Node {    public:       int data;       Node *left, *right;    };    Node *getNode(int item) {       Node *newNode = ... Read More

Types of Polymorphisms - Ad-hoc, Inclusion, Parametric & Coercion

Arnab Chakraborty
Updated on 20-Aug-2019 07:01:05

4K+ Views

Here we will see different types of polymorphism. The types are −Ad-HocInclusionParametricCoercionThe Ad-Hoc polymorphism is called as overloading. This allows function with same name to act in different manner for different types. The function and the operator both can be overloaded. Some language does not support operator overloading, but function overloading is common.Example#include using namespace std; int add(int a, int b) {    return a + b; } string add(string a, string b) {    return a + b; //concatenate } int main() {    cout

A Puzzle using C Program

Arnab Chakraborty
Updated on 20-Aug-2019 06:57:35

726 Views

Here we will see one C puzzle question. Suppose we have two numbers 48 and 96. We have to add the first number after the second one. So final result will be like 9648. But we cannot use any logical, arithmetic, string related operations, also cannot use any pre-defined functions. So how can we do that?This is easy. We can do by using Token Pasting operator(##) in C. The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the macro definition.Example#include #define MERGE(x, ... Read More

3-Way QuickSort (Dutch National Flag)

Arnab Chakraborty
Updated on 20-Aug-2019 06:56:42

2K+ Views

Here we will see the quicksort technique but we will use three-way quicksort. The basic quicksort technique is just finding an element as pivot then partition the array around pivot, after that, recur for sub arrays on left and right of the pivot.The three-way quicksort is similar, but there are three sections. array arr[1 to n] is divided into three parts.arr[1 to i]arr[i + 1, j]arr[j + 1, n]Algorithmpartition(arr, left, right, i, j) −begin    if right – left

C++: Methods of code shortening in competitive programming?

Arnab Chakraborty
Updated on 19-Aug-2019 14:35:42

471 Views

In this section we will see some examples of code shortening strategy for competitive programming. Suppose we have to write some large amount of codes. In that code, we can follow some strategy to make them more short.We can change the type-name to make it short. Please check the code to get the ideaExample Code#include using namespace std; int main() {    long long x = 10;    long long y = 50;    cout

A Space Optimized Solution of LCS in C Program?

Arnab Chakraborty
Updated on 19-Aug-2019 14:28:42

250 Views

Here we will see one space optimized approach for LCS problem. The LCS is the longest common subsequence. If two strings are “BHHUBC” and “HYUYBZC”, then the length of the subsequence is 4. One dynamic programming approach is already their, but using the dynamic programming approach, it will take more space. We need table of order m x n, where m is the number of characters in first string, and n is the number of characters in the second string.Here we will see how to implement this algorithm using O(n) amount of auxiliary space. If we observe the old approach ... Read More

A Peterson Graph Problem in C Program?

Arnab Chakraborty
Updated on 19-Aug-2019 14:22:04

342 Views

Suppose we have one graph like below. That graph is Peterson graph. The vertices are numbered from 0 through 9. Each vertex has some letters. Let consider one walk W, in that graph, where L vertices are used. A string S with L letters is realized by the walk W when the letter sequence in W and S are same. We can visit the vertices multiple times.For example, one string S is like “ABBECCD”, this is realized by the walk (0, 1, 6, 9, 7, 2, 3). Our task is to find such walk, and if that walk is present, ... Read More

A C/C++ Pointer Puzzle?

Arnab Chakraborty
Updated on 19-Aug-2019 14:15:22

429 Views

Suppose we have one integer variable whose size is 4 byte, another pointer variable is there, whose size is 8 bytes. So what will be the output of the following?Example#include using namespace std; main() {    int a[4][5][6];    int x = 0;    int* a1 = &x;    int** a2 = &a1;    int*** a3 = &a2;    cout

Advertisements