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

Two Sum in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:04:05

19K+ Views

Suppose we have an array of integers. We have to return the indices of two integers, such that if we add them up, we will reach to a specific target that is also given. Here we will take one assumption, that is always there will be one unique solution in the array, so no two set of indices for same target will be there.For an example, suppose the array is like A = [2, 8, 12, 15], and the target sum is 20. Then it will return indices 1 and 2, as A[1] + A[2] = 20.To solve this, we ... Read More

Triangle in C++

Arnab Chakraborty
Updated on 28-Apr-2020 07:14:50

360 Views

Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step, we can move to adjacent numbers on the row below.For example, if the following triangle is like[       [2],      [3, 4],     [6, 5, 7],    [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the steps −Create one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for ... Read More

Valid Parenthesis String in C++

Arnab Chakraborty
Updated on 28-Apr-2020 07:02:40

1K+ Views

Suppose we have an expression. The expression has some parentheses; we have to check the parentheses are balanced or not. The order of the parentheses are (), {} and []. Suppose there are two strings. “()[(){()}]” this is valid, but “{[}]” is invalid.To solve this, we will follow these steps −Traverse through the expression until it has exhaustedif the current character is opening bracket like (, { or [, then push into stackif the current character is closing bracket like ), } or ], then pop from stack, andcheck whether the popped bracket is corresponding starting bracket of thecurrent character, ... Read More

Counting Bits in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:59:03

2K+ Views

Suppose we have a non-negative integer number num. For each number i in the range 0 ≤ i ≤ num we have to calculate the number of 1's in their binary counterpart and return them as a list. So if the number is 5, then the numbers are [0, 1, 2, 3, 4, 5], and number of 1s in these numbers are [0, 1, 1, 2, 1, 2]To solve this, we will follow these steps −res := an array which holds num + 1 number of 0soffset := 0for i in range 1 to num + 1if i and i ... Read More

Largest Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:55:35

582 Views

Suppose there is a list of non-negative integers, we have to arrange them such that they form the largest number. So if the array is [10, 2], then the largest number will be 210.To solve this, we will follow these steps −Arrange the numbers where the most significant digits are greater than place them at first, like this arrangement the numbers. After that just join the numbers from the array.ExampleLet us see the following implementation to get a better understanding − Live Demofrom functools import cmp_to_key class Solution(object):    def largestNumber(self, nums):       for i in range(len(nums)):     ... Read More

Evaluate Reverse Polish Notation in C++ Program

Arnab Chakraborty
Updated on 28-Apr-2020 06:44:43

5K+ Views

Suppose we have Reverse polish notation and we have to evaluate the value. The reverse polish notation is also known as postfix expression. Here we have to use the stack data structure to solve the postfix expressions.From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then the operation is performed in the correct sequence. After that, the result is also pushed in the stack for future use. After completing the whole expression, the final result is also stored in the stack top. So ... Read More

Word Break Problem in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:39:14

969 Views

Suppose we have one non-empty string s and a dictionary wordDict. That is containing a list of non-empty words, determine when s can be segmented into a space-separated sequence of one or more dictionary words. We have to follow some rules −The same word in the dictionary may be reused multiple numbers of times in the segmentation.We can assume that the dictionary does not contain duplicate words.Suppose the string s = “applepenapple”, and word dictionary is like [“apple”, “pen”], then the output will be true because the string s can be segmented as “apple pen apple”.Let us see the steps ... Read More

Gas Station Problem in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:33:33

613 Views

Suppose there is a circle, and there are n gas stations on the circle. We have two sets of data like −The amount of gas that every gas stations hasDistance from one gas stations to another.Calculate the first point, from where a car will be able to complete the circle. Assume for 1 unit of gas, the car can go 1 unit of distance. Suppose there are four gas stations, and the amount of gas, and distance from the next gas stations is as like [(4, 6), (6, 5), (7, 3), (4, 5)], the first point from where car can ... Read More

Palindrome Partitioning in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:28:15

437 Views

Suppose we have one input string, a partitioning of that string is palindrome partitioning, when every substring of the partition is a palindrome. In this section, we have to find the minimum cuts are needed to palindrome partitioning the given string. So if the string is like “ababbbabbababa” Minimum cut to partition as palindrome. Here 3 cuts are needed. The palindromes are: a | babbbab | b | ababaTo solve this, we will follow these steps −n := length of strdefine cut matrix and pal matrix each of order n x nfor i := 0 to n, dopal[i, i] := ... Read More

Advertisements