Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 303 of 377

Minimize Cash Flow among a given set of friends who have borrowed money from each other in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose there are few friends. They have borrowed money from each other. So there will be some cash flow on the network. Our task is to minimize the cash flow in the network. Suppose there are three friends P1, P2, and P3. The cash flow among them is like below −This cash flow is not minimum; we have to minimize it. Then the final diagram will be like −To solve this problem we will use the greedy approach. Here in each step settle all amounts of one person and recur for remaining n-1 persons. Now the question comes, how to ...

Read More

Find K Closest Points to the Origin in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 395 Views

Suppose we have a set of points. Our task is to find K points which are closest to the origin. Suppose the points are (3, 3), (5, -1) and (-2, 4). Then the closest two (K = 2) points are (3, 3), (-2, 4).To solve this problem, we will sort the list of points based on their Euclidean distance, after that take the top most K elements from the sorted list. Those are the K nearest points.Example#include #include using namespace std; class Point{    private:    int x, y;    public:    Point(int x = 0, int y = 0){       this->x = x;       this->y = y;    }    void display(){       cout

Read More

Find Simple Closed Path for a given set of points in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 405 Views

Consider we have a set of points. We have to find a simple closed path covering all points. Suppose the points are like below, and the next image is making a closed path on those points.To get the path, we have to follow these steps −Find the bottom left point as PSort other n – 1 point based on the polar angle counterclockwise around P, if polar angle of two points are same, then put them as the distance is shortestTraverse the sorted list of points, then make the pathExample#include using namespace std; class Point {    public:   ...

Read More

Finding Quadrant of a Coordinate with respect to a Circle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 315 Views

We have one circle (center coordinate and radius), we have to find the quadrant of another given point (x, y) lies with respect to the center of the circle, if this is present in the circle, print quadrant, otherwise print error as the point is present outside.Suppose the center of the circle is (h, k), the coordinate of the point is (x, y). We know that the equation of the circle is −(𝑥−ℎ)2+(𝑦−𝑘)2+𝑟2=0Now there are few conditions, based on which we can decide the result.𝑖𝑓 (𝑥−ℎ)2+(𝑦−𝑘)2> 𝑟, 𝑡ℎ𝑒𝑛 𝑡ℎ𝑒 𝑝𝑜𝑖𝑛𝑡 𝑖𝑠 𝑜𝑢𝑡𝑠𝑖𝑑𝑒 𝑡ℎ𝑒 𝑐𝑖𝑟𝑐𝑙𝑒𝑖𝑓 (𝑥−ℎ)2+(𝑦−𝑘)2= 0, 𝑡ℎ𝑒𝑛 𝑡ℎ𝑒 𝑝𝑜𝑖𝑛𝑡 𝑖𝑠 ...

Read More

Check horizontal and vertical symmetry in binary matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 338 Views

Suppose we have a binary matrix of order M x N. The task is to check whether the matrix is horizontally symmetric, vertically symmetric or both. One matrix is said to be horizontally symmetric if the ith row is the same as the (M – i)th row, this is said to be vertically symmetric if the jth column is the same as the (N – j)th column. Suppose the input matrices are like below −011101011This is Horizontally symmetric.111101111This is Vertically symmetric.111101111This is Horizontally as well as vertically symmetric.We will solve this problem using two phases. At first, we will check ...

Read More

Check if a binary tree is subtree of another binary tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 854 Views

Suppose we have two binary trees. We have to check whether the smaller tree is a subtree of another binary tree or not. Consider these two trees are given.There are two trees. The second tree is the subtree of the first one. To check this property, we will traverse the tree in post-order fashion, then if the subtree rooted with this node is identical to the second tree, then it is subtree.Example#include using namespace std; class node {    public:    int data;    node *left, *right; }; bool areTwoTreeSame(node * t1, node *t2) {    if (t1 == ...

Read More

Check if a doubly linked list of characters is palindrome or not in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 795 Views

Here we will see how to check a string is a palindrome or not using a Doubly linked list. Here we will push each character of a string inside one doubly linked list. There will be two pointers, left and right. Then start scanning from both sides. If a left character is the same as right character, then move the left pointer to the next node, and move the right pointer to the previous node. Otherwise, return false. This process will be continued until the left and right are pointing at the same node, or the right pointer is pointing ...

Read More

Check if a given array contains duplicate elements within k distance from each in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 492 Views

Here we will see how to check whether an unsorted array has duplicate elements within k distance from each other or not. Suppose a list of elements is {1, 2, 3, 1, 4, 5}, here if k = 3, then the program will return true, as the distance between two 1s is 3.We will solve this using a hash table. The steps will be like below −Create one empty hash tableFor each index i, let the element e = arr[i] in the list, doif e is present in the hash table, then return trueotherwise, add e to hash table, and ...

Read More

Check if a given Binary Tree is SumTree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 286 Views

Here we will see how to check whether a binary tree is sum-tree or not. Now the question is what is the sum-tree. A sum-tree is a binary tree where a node will hold the sum value of its children. The root of the tree will contain an entire sum of all elements below it. This is an example of sum-tree −To check this, we will follow a simple trick, we will find the sum of left and right subtree elements if the sum value is the same as the root, then that is sum-tree. This will be one recursive ...

Read More

Check if a given circle lies completely inside the ring formed by two concentric circles in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

We have two circles. The center of both of them is at the origin. The radius of these two circles is given. They are r and R, R > r. Another circle is also present. Its radius (r1) and the center point are given, we have to check whether that point is inside the ring formed by the first two circles or not.We can solve this using the Pythagorean theorem. compute the distance from the center of the circle and origin. Then if (distance – r1) >= r and (distance – r1) = R && dis+r1

Read More
Showing 3021–3030 of 3,768 articles
« Prev 1 301 302 303 304 305 377 Next »
Advertisements