Sort List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:29:28

274 Views

Suppose we have a list, we have to sort this in O(n logn) time using constant space complexity, so if the list is like [4, 2, 1, 3], then it will be [1, 2, 3, 4]To solve this, we will follow these steps −Define a method for merging two lists in sorted, order, that method is merge(), this takes two lists l1 and l2.The sort list method will work like below −if head is null, or next of head is null, then return headslow := head, fast := head, and prev = nullwhile fast is not null and next of ... Read More

Flatten Binary Tree to Linked List in C++

Arnab Chakraborty
Updated on 30-Apr-2020 09:21:28

237 Views

Suppose we have a binary tree; we have to flatten it into linked list in place. So if the tree is like −The output tree will be −To solve this, we will follow these steps −ser prev := nullDefine a recursive function solve(), that will take root as input.if root is null, then returnsolve(right of root)solve(left of root)right of root := prev, left of root := nullprev := rootLet us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class TreeNode{    public:    int val;    TreeNode *left, *right;    TreeNode(int data){     ... Read More

Binary Tree Zigzag Level Order Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 09:13:33

763 Views

Suppose we have a binary tree; we have to find the Zigzag level order traversal. So for the first row, scan from left to right, then right to left from the second row, then again left to right and so on. So if the tree is like −The traversal sequence will be [[3], [20, 9], [15, 7]]To solve this, we will follow these steps −if the tree is empty, return empty listqueue := make a queue and insert root into the queue, create two empty lists res and res2, and set flag as Truewhile queue is not emptymake a list ... Read More

Task Scheduler in C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:47:54

4K+ Views

Suppose we have a char array representing tasks CPU need to do. This contains uppercase letters A to Z where different letters represent different tasks. The tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one job or just be idle. However, there is a non-negative cooling interval called n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. We have to find the least number of intervals the CPU will take to finish ... Read More

Differences Between jdeps andjdeprscan Tools in Java 9

raja
Updated on 30-Apr-2020 08:23:25

634 Views

Jdeps tool can be used to analyze the dependencies of our classes. The running of the "jdeps -jdkinternals jararchive.jar" command prints a list of all classes that use Java internal API. Jdeps tool returns a detailed description of the dependencies while Jdeprscan is another useful tool particularly used in combination with the "-for-removal" flag. This tool shows us all uses of deprecated API by a given jar archive, and only deprecated uses of jdk methods can be shown and can't use this tool to check for deprecation in third party jar.Jdeps tool:"jdeps" is a class dependency analyzer tool can be used for package level ... Read More

Arithmetic Slices in C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:07:41

266 Views

Suppose we have a sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. So for example, these are arithmetic sequence: [1, 3, 5, 7, 9], [7, 7, 7, 7], [3, -1, -5, -9], But the following sequence is not arithmetic. [1, 1, 2, 5, 7]Now a zero-indexed array A consisting of N numbers is given. A slice of that given array is any pair of integers (P, Q) such that 0

Smallest Integer Divisible by K in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:52:31

355 Views

Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.To solve this, we will follow these steps −if k is even, or k is divisible by 5, then return -1set r := 0 and N = 1for i in range 1 to K + ... Read More

Clumsy Factorial in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:49:18

2K+ Views

As we know that the factorial of a positive integer n is the product of all positive integers less than or equal to n. So factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We will try to find a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.The clumsy factorial is like clumsy(10) = 10 * 9 / 8 + 7 - 6 * ... Read More

Max Consecutive Ones III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:45:44

587 Views

Suppose we have an array A of 0s and 1s, we can update up to K values from 0 to 1. We have to find the length of the longest (contiguous) subarray that contains only 1s. So if A = [1,1,1,0,0,0,1,1,1,1,0] and k = 2, then the output will be 6, So if we flip 2 0s, the array can be of like [1,1,1,0,0,1,1,1,1,1,1], the length of longest sequence of 1s is 6.To solve this, we will follow these steps −set ans := 0, j := 0 and n := size of arrayfor i in range 0 to n – 1if A[i] is 0, then decrease k by 1while j

Subarray Sums Divisible by K in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:42:38

524 Views

Suppose we have an array A of integers. We have to find the number of contiguous non-empty subarray, that have a sum divisible by k. If A = [4, 5, 0, -2, -3, 1] and k = 5, then the output will be 7. There are seven subarrays. [[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]]To solve this, we will follow these steps −make one map m and set m[0] as 1temp := 0, ans := 0, and n := size of array afor i in range 0 to ... Read More

Advertisements