Found 7347 Articles for C++

Move all zeros to the front of the linked list in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:02:21

249 Views

Given a linked list with random integers and zeroes. We have to move all the zeroes to the front of the linked list. Let's see an example.Input3 -> 0 -> 1-> 0 -> 0 -> 1 -> 0 -> 0 -> 3 -> NULLOutput0->0->0->0->0->3->1->1->3->NULL AlgorithmInitialise the linked list.Return if the linked list is empty or it has single node.Initialise two nodes with second node and first node respectively to track current and previous nodes.Iterate over the linked list until we reach the end.If the current node is 0, then make it new head.Update the values of current and previous nodes ... Read More

Move all zeros to start and ones to end in an Array of random integers in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:49:34

667 Views

In this tutorial, we are going to write a program that moves all zeroes to front and ones to end of the array.Given an array with zeroes and ones along with random integers. We have to move all the zeroes to start and ones to the end of the array. Let's see an example.Inputarr = [4, 5, 1, 1, 0, 0, 2, 0, 3, 1, 0, 1]Output0 0 0 0 4 5 2 3 1 1 1 1AlgorithmInitialise the array.Initialise an index to 1.Iterate over the given array.If the current element is not zero, then update the value at the ... Read More

Move all zeroes to end of array in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:44:33

2K+ Views

Given array with multiple zeroes in it. We have to move all the zeroes in the array to the end. Let's see an example.Inputarr = [4, 5, 0, 3, 2, 0, 0, 0, 5, 0, 1]Output4 5 3 2 5 1 0 0 0 0 0AlgorithmInitialise the array.Initialise an index to 0.Iterate over the given array.If the current element is not zero, then update the value at the index with the current element.Increment the index.Write a loop that iterates from the above index to nUpdate all the elements to 0.ImplementationFollowing is the implementation of the above algorithm in C++#include ... Read More

Motzkin number in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:40:19

136 Views

The Motzkin number series starts with 1, 1, 4, 9, etc.., We can get the generalised nth term with the sequence. The Motzkin number sequence is as follows.a0 = 1a1 = 1a2 = 4a3 = 9an = ((2 * n + 1)/ n + 2) * M(n-1) +((3 * n - 3)/ n + 2) * M(n - 2)AlgorithmInitialise the number n.Iterate till n.Update the previous two numbersReturn the last number.ExampleImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    if(n == 0 || n == 1) {       return 1;    }    int a = 1, b = 1;    for(int i = 2; i

Most frequent element in an array in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:35:35

4K+ Views

We are given a array and we need to find the most frequent element from it. Let's see an example.Inputarr = [1, 2, 3, 3, 2, 2, 1, 1, 2, 3, 4]Output2In the above array, 2 occurs 4 times which is most frequent than any other in the array.Algorithm - 1Initialise the array.Initialise a map to store the frequency of each element.Count the frequency of each element and store it in the map.Iterate over the map and find the element with most frequency.Return the element.Algorithm - 2Initialise the array.Sort the given array.Maintain the variables for max count, result and current ... Read More

Next smallest prime palindrome in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:27:23

168 Views

We are given a number N. We need to find the prime palindrome that is greater than N. Let's see an example.InputN = 10Output11AlgorithmInitialise the number N.Write a function to check whether the given number is prime or not.Write a function to check whether the given number is palindrome.Write a loop that iterates from N + 1 till you find the next prime palindrome.Check if the number is prime and palindrome.If the number is prime and palindrome.Return the number.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; bool isPrime(int n) {    if (n < 2) ... Read More

Next Smaller Element in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:07:58

990 Views

The next smaller element is the element that is the first smaller element after it. Let's see an example.arr = [1, 2, 3, 5, 4]The next smaller element for 5 is 4 and the next smaller element for elements 1, 2, 3 is -1 as there is no smaller element after them.AlgorithmInitialise the array with random numbersInitialise a stack.Add first element to the stack.Iterate through the element of the array.If the stack is empty, add the current element to the stack.While the current element is smaller than the top element of the stack.Print the top element with the next smaller ... Read More

Next Larger element in n-ary tree in C++

Hafeezul Kareem
Updated on 25-Oct-2021 04:01:50

256 Views

The n-ary tree is the tree with n children for each node. We are given a number n and we have to find the next larger element from the n-ary tree.We can find the solution by traversing through the n-ary tree and maintaining the result.AlgorithmCreate n-ary tree.Initialise a result.Write a function to get next larger element.Return if the current node is null.Check the current node data is greater than the expected element or not.If yes, then check whether the result is empty or the result is greater than the current node data.If the above condition satisfies, then update the result.Get ... Read More

Next higher number with same number of set bits in C++

Hafeezul Kareem
Updated on 25-Oct-2021 03:55:28

395 Views

We are given a number n, we have to find the number that is greater than n with same number of set bits as n in its binary representation.The digit 1 in the binary representation is called set bit.Let's see an example.Input124Output143AlgorithmInitialise the number n.Write a function get the count of number of set bits.Initialise the iterative variable with n + 1.Write an infinite loop.Check for the number of set bits for numbers equal to the number of set bits of n.Return the number when you find it.Increment the number.ImplementationFollowing is the implementation of the above algorithm in C++#include ... Read More

Advertisements