Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level). Example: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Input: root = [1] Output: [[1]] Algorithm: Since we have to traverse the tree level wise and need to process node from left to right, we can… Continue reading
Category: C++
Print Odd Even in Separate threads in C++| TechTrendings
This is a popular question asked for experience programmer while testing multithreading knowledge. We can do it in two ways. Approach 1: Using lock mechanism Approach 2: Using Condition variable
Program to move all zeroes to the end
Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums = [0,1,0,3,12]Output: [1,3,12,0,0]Example 2: Input: nums = [0]Output: [0]
Program to reverse word in a string
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = “Let’s take LeetCode contest”Output: “s’teL ekat edoCteeL tsetnoc”Example 2: Input: s = “God Ding”Output: “doG gniD” Solution: string reverseWords(string s) {int n = s.length();if(n==0){return s;} };
Find the length of longest substring without repeating characters|techtrendings
Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2: Input: s = “bbbbb”Output: 1Explanation: The answer is “b”, with the length of 1.Example 3: Input: s = “pwwkew”Output: 3Explanation: The answer is “wke”,… Continue reading
Program to find unpaired element in an Array in C++|Leetcode |techtrendings
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] =… Continue reading
Program to Rotate an Array in C++|LeetCode |techtrendings
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]… Continue reading
Custom Deleters in C++
In this tutorial, we will learn how to create a custom deleted to delete a smart pointer in C++. Introduction to Custom Deleter in C++ When a smart pointer of any user-defined type is destroyed, the destructor of the class is invoked giving us the ability to explicitly inspect and modify the delete process. But… Continue reading
Copy Constructor vs Assignment Operator in C++
The Assignment Operator is when you replace the data with an already existing(previously initialized) object withsome other object’s data. Lets see an example: You can see here I call the assignment operator when I already initialized the Assign object. Then later I assign foo2 tofoo . All the changes to appear when you call that… Continue reading
marsexploration in C++ Hackerank
A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help. Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, , determine how many letters of the SOS message have been changed by radiation.… Continue reading