Found 26504 Articles for Server Side Programming

Largest Unique Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:46:16

566 Views

Suppose we have a list of numbers, we have to return the number whose occurrence is 1, if no such element is present, then return -1. So if the list is like [5, 2, 3, 6, 5, 2, 9, 6, 3], then the output will be 9.To solve this, we will follow these steps −We will check each element, and put the elements inside the map, so if the element is not in map, then put a new entry, otherwise increase the valuethen go through the map, when the value is 1, return the key.Example(Python)Let us see the following implementation ... Read More

Remove Vowels from a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:41:54

2K+ Views

Suppose we have a string, we have to remove all vowels from that string. So if the string is like “iloveprogramming”, then after removing vowels, the result will be − "lvprgrmmng"To solve this, we will follow these steps −create one array vowel, that is holding [a, e, i, o, u]for v in a vowelreplace v using blank stringExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def removeVowels(self, s):       s = s.replace("a", "")       s = s.replace("e", "")       s = s.replace("i", "")       s ... Read More

Number of days in a month in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:35:20

7K+ Views

Suppose we have one year Y and a month M, we have to return the number of days of that month for the given year. So if the Y = 1992 and M = 7, then the result will be 31, if the year is 2020, and M = 2, then the result is 29.To solve this, we will follow these steps −if m = 2, thenif y is a leap year, return 29, otherwise 28make an array with elements [1, 3, 5, 7, 8, 10, 12]if m is in the list, then return 31, otherwise, return 30.Example(Python)Let us see ... Read More

Two Sum Less Than K in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:31:32

856 Views

Suppose we have an array A of integers and another integer K is given. We have to find the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If there is no i, j exists satisfying this equation, then return -1. So for example if A = [34, 23, 1, 24, 75, 33, 54, 8] and K = 60, then the output will be 58, as we can use 34 and 24 to sum 58, which is less than 60.To solve this, we will follow these steps −res = - ... Read More

Index Pairs of a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:27:27

931 Views

Suppose we have a text string and words (a list of strings), we have to find all index pairs [i, j] such that the substring text[i]...text[j] is in the list of words. So if the string is like “ababa” and words array is like [“aba”, “ab”], then the output will be [[0, 1], [0, 2], [2, 3], [2, 4]]. One thing we can notice, that the matches can overlap, the “aba” is matched in [0, 2] and [2, 4].To solve this, we will follow these steps −res := an empty listfor i in range 0 to a length of stringfor ... Read More

Combination Sum IV in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:53:24

277 Views

Suppose we have an integer array with all positive numbers and all elements are unique, find the number of possible combinations, so that if we add up, we will get positive integer target.So if the array is [1, 2, 3] and the target is 4, then the possible combinations will be [[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [1, 3], [3, 1], [2, 2]], so output will be 7.To solve this, we will follow these steps −Suppose we have one recursive function called solve(), this is taking array, target and another array for dynamic ... Read More

Coin Change in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:50:21

5K+ Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 11, the output is 3. This is formed using 5 + 5 + 1 = 11.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Fixed Point in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:24:35

1K+ Views

Suppose we have an array A of unique integers sorted in ascending order, we have to return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. So if the array is like [-10, -5, 0, 3, 7], then the output will be 3, as A[3] = 3 the output will be 3.To solve this, we will follow these steps −For i in range 0 to length of Aif i = A[i], then return ireturn -1Example(Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):    def fixedPoint(self, A):   ... Read More

Sum Root to Leaf Numbers in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:45:08

449 Views

Suppose we have a binary tree containing digits from 0-9 only, here all root-to-leaf path could represent a number.So if the tree is like −This is representing two paths 21 and 23, so the output will be 21 + 23 = 44.To solve this, we will follow these steps −Create one recursive function called dfs(), this will take root, and num. initially num = 0if the node is not nullnum := num * 10 + value of nodeif node right is not null and node left is not null, then’sum := sum + numnum := num / 10return from the ... Read More

Validate Binary Search Tree in Python

Farhan Muhamed
Updated on 22-Aug-2025 15:53:32

1K+ Views

A binary search tree is a special binary tree in which for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. In this article, we will discuss how to validate whether a given binary tree is a valid binary search tree (BST) in Python. Validate Binary Search Tree Algorithm to Validate Binary Search Tree Python Program to Validate Binary Search Tree Validate Binary Search Tree Given a root node of ... Read More

Advertisements