Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 376 of 377

Binary Search Tree insert with Parent Pointer in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 919 Views

We can insert new node into the BST in recursive manner. In that case we return the address of the root of each subtree. Here we will see another approach, where parent pointer will need to be maintained. Parent pointer will be helpful to find ancestor of a node etc.The idea is to store the address of the left and right subtrees, we set parent pointers of the returned pointers after recursive call. This confirms that all parent pointers are set during insertion. The parent of root is set to null.Algorithminsert(node, key) −begin    if node is null, then create ...

Read More

Assign weights to edges such that longest path in terms of weights is minimized in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 181 Views

Here we will see one problem, in this problem one edge of a tree and a sum S is given. The task is to assign weights to all other weights, such that longest path in terms of weight is minimized. The sum of weights assigned to it is same as ‘S’.The approach is simple. The property of a tree that a path can have a maximum of two leaf nodes in it. That will be used to get the solution. So if we assign weights only to the edges connecting the leaf nodes, and assign other edges to 0. Then ...

Read More

Array Index with same count of even or odd numbers on both sides in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 204 Views

Here we will see one problem, suppose one array is given. There are n elements. We have to find one index, where frequency of even numbers of its left and the frequency of even numbers of its right side are same, or the frequency of odd numbers of its left is same as the frequency of odd numbers of its right. If no such result is there, return -1.Suppose the array is like {4, 3, 2, 1, 2, 4}. The output is 2. The element at index 2 is 2, there is only one odd number at left of it, ...

Read More

Area of the Largest square that can be inscribed in an ellipse in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 178 Views

Here we will see the area of largest square that can be inscribed in an ellipse. The square in ellipse will be like below −The area of ellipse is −Now, if x and y are same, thenSo area is −Example#include #include using namespace std; float area(float a, float b) {    if (a < 0 || b < 0 ) //if values are is negative it is invalid       return -1;    float area = (4*(a*a + b*b)) / (a*a*b*b);    return area; } int main() {    float a = 4, b = 2;    cout

Read More

Area of the circumcircle of any triangles with sides given in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 198 Views

Here we will see how to get the area of the circumcircle of any triangle whose sides are given. Here the side AB is a, BC is b and CA is c, the radius is ‘r’.The radius r is same as −Example#include #include using namespace std; float area(float a, float b, float c) {    if (a < 0 || b < 0 || c < 0) //if values are is negative it is invalid       return -1;    float s = (a + b + c) /2;    float triangle = sqrt(s * (s - ...

Read More

Hidden tricks of C++ related to STL

Arnab Chakraborty
Arnab Chakraborty
Updated on 20-Aug-2019 299 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Aug-2019 513 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

Read More

C++ program to read file word by word?

Arnab Chakraborty
Arnab Chakraborty
Updated on 13-Aug-2019 5K+ Views

In this section we will see how we can read file content word by word using C++. The task is very simple. we have to use the file input stream to read file contents. The file stream will open the file by using file name, then using FileStream, load each word and store it into a variable called word. Then print each word one by one.Algorithmread_word_by_word(filename)begin    file = open file using filename    while file has new word, do       print the word into the console    done endFile Content (test_file.txt)This is a test file. There are ...

Read More

Huffman Coding

Arnab Chakraborty
Arnab Chakraborty
Updated on 05-Aug-2019 9K+ Views

Huffman coding is lossless data compression algorithm. In this algorithm a variable-length code is assigned to input different characters. The code length is related with how frequently characters are used. Most frequent characters have smallest codes, and longer codes for least frequent characters.There are mainly two parts. First one to create Huffman tree, and another one to traverse the tree to find codes.For an example, consider some strings “YYYZXXYYX”, the frequency of character Y is larger than X and the character Z has least frequency. So the length of code for Y is smaller than X, and code for X ...

Read More

Amortized Complexity

Arnab Chakraborty
Arnab Chakraborty
Updated on 05-Aug-2019 4K+ Views

Amortize AnalysisThis analysis is used when the occasional operation is very slow, but most of the operations which are executing very frequently are faster. In Data structures we need amortized analysis for Hash Tables, Disjoint Sets etc.In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it executes O(n) operations. When we want to search or insert an element in a hash table for most of the cases it is constant time taking the task, but when a collision occurs, it needs O(n) times operations for collision resolution.Aggregate MethodThe aggregate method is used to ...

Read More
Showing 3751–3760 of 3,768 articles
Advertisements