Found 7197 Articles for C++

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

Arnab Chakraborty
Updated on 18-Dec-2019 10:25:32

409 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 all combinations of k-bit numbers with n bits set where 1 <= n <= k in sorted order in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:26:54

429 Views

Suppose we have a number k. Find all possible combinations of k- bit numbers with n set-bits where 1

Find maximum vertical sum in binary tree in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:23:16

138 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 a time for which angle between hour and minute hands is given theta in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:24:37

147 Views

Suppose we have one theta, or angle value. We have to find one time in hh:mm format, that creates the angle by the hour and minute hands. Suppose the angle is 90°, then the result can be 3:00.As there are 12 hours, so there are 12 possibilities for hours and 60 possibilities for minutes. We will loop through all possible times. If angle for any time is same as given theta, then print that time.Example Live Demo#include #include using namespace std; float angleFromClockHand(int hour, int minute) {    float hour_angle = 0.5 * (hour*60 + minute);    float minute_angle = 6*minute; ... Read More

Find maximum value of x such that n! % (k^x) = 0 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:19:43

195 Views

Suppose we have two integers n and k. We have to find the maximum value of x, such that n! mod (k^x) = 0. So when n = 5, and k = 2, then output will be 3. As n! = 120, now for different values of x, it will be −120 mod 2^0 = 0, 120 mod 2^1 = 0, 120 mod 2^2 = 0, 120 mod 2^3 = 0, 120 mod 2^4 = 8, 120 mod 2^5 = 24, 120 mod 2^6 = 56, 120 mod 2^7 = 120. As the max value of x = 3, the ... Read More

Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:18:01

274 Views

Suppose we have two integers N and K. We have to find a permutation of integers from the range [1 to N] such that the number of indices (1 – base indexing) where gcd(P[i], i) > 1 is exactly K. So if N = 4 and K = 3, then output will be [1, 2, 3, 4], as gcd(1, 1) = 1, gcd(2, 2) = 2, gcd(3, 3) = 3, gcd(4, 4) = 4If we observe it carefully, we can find that gcd(i, i+1) = 1, gcd(1, i) = 1 and gcd(i, i) = i. As the GCD of any ... Read More

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

Arnab Chakraborty
Updated on 18-Dec-2019 10:16:30

257 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
Updated on 18-Dec-2019 10:15:05

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 maximum number that can be formed using digits of a given number in C++

Arnab Chakraborty
Updated on 18-Dec-2019 10:14:26

524 Views

Suppose we have a number of n digits. We have to find the maximum number that can be obtained using all digits of digits of that number. So if the number is say 339625, then maximum number can be 965332.From the problem, we can see that we can easily sort the digits in non-increasing order, then print them. But we can solve this using more efficient way. We can create one array of size 10 to store the frequency of each digit, then print the numbers from 9 to 0 accordingly.Example Live Demo#include #include using namespace std; int maxNumFromNum(int ... Read More

When should we write our own assignment operator in C++ programming?

Arnab Chakraborty
Updated on 18-Dec-2019 10:12:49

229 Views

Here we will see when we need to create own assignment operator in C++. If a class do not have any pointers, then we do not need to create assignment operator and copy constructor. C++ compiler creates copy constructor and assignment operator for each class. If the operators are not sufficient, then we have to create our own assignment operator.Example#include using namespace std; class MyClass { //no user defined assignment operator or copy constructor is present    int *ptr;    public:       MyClass (int x = 0) {          ptr = new int(x);       }       void setValue (int x) {          *ptr = x;       }       void print() {          cout

Advertisements