Count Set Bits Using Python List Comprehension

Pradeep Elance
Updated on 19-Dec-2019 11:34:21

206 Views

Set bits are the bits representing 1 in the binary form of a number. In this article we will see how to count the number of set bits in a given decimal number.#53 in binary is: 110101 The number of set bits is the number of ones. Here it is 4.In the below program we take the number and convert it to binary. As the binary conversion contains 0b as the first two characters, we remove it using string splitting technique. Then use a for loop to count each bit of the binary number if that value of the digit ... Read More

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

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

149 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 Common Nodes in Two Singly Linked Lists in C++

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

470 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

362 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

Binary List to Integer in Python

Pradeep Elance
Updated on 19-Dec-2019 11:14:51

2K+ Views

We can convert a list of 0s and 1s representing a binary number to a decimal number in python using various approaches. In the below examples we use the int() method as well as bitwise left shift operator.Using int()The int() method takes in two arguments and changes the base of the input as per the below syntax.int(x, base=10) Return an integer object constructed from a number or string x.In the below example we use the int() method to take each element of the list as a string and join them to form a final string which gets converted to integer ... Read More

Find Altitude and Area of Isosceles Triangle in C++

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

339 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

Find Tangent at a Given Point on the Curve in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:54:19

386 Views

Suppose we have a curve like y = x(A - x), we have to find the tangent at a given point (x, y) on that curve. Here A is an integer number, x and y are also integers.To solve this, we have the check that the given point is on the curve or not, if so, then find the differentiation of that curve, so it will be −$$\frac{\text{d}y}{\text{d}x}=A-2x$$Then put x and y into the dy/dx, then find the tangent using this equation −$$Y-y=-\lgroup\frac{\text{d}y}{\text{d}x}\rgroup*\lgroup X-x \rgroup$$Example Live Demo#include using namespace std; void getTangent(int A, int x, int y) {    int differentiation ... Read More

Print Inverse Pyramid Character Pattern in C++

Ayush Gupta
Updated on 19-Dec-2019 10:50:43

308 Views

In this tutorial, we will be discussing a program to print an inverse pyramid character pattern.For this we will be provided with the number of rows containing in the inverted pyramid triangle. Our task is to print the alphabets in the given number of rows to develop the shape of an inverse pyramid.Example Live Demo#include using namespace std; //printing the inverse pyramid pattern void inv_pyramid(int n){    int i, j, num, gap;    for (i = n; i >= 1; i--) {       for (gap = n - 1; gap >= i; gap--) {          cout

Print All Numbers Divisible by 3 and 5 in C++

Ayush Gupta
Updated on 19-Dec-2019 10:46:24

1K+ Views

In this tutorial, we will be discussing a program to print all the numbers divisible by 3 and 5 less than the given number.For this we will be given with a number say N. Our task is to print all the numbers less than N which are divisible by both 3 and 5.Example Live Demo#include using namespace std; //printing the numbers divisible by 3 and 5 void print_div(int N){    for (int num = 0; num < N; num++){       if (num % 3 == 0 && num % 5 == 0)       cout

Find Sum of Sum of All Subsequences in C++

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

269 Views

Consider we have an array A with n elements. We have to find the total sum of the sum of all the subsets of the array. So if the array is like A = [5, 6, 8], then it will be like −SubsetSum5566885, 6116, 8145, 8135, 6, 819Total Sum76As the array has n elements, then we have a 2n number of subsets (including empty). If we observe it clearly, then we can find that each element occurs 2n-1 timesExample Live Demo#include #include using namespace std; int totalSum(int arr[], int n) {    int res = 0;    for (int i = ... Read More

Advertisements