Delete Middle of Linked List in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:06:58

3K+ Views

In this tutorial, we are going to learn how to delete the middle node in a linked list.The solution to the problem is straightforward. We will have two pointers one moves one node at a time and the other one moves two nodes at a time. By the time the second pointer reaches the final node, the first will be in the middle of the linked list.Let's see the steps to solve the problem.Write a struct Node for the linked list node.Initialize the linked list with the dummy data.Write a function to delete the linked list.Initialize two-pointers (slow and fast) ... Read More

Delete Leaf Nodes with Value K in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:05:33

112 Views

In this tutorial, we are going to learn how to delete the leaf nodes from a tree with the given value.Let's see the steps to solve the problem.Write a struct Node for a binary tree.Write a function to traverse (inorder, preorder, postorder) through the tree and print all data.Initialize the tree by creating nodes with the struct.Initialize the x value.Write a function to delete the leaf nodes with the given value. It accepts two arguments root node and k value.If the root is a null return.Replace the left node of the root with a new root after deletion.Same with the ... Read More

Delete Leaf Nodes with Value as X in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:02:19

1K+ Views

In this tutorial, we are going to learn how to delete the leaf nodes from a tree with the given value.Let's see the steps to solve the problem.Write a struct Node for a binary tree.Write a function to traverse (inorder, preorder, postorder) through the tree and print all data.Initialize the tree by creating nodes with the struct.Initialize the x value.Write a function to delete the leaf nodes with the given value. It accepts two arguments root node and x value.If the root is a null return.Replace the left node of the root with a new root after deletion.Same with the ... Read More

Delete Array Element in Given Index Range in C++ Program

Hafeezul Kareem
Updated on 27-Jan-2021 12:01:58

21K+ Views

In this tutorial, we are going to learn how to delete elements from the given range. Let's see the steps to solve the problem.Initialize the array and range to delete the elements from.Initialize a new index variable.Iterate over the array.If the current index is not in the given range, then update the element in the array with a new index variableIncrement the new index.Return the new index.ExampleLet's see the code. Live Demo#include using namespace std; int deleteElementsInRange(int arr[], int n, int l, int r) {    int i, newIndex = 0;    for (i = 0; i < n; i++) ... Read More

Delete Element from Array Using Two Traversals and One Traversal in C++

Hafeezul Kareem
Updated on 27-Jan-2021 12:01:37

412 Views

In this tutorial, we are going to learn how to delete an element with two loops and on loop. We don't need to delete the element. We will just replace the deleting element with the next elements.Two TraversalsLet's see the steps to delete an element from the array using two loops.Initialize the array and delete the element.Write a function to delete the element.Iterate over the array and search for the element.If the element found, break the loop.If the element is found, decrease the size of the array.Move all the elements to their previous index.Return the new size of the array.ExampleLet's ... Read More

Count Numbers Smaller than Current Number in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:35:15

261 Views

We are required to write a JavaScript function that takes in an array of Numbers.The function should construct a new array based on the input array.Each corresponding element of the new array should be the number of elements that are smaller in the original array than that corresponding element.For example −If the input array is −const arr = [2, 7, 3, 1, 56, 4, 7, 8];Then the output array should be −const output = [1, 4, 2, 0, 7, 3, 4, 6 ];ExampleFollowing is the code −const arr = [2, 7, 3, 1, 56, 4, 7, 8]; const smallerThanCurrent = ... Read More

Finding Group with Largest Elements Having Same Digit Sum in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:31:10

137 Views

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument.The function should first group the integers from 1 to n to subarray where a specific subarray contains all the elements contains a specific digit sum. Then the function should examine each subarray and return the length of that subarray which contains the most elements.For example −If the input number is −const num = 15;Then the output should be −const output = 2;because the groups are −[1, 10], [2, 11], [3, 12], [4, 13], [5, 14], [6, 15], [7], [8], [9]ExampleFollowing ... Read More

Grouping Words with Their Anagrams in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:29:21

1K+ Views

Anagrams:Two words or phrases which can be made by arranging the letters of each other in a different order are called anagrams of each other, like rat and tar.We are required to write a JavaScript function that takes in an array of strings that might contain some anagram strings. The function should group all the anagrams into separate subarrays and return the new array thus formed.For example −If the input array is −const arr = ['rat', 'jar', 'tar', 'raj', 'ram', 'arm', 'mar', 'art'];Then the output array should be −const output = [    ['rat', 'tar', 'art'],    ['jar', 'raj'],   ... Read More

Finding Hamming Distance in a String in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:27:24

586 Views

Hamming Distance:The hamming distance between two strings of equal length is the number of positions at which these strings vary.In other words, it is a measure of the minimum number of changes required to turn one string into another. Hamming Distance is usually measured for strings equal in length.We are required to write a JavaScript function that takes in two strings, lets say str1 and str2, of the same length. The function should calculate and return the hamming distance between those strings.ExampleFollowing is the code −const str1 = 'Hello World'; const str2 = 'Heeyy World'; const findHammingDistance = (str1 = ... Read More

Counting the Number of 1s Upto n in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:25:30

268 Views

We are required to write a JavaScript function that takes in a positive integer, say num.The task of our function is to count the total number of 1s that appears in all the positive integers upto n (including n, if it contains any 1).Then the function should finally return this count.For example −If the input number is −const num = 31;Then the output should be −const output = 14;because 1 appears in, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 31ExampleFollowing is the code −const num = 31; const countOnes = (num = 1) => { ... Read More

Advertisements