Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 366 of 377

Find minimum area of rectangle with given set of coordinates in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 484 Views

Suppose we have an array of some points in XY plane. We have to find the minimum area of rectangle that can be formed from these points. The side of the rectangle should be parallel to the X and Y axes. If we cannot form the rectangle, then return 0. So if the array of points is like [(1, 1), (1, 3), (3, 1), (3, 3), (2, 2)]. The output will be 4. As the rectangle can be formed using the points (1, 1), (1, 3), (3, 1) and (3, 3).To solve this, give the points by x coordinates, so ...

Read More

Find maximum vertical sum in binary tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 190 Views

Suppose we have a binary tree. The task is to print maximum of the sum of all nodes in the vertical order traversal. So if the tree is like below −The vertical order traversal is like −4 2 1 + 5 + 6 = 12 3 + 8 = 11 7 9Here the maximum is 12. The approach is simple. We will perform the vertical order traversal, then find the sum and check for maximum.Example#include #include #include #include using namespace std; class Node {    public:    int key;    Node *left, *right; }; Node* getNode(int key){    Node* node ...

Read More

Find maximum power of a number that divides a factorial in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 302 Views

Suppose we have two numbers n and fact. We have to find the largest power of n, that divides fact! (factorial of fact). So if fact = 5, and n = 2, then output will be 3. So 5! = 120, and this is divisible by 2^3 = 8.Here we will use the Legendre’s formula. This finds largest power of a prime, that divides fact!. We will find all prime factors of n, then find largest power of it, that divides fact!.So if fact is 146, and n = 15, then prime factors of n are 5 and 3. Sofor ...

Read More

Why C++ is partially Object Oriented Language?

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 2K+ Views

As we know some basic features of an object oriented programming language are the Inheritance, Encapsulation, Polymorphism. Any language that supports these features completely are known as object oriented programming languages. Some languages like C++ supports these three but not fully, so they are partially object oriented language. Let us see the reason why C++ is not known as completely object oriented language.In C++, we need the main() function to start executing, but in C++, the main functions are not present inside the class. So we can also write code without using class in C++. Some OOP languages like JAVA, ...

Read More

Find all possible outcomes of a given expression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 18-Dec-2019 304 Views

Suppose we have an arithmetic expression without parentheses. Our task is to find all possible outcomes of that expression. Suppose the expression is like 1+2*3-4, this can be interpreted like below −1+(2*(3-4)) = 1 + (2* -1) = -1(1+2)*(3-4) = 3 * -1 = -31+((2*3)-4) = 1 + (6 - 4) = 3((1+2)*3)-4 = (3 * 3) - 4 = 51+(2*3)-4 = 1 + 6 – 4 = 3To solve this problem, we have to follow these steps −Initially set res as emptyFor every operator x, do the following −Recursively evaluate all possible values on left of x, let the ...

Read More

Find the node whose absolute difference with X gives maximum value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 17-Dec-2019 143 Views

Suppose we have a tree, and the weights of all the nodes and an integer x. We have to find the node i, such that |weight[i] - x| is minimum. If the graph is like below, and x = 15Output will be 3. Now for different nodes, it will be like belowNode 1, |5 – 15| = 10Node 2, |10 – 15| = 5Node 3, |11 – 15| = 4Node 4, |8 – 15| = 7Node 5, |6 – 15| = 9The idea is simple. We will perform the DFS on the tree, and keep track of the node, whose ...

Read More

Find the smallest number X such that X! contains at least Y trailing zeros in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 159 Views

We have to take a number Y, we will find smallest number X, such that X! contains at least Y number of training zeros. For example, if Y = 2, then the value of X = 10. As X! = 3228800. It has Y number of zeros.We can solve this using binary search. The number of trailing zeros in N! is given by the count of the factors 5 in N!. X can be found using binary search in range [0, 5*Y]Example #include using namespace std; int factorCount(int n, int X) {    if (X < n)       ...

Read More

Find the smallest and second smallest elements in an array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 2K+ Views

Suppose we have an array of n elements. We have to find the first, second smallest elements in the array. First smallest is the minimum of the array, second smallest is minimum but larger than the first smallest number.Scan through each element, then check the element, and relate the condition for first, and second smallest elements conditions to solve this problem.Example #include using namespace std; int getTwoSmallest(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX;    for (int i = 0; i < n; i++) {       if (arr[i] < first) {     ...

Read More

Find the product of last N nodes of the given Linked List in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 202 Views

Consider we have few elements in a linked list. We have to find the multiplication result of last n number of elements. The value of n is also given. So if the list is like [5, 7, 3, 5, 6, 9], and n = 3, then result will be 5 * 6 * 9 = 270.The process is straight forward. We simply read the current element starting from left side, then add the elements into stack. After filling up the stack, remove n elements and multiply them with the prod. (initially prod is 1), when n number of elements are ...

Read More

Find the product of first k nodes of the given Linked List in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 04-Nov-2019 159 Views

Consider we have few elements in a linked list. We have to find the multiplication result of first k number of elements. The value of k is also given. So if the list is like [5, 7, 3, 5, 6, 9], and k = 3, then result will be 5 * 7 * 3 = 105.The processes is straight forward. We simply read the current element starting from left side, then multiply it with the prod. (initially prod is 1), when k number of elements are traversed, then stop.Example#include #include using namespace std;    class Node{       public: ...

Read More
Showing 3651–3660 of 3,768 articles
« Prev 1 364 365 366 367 368 377 Next »
Advertisements