Server Side Programming Articles - Page 1993 of 2650

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

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

455 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

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 the closest leaf in a Binary Tree in C++

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

337 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

327 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

Program to print an inverse pyramid character pattern in C++

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

288 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

Find Tangent at a given point on the curve in C++

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

370 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

Find sum of sum of all sub-sequences in C++

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

256 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

Program to print all the 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 digits in factorial of a number in C++

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

550 Views

Suppose, we have a number n, our task is to find the sum of digits in then!. Consider n = 5, then n! = 120. So the result will be 3.To solve this problem, we will create a vector to store factorial digits and initialize it with 1. Then multiply 1 to n one by one to the vector. Now sum all the elements in the vector and return the sumExample Live Demo#include #include using namespace std; void vectorMultiply(vector &v, int x) {    int carry = 0, res;    int size = v.size();    for (int i = 0 ; ... Read More

Program to print all substrings of a given string in C++

Ayush Gupta
Updated on 19-Dec-2019 10:38:57

3K+ Views

In this tutorial, we will be discussing a program to print all the substring of a given string.For this we will be given with a string or an array of characters. Our task is to print all the substrings of that particular string.Example Live Demo#include using namespace std; //printing all the substrings void print_substr(char str[], int n){    for (int len = 1; len

Advertisements