Server Side Programming Articles - Page 1892 of 2646

Search a 2D Matrix II in Python

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

676 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

976 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

212 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

House Robber II in C++

Arnab Chakraborty
Updated on 04-May-2020 08:59:30

811 Views

Consider, you are a professional robber. And you are planning to rob houses along a street. Each house has a certain amount of money stored. All houses are arranged in a circle. That means the first house is the neighbor of the last house. We have to keep in mind that the adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. So if we have a list of integers representing the amount of money of each house, determine the maximum amount of money you can ... Read More

Implement Trie (Prefix Tree) in Python

Arnab Chakraborty
Updated on 04-May-2020 08:58:35

5K+ Views

Suppose we have to make the trie structure, with three basic operations like insert(), search(), startsWith() methods. We can assume that all inputs are in lowercase letters. For example, if we call the functions as follows, we will see the outputsTrie trie = new Trie()trie.insert(“apple”)trie.search(“apple”)     //This will return truetrie.search(“app”)        //This will return falsetrie.startsWith(“app”)   //This will return truetrie.insert(“app”)trie.search(“app”)        //This will return trueTo solve this, we will follow these steps −Initially make one dictionary called child.The insert method will be like −current := childfor each letter l in word −if l is not present in ... Read More

Number of Islands in Python

Arnab Chakraborty
Updated on 04-May-2020 08:57:19

2K+ Views

Suppose we have a grid, there are few 0s and few 1s. We have to count the number of islands. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is ... Read More

Advertisements