All posts tagged algorithms
Problem Statement Write a method to compute all permutations of a string Solution Let’s assume a given string represented by the letters To permute set , we can select the first character, , permute the remainder of the string to get a new list Then, with that new list, we . . . Read more
Problem statement You are given two 32bit numbers, and , and two bit positions, and . Write a method to set all bits between and in equal to (e g , becomes a substring of located at and starting at ) Example Input: , , , Output: Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

public class SetBits { public static int updateBits(int n, int m, int i, int j) { int max = ~0; /* All 1’s */ // 1’s through position j, then 0’s int left = max  ((1 << j)  1); System.out.println(Integer.toBinaryString(left)); // 1’s after position i int right = ((1 << i)  1); System.out.println(Integer.toBinaryString(right)); // 1’s, with 0s between i and j int mask = left  right; // Clear i through j, then put m in there return (n & mask)  (m << i); } public static void main(String[] args) { updateBits(1024, 21, 2, 6); } } 
Problem Statement Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. Solution We will try to create a binary tree such that for each node, the number of nodes in the left subtree and the right subtree are equal, if possible Algorithm: . . . Read more
Problem statement Given an array of integers, find the second smallest integer in that array. Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

public class SecondSmallestInteger { public static int solve(int[] arr) { int smallest = Integer.MAX_VALUE; int secondSmallest = Integer.MAX_VALUE; ; for (int i = 0; i < arr.length; i++) { if (arr[i] < smallest) { secondSmallest = smallest; smallest = arr[i]; } else if (arr[i] == smallest) { secondSmallest = smallest; } else if (arr[i] < secondSmallest) { secondSmallest = arr[i]; } } return secondSmallest; } public static void main(String[] args) { int[] arr = { 1, 2, 7, 10, 100, 999, 2, 0, 34, 42 }; int ans = solve(arr); Arrays.sort(arr); if (ans == arr[1]) { System.out.println(ans); } else { System.out.println("Problem"); } } } 
Problem statement There are stairs, a person standing at the bottom wants to reach the top. The person can climb either stair or stairs at a time. Count the number of ways, the person can reach the top. Solution We can solve this problem recursively. If the person were to . . . Read more
Problem statement Given a linked list, determine if it has a cycle in it. Solution For the solution we are going to use a technique known as the twopointer technique. That is we will involve two pointers: one slowrunner and the other fastrunner. The slowrunner will move forward 1 list . . . Read more
Problem statement Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution Obviously the naive approach to this problem would be to subtract the divisor from the dividend until the dividend becomes less than the divisor, while keeping track of how many times . . . Read more
Problem statement Given a list, nums, of distinct numbers, return all possible unique permutations. Sample input
Sample output

[[1, 2, 1], [2, 1, 1], [1, 1, 2]] 
Solution In another post we showed how this problem can be solved iteratively. We can use the same technique but add a check to skip adding the number n to . . . Read more
Problem statement Given a list, nums, of distinct numbers, return all possible permutations. Sample input
Sample output

[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 
Solution In another post we showed how this problem can be solved using a recursive technique by swapping. We stumbled upon a nice iterative solution that we thought might be worth . . . Read more
Problem statement Given a list, nums, of distinct numbers, return all possible permutations. Sample input
Sample output

[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 
Solution We are using a technique called permutations by swapping. Full code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

class Solution(object): def swap(self, nums, i, j): tmp = nums[i] nums[i] = nums[j] nums[j] = tmp def solve(self, nums, i, result): if i == len(nums)1: result.append(list(nums)) return for j in range(i, len(nums)): self.swap(nums, i, j) self.solve(nums, i+1, result) self.swap(nums, i, j) def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ ans = [] self.solve(nums, 0, ans) return ans def main(): solution = Solution() print(solution.permute([1,2,3])) if __name__ == "__main__": main() 