Found 7197 Articles for C++

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

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

158 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

5K+ 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

335 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

122 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

178 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

Find the K-th minimum element from an array concatenated M times in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:35:33

141 Views

Consider we have an array A, and another two integers K and M. We have to find Kth minimum element after concatenating the array to itself M number of times. Suppose the array is like A = [3, 1, 2], K = 4 and M = 3, so after concatenating A, 3 times, it will be [3, 1, 2, 3, 1, 2, 3, 1, 2], the 4th smallest element is 2 here.To solve this problem, we will sort the array A, then return the value, present at index ((K – 1)/M), of the array.Example Live Demo#include #include using namespace std; int ... Read More

Find the Diameter or Longest chord of a Circle in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:25:00

130 Views

Suppose we have radius r is given. We have to find the diameter or longest chord of the circle. If the radius is 9, and diameter will be 18. This task is extremely simple, we have to find the 2*r, that is the diameter of the circle.Example Live Demo#include using namespace std; int getDiameter(int r) {    return 2*r; } int main() {    int r = 9;    cout

Find the common nodes in two singly linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:23:36

441 Views

Suppose we have two singly-linked lists. We have to find the total number of common nodes in both the singly linked list. So if two lists are like [15, 16, 10, 9, 7, 17], and [15, 16, 40, 6, 9], there are three common nodes.Traverse both lists up to end of the list using two nested loops, for every node in the list, check if it is matched with any node of the second list or not. If a match is found, then increase the counter, and finally return the count.Example Live Demo#include using namespace std; class Node {    public: ... Read More

Find the closest leaf in a Binary Tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:20:59

328 Views

Suppose, one binary tree is given. It has leaf nodes at different levels. Another pointer is given, that is pointing to a node. We have to find the distance to the nearest leaf node from the pointed node. Consider the tree is like below −Here leaf nodes are 2, -2 and 6. If the pointer is pointing to node -5, The nearest nodes from -5 will be at distance 1.To solve this, we will traverse the subtree rooted with the given node, and find the closest leaf in the subtree, then store the distance. Now traversing tree starting from the ... Read More

Find the altitude and area of an isosceles triangle in C++

Arnab Chakraborty
Updated on 19-Dec-2019 11:06:00

321 Views

Consider we have the side of the isosceles triangle, our task is to find the area of it and the altitude. In this type of triangle, two sides are equal. Suppose the sides of the triangle are 2, 2 and 3, then altitude is 1.32 and the area is 1.98.Altitude(h)=$$\sqrt{a^{2}-\frac{b^{2}}{2}}$$Area(A)=$\frac{1}{2}*b*h$Example Live Demo#include #include using namespace std; float getAltitude(float a, float b) {    return sqrt(pow(a, 2) - (pow(b, 2) / 4)); } float getArea(float b, float h) {    return (1 * b * h) / 2; } int main() {    float a = 2, b = 3;    cout

Advertisements