Found 7347 Articles for C++

Largest Merge of Two Strings in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:26:42

180 Views

Let us suppose we have two strings 'a' and 'b' and a string 'merge'. The task is to fill the string 'merge' with the characters from 'a' and 'b' in such a way that, If the string 'a' is non-empty, then remove the first character from the string 'a' and copy it into string 'merge'.If the string 'b' is non-empty, then remove the first character from the string 'b' and copy it into string 'merge'.If the strings 'a' and 'b' are non-empty, then remove the first characters from string 'a' and copy it into string 'merge' and then remove the ... Read More

Intersection of Two Linked Lists in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:28:35

1K+ Views

A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to the other nodes in the list. The task is to find the node at which two linked lists intersect each other. If they don't intersect, then return NULL or empty as output.For ExampleInput-1:             Output:2Explanation: Since the given linked ... Read More

Find the sum of left leaf nodes of a given Binary Tree in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:29:51

921 Views

Let us suppose we have a Binary Tree having a root node and its left child and right child. The task is to find the total sum of leaf nodes of the tree which are left to its parent node.For ExampleInput-1:      Output:15Explanation: In the given input Binary Tree, the sum of all the left leaf nodes is 9+4+2 = 15. So, the output is 15.Approach to Solve this ProblemWe have a Binary Tree and the task is to find the sum of all the leaf nodes which are left to its parent.The recursive approach to solve this problem is to ... Read More

C++ Program to find the Shortest Distance to a character

Dev Prakash Sharma
Updated on 23-Feb-2021 19:32:58

434 Views

Given a string 'a' and a character 'char', the task is to print the distance of 'char' from each character of the given string. The size of the distance array is same as the size of the string, since we have to find the distance of the character from each character of the given string.For ExampleInput-1:a = “tutorialspoint”char = “o”Output: [ 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3]Explanation: In the given string, the distance of the character from each character of the given string is [3, 2, 1, 0, 1, 2, 3, 4, 5, 6, ... Read More

Find the Number of 1 Bits in a large Binary Number in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:34:20

2K+ Views

Given a 32-bit Unsigned Binary Number, the task is to count the set bits, i.e., '1's present in it.For ExampleInput:N = 00000000000000100111Output:4Explanation: Total set bits present in the given unsigned number is 4, thus we will return the output as '4'.Approach to Solve this ProblemWe have given an unsigned 32-bit binary number. The task is to count how many '1's present in it.To count the number of '1's present in the given binary number, we can use the inbuilt STL function '__builin_popcount(n)' which takes a binary number as the input parameter.Take a binary number N as the input.A function count1Bit(uint32_t n) ... Read More

Daily Temperatures in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 15:26:08

325 Views

Let us suppose we have an array of positive temperatures which represent the temperatures T. The task is to calculate how many days are there for the next warmer temperature in the given list.For exampleInput-1: T = [ 73, 74, 75, 71, 69, 72, 76, 73]Output: [1, 1, 4, 2, 1 ,1 ,0 ,0]Explanation: In the given list of temperatures [73, 74, 75, 71, 69, 72, 76, 73],  the next greater temperature is at Day 1. Similarly, Day 6 is the warmest in all the temperatures, thus the output will be [ 1, 1, 4, 2, 1, 1, 0, 0].Approach to Solve this ... Read More

Copy list with random Pointer in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:35:41

583 Views

A Linked List is a linear data structure in which each node is having two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to other nodes in the list. The task is to construct the list with the same as the original list. Copying the list from the original list which is having some random pointer is called a 'Deep Copy' of the linked list.For ... Read More

Circle Sort in C++

Dev Prakash Sharma
Updated on 23-Feb-2021 19:38:17

268 Views

Circle Sort is an interesting sorting algorithm to sort a given array of elements. The algorithm compares the elements of the array diametrically and once the elements in one part is sorted, then continuously sort the other end of the array diametrically.ExampleLet us visualize the circle sort for an array. Let us suppose we have an array with 6 elements.Input:N = 6arr [ ] = { 2, 1, 5, 8, 7, 9 }When we draw concentric circles for each array element, then it will appear as followsOutput:1 2 5 7 8 9Explanation: After sorting the elements in the array using Circle ... Read More

C++ Program to find the smallest digit in a given number

Dev Prakash Sharma
Updated on 23-Feb-2021 19:39:23

3K+ Views

Given a non-negative number, the task is to find its smallest digit.For exampleInput:N = 154870Output:0Explanation: In the given number '154870', the smallest digit is '0'.Approach to Solve this ProblemThe simplest approach to solve this problem is to extract the last digit in the given number using the remainder theorem. While traversing the number, we will check if the extracted digit is less than the last digit, then return the output.Take a number n as the input.An integer function smallest_digit(int n) takes 'n' as the input and returns the smallest digit in the given number.Now initialize min as the last digit of the ... Read More

C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not

Dev Prakash Sharma
Updated on 23-Feb-2021 19:41:10

1K+ Views

Given a Binary Tree, the task is to check whether it is a Full Binary Tree or not. A Binary Tree is said to be a Full Binary Tree if every node has zero or two children.For ExampleInput-1Output:1Explanation: Every node except the leaf node has two children, so it is a full binary tree.Input-2: Output:0Explanation: Node 2 has only one child, so it is not a full binary tree.Approach to Solve this ProblemTo check whether a given binary tree is full or not, we can check recursively for the left subtree and right subtree.Input a given Binary Tree having nodes and its children.A ... Read More

Advertisements