Server Side Programming Articles - Page 1893 of 2650

Longest Increasing Subsequence in Python

Arnab Chakraborty
Updated on 04-May-2020 09:10:18

3K+ Views

Suppose we have an unsorted list of integers. We have to find the longest increasing subsequence. So if the input is [10, 9, 2, 5, 3, 7, 101, 18], then the output will be 4, as the increasing subsequence is [2, 3, 7, 101]To solve this, we will follow these steps −trail := an array of length 0 to length of nums – 1, and fill this with 0size := 0for x in numsi := 0, j := sizewhile i is not jmid := i + (j - i) / 2if trails[mid] < x, then i := mid + 1, ... Read More

Find the Duplicate Number in Python

Arnab Chakraborty
Updated on 04-May-2020 09:09:06

3K+ Views

Suppose we have an array nums containing n + 1 integers. The members are in range 1 to n. prove that at least one duplicate number must be there. Assume that there is only one duplicate number, we have to find that duplicate element. So if the array is like [1, 3, 4, 2, 2], then the duplicate element will be 2.To solve this, we will follow these steps −a := nums[0] and b := nums[0]while Truea := nums[nums[a]]b := nums[b]if a = b, then breakptr := nums[0]while ptr is not bptr := nums[ptr]b := nums[b]return ptrLet us see the ... Read More

Perfect Squares in C++

Arnab Chakraborty
Updated on 04-May-2020 09:08:12

538 Views

Suppose we have a positive integer n, find the least number of perfect square numbers whose sum is n. So if the number is 13, then the output is 2, as the numbers are 13 = 9 + 4To solve this, we will follow these steps −create one table for dynamic programming, of length n + 1, and fill it with infinitydp[0] := 0for i := 1, when i*i dp(n+1,INF);       dp[0] = 0;       for(int i =1;i*i

Search a 2D Matrix II in Python

Arnab Chakraborty
Updated on 04-May-2020 09:07:25

652 Views

   Suppose we have one m x n matrix. We have to write an efficient algorithm that searches for a value in that matrix. This matrix has the following properties −Integers in each row are sorted in ascending from left to right.Integers in each column are sorted in ascending from top to bottom.So if the matrix is like −14711152581219369162210131417241821232630If target is 5, then return true, if target is 20, then return falseTo solve this, we will follow these steps −len := number of columns, c1 := 0, c2 := len – 1while trueif matrix[c1, c2] = target, then return trueelse ... Read More

Product of Array Except Self in Python

Arnab Chakraborty
Updated on 04-May-2020 09:06:39

1K+ Views

Suppose we have an array called nums of n integers where n > 1. We have to find an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. So if the input array is [1, 2, 3, 4], then the output will be [24, 12, 8, 6]. We have to solve this without using division operator.To solve this, we will follow these steps −right_mul := an array of size same as nums, fill it with 0last element of right_mul = last element of numsfor i in range 1 to length of ... Read More

Lowest Common Ancestor of a Binary Tree in Python

Arnab Chakraborty
Updated on 04-May-2020 09:05:27

1K+ Views

Suppose we have a binary tree. we have to find the Lowest common ancestor nodes of two given nodes. The LCA of two nodes p and q is actually as the lowest node in tree that has both p and q as decedent. So if the binary tree is like [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]. The tree will be like −Here LCA of 5 and 1 is 3To solve this, we will follow these steps −If the tree is empty, then return nullif p and q both are same as root, then return rootleft ... Read More

Kth Smallest Element in a BST in Python

Arnab Chakraborty
Updated on 04-May-2020 09:03:32

1K+ Views

Suppose we have a binary search tree. We have to find the Kth smallest element in that BST. So if the tree is like −So if we want to find 3rd smallest element, then k = 3, and result will be 7.To solve this, we will follow these steps −create one empty list called nodescall solve(root, nodes)return k – 1th element of nodesthe solve method is created, this takes root and nodes array, this will work as follows −if root is null, then returnsolve(left of root, nodes)add value of root into the nodes arraysolve(right of root, nodes)Let us see the ... Read More

Count Complete Tree Nodes in C++

Arnab Chakraborty
Updated on 04-May-2020 09:02:45

929 Views

Suppose we have a complete binary tree, we have to count the number of nodes. So if the tree is like −So the output will be 6.To solve this, we will follow these stepsThis will use the recursive approach. This method, countNodes() is taking the root as argument.hr := 0 and hl := 0create two nodes l and r as rootwhile l is not emptyincrease hl by 1l := left of lwhile r is not emptyr := right of rincrease hr by 1if hl = hr, then return (2 ^ hl) – 1return 1 + countNodes(left of root) + countNodes(right ... Read More

Combination Sum IIII in C++

Arnab Chakraborty
Updated on 04-May-2020 09:01:23

186 Views

Consider we have to generate all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used. Each combination should be a unique set of numbers. All numbers should be positive, and the solution must not contain duplicate combinations. So if k = 3 and n = 9, then the possible combinations are [[1, 2, 6], [1, 3, 5], [2, 3, 4]]To solve this, we will follow these steps −Suppose we will solve this using forming a method called solve. This will be recursive method, this will take ... Read More

Kth Largest Element in an Array in Python

Arnab Chakraborty
Updated on 04-May-2020 09:00:26

2K+ Views

Suppose we have an unsorted array, we have to find the kth largest element from that array. So if the array is [3, 2, 1, 5, 6, 4] and k = 2, then the result will be 5.To solve this, we will follow these steps −We will sort the element, if the k is 1, then return last element, otherwise return array[n – k], where n is the size of the array.Let us see the following implementation to get better understanding −Example Live Democlass Solution(object):    def findKthLargest(self, nums, k):       nums.sort()       if k ==1:   ... Read More

Advertisements