Close

Comparing different types in python

If someone already have experience with languages like C/C++, Java, C# comparison between two variables of different types results in a compile error. But in Python this will simply evaluate to false. So beware when comparing a string and int containg same values. when we print, they look the same. I actually faced this issue…

Edit distance 1

Given two strings, how do we check if their edit distance is 1? Edit distance is defined as the number of characters that needs to be modified, inserted or deleted from one string to transform into the other string. For example let us consider two strings “java”, “lava” they both differ by just one character.…

Maximum elements of array

Given an array A of size N, We want to perform several iterations as described below. Select any index i such that 1 <= i <= N, Mark A[i] = 0, move left by filling each element one greater than it’s right Similarly move right, and fill each element which is one greater than it’s…

Billing algorithm

This problem is from Codechef. I am re-wording it for more clarity. A garments shop has “Buy 2, Get 2” offer for it’s customers. Whatever the items may be, they charge for costliest items from an order and give the cheapest ones free. However we can divide the items into multiple orders to save more.…

Partial sum queries

Given an array of numbers, we need to answer many queries of the form Sum(A[i]…A[j]), For example, let the array be [15, 6, 1, 12, 7, 4, 10] Sum(A[1:3]) = 15+6+1 = 22 Sum(A[4:6]) = 12+7+4 = 23 … The simplest solution is to iterate through the elements from i to j and return the…