Found 7347 Articles for C++

Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:31:23

70 Views

Given a number N. We have to find the count of such numbers that can be formed using digit 3 and 4. So if N = 6, then the numbers will be 3, 4, 33, 34, 43, 44.We can solve this problem if we look closely, for single digit number it has 2 numbers 3 and 4, for digit 2, it has 4 numbers 33, 34, 43, 44. So for m digit numbers, it will have 2m values.Example Live Demo#include #include using namespace std; long long countNumbers(int n) {    return (long long)(pow(2, n + 1)) - 2; } int main() {    int n = 3;    cout

Find the closest and smaller tidy number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:23:29

89 Views

Suppose we have a number n, we have to find the closest and smaller tidy number of n. So a number is called tidy number, if all of its digits are sorted in non-decreasing order. So if the number is 45000, then the nearest and smaller tidy number will be 44999.To solve this problem, we will traverse the number from end, when the tidy property is violated, then we reduce digit by 1, and make all subsequent digit as 9.Example Live Demo#include using namespace std; string tidyNum(string number) {    for (int i = number.length()-2; i >= 0; i--) {   ... Read More

Find the center of the circle using endpoints of diameter in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:20:38

134 Views

Suppose we have two endpoints of diameter of a circle. These are (x1, y1) and (x2, y2), we have to find the center of the circle. So if two points are (-9, 3) and (5, -7), then the center is at location (-2, -2).We know that the mid points of two points are −$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$Example Live Demo#include using namespace std; class point{    public:       float x, y;       point(float x, float y){          this->x = x;          this->y = y;       }       void display(){          cout

Find sum of a number and its maximum prime factor in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:17:22

112 Views

Suppose we have a positive number n, and we have to find the sum of N and its maximum prime factor. So when the number is 26, then maximum prime factor is 13, so sum will be 26 + 13 = 39.Approach is straight forward. Simply find the max prime factor, and calculate the sum and return.Example Live Demo#include #include using namespace std; int maxPrimeFact(int n){    int num = n;    int maxPrime = -1;    while (n % 2 == 0) {       maxPrime = 2;       n /= 2;    }    for (int i = 3; i 2)    maxPrime = n;    return maxPrime; } int getRes(int n) {    int sum = maxPrimeFact(n) + n;    return sum; } int main() {    int n = 26;    cout

Find square root of number upto given precision using binary search in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:13:45

1K+ Views

Suppose we have a positive number n, and precision p. We have to find square root of the number n up to p decimal places using binary search technique. So if the number is n = 50, and p = 3, then output is 7.071.So solve this, we have to follow some steps −Initialize start := 0 and end := nCompare the square of mid integer, if this is equal to the number then integral part has found out, otherwise look for left or right as required.Once we have completed the task for integral part, then do for the fractional ... Read More

Find smallest values of x and y such that ax – by = 0 in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:10:27

109 Views

Suppose we have two values a and b. We have to find x and y, such that ax – by = 0. So if a = 25 and b = 35, then x = 7 and y = 5.To solve this, we have to calculate the LCM of a and b. LCM of a and b will be the smallest value that can make both sides equal. The LCM can be found using GCD of numbers using this formula −LCM (a,b)=(a*b)/GCD(a,b)Example Live Demo#include #include using namespace std; void getSmallestXY(int a, int b) {    int lcm = (a * b) / __gcd(a, b);    cout

destroy() method in Tkinter - Python

Pradeep Elance
Updated on 19-Dec-2019 12:19:04

4K+ Views

The destroy() method in Tkinter destroys a widget. It is useful in controlling the behavior of various widgets which depend on each other. Also when a process is complete by some user action we need to destroy the GUI components to free the memory as well as clear the screen. The destroy() method achieves all this.In the below example we have screen with 3 buttons. Clicking the first button closes the window itself where as the clicking of the second button closes the 1st button and so on. This behavior is emulated by using the destroy method as shown in ... Read More

Find smallest subarray that contains all elements in same order in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:07:10

197 Views

Suppose we have two arrays of size m and n, The task is to find minimum length subarray in the first array, that contains all the elements if the second array. Element in second array may be present in the large array in non-contiguous but order must be same. So if two arrays are like A = [2, 2, 4, 5, 8, 9], and B = [2, 5, 9], then the output will be 5. As the smallest subarray of A, will be [2, 4, 5, 8, 9]. Here all elements like [2, 5, 9] are in the same order. ... Read More

Find the Nth term of the series where each term f[i] = f[i – 1] – f[i – 2] in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:42:16

81 Views

Suppose we have a series called f. Each term of f, follows this rule f[i] = f[i – 1] – f[i – 2], we have to find the Nth term of this sequence. f[0] = X and f[1] = Y. If X = 2 and Y = 3, and N = 3. The result will be -2.If we see this closely, there will be almost six terms before the sequence starts repeating itself. So we will find the first 6 terms of the series and then the Nth term will be the same as (N mod 6)th term.Example#include< iostream> using ... Read More

Find the last digit when factorial of A divides factorial of B in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:37:38

116 Views

If we have two integers A and B, and B >= A, we have to compute the last digit of the B! / A! When the value of A = 2 and B = 4, then result is 2, 2! = 2 and 4! = 24, so 24/2 = 12. the last digit is 2.As we know that the last digit of factorial will be in set {0, 1, 2, 4, 6}, then follow these steps to solve this problem −We will find the difference between A and Bif diff >=5, then answer is 0otherwise, iterate from (A + 1) ... Read More

Advertisements