Tag: java
-
Find first and last position of element in sorted array
Problem: Given a sorted integer array find the first and last position of given target element For example, If given array is [ 1,3,6,6,7,9] and target is 6 Then output is [2,3] which represents the first and last index position of the element 6 For the same input if the target is 8 Then the…
-
Find the next highest permutation of an array
Problem: Given an integer array , find the next highest permutation of the numbers. If the next highest permutation is not found return the smallest permutation of the given input. Permutation of a number is all possible combinations of the given numbers. For example, Given the array: [1,2,3] The possible permutations are [1,3,2] , [2,1,3],…
-
Find if a linked list is a palindrome or not
Problem: Given the head of a linked list , find if it is a palindrome or not. Find the solution in O(n) time and in place (O(1) space complexity) For example , the below linked list : 1 -> 2 -> 2 -> 1 is a palindrome The below is not: 1 -> 5 ->…
-
Convert Excel Sheet Column Title to Number
Problem: Given an excel sheet column title , convert it to the column value. For example, Column value of sheet number ‘A’ is 1 ‘Z’ is 26 ‘AA’ is 27 ‘BA’ is 53 and so on. Constraints: Try out the solution here; https://leetcode.com/problems/excel-sheet-column-number/ Solution: If it is a single letter we can return the value…
-
First Unique Character in a String
Given a string find the index of the first unique character (non repeating). For example, For the string “see” the output is 0 since the letter s is not repeated in the string For the string “bbaaddavt” the output is 7 because the letter v is not repeated and is present in index 7. Assumption:…
-
Kth largest element in an array
Given an array of integers, find the kth largest element in the array. For example, Given the input [3,2,1,5,6,4] and k = 2 The output is 5 Try out the solution here: https://leetcode.com/problems/kth-largest-element-in-an-array/ Solution: We can solve this problem in multiple ways. Let’s look at three approaches. Approach 1 – Sort the array In this…
-
Sort three colors in an array
Given an array containing three different colors red ,white and blue denoted by numbers 0,1 and 2 sort the array such that red comes first followed by white followed by blue. Constraints: For example, Given the input: [2,0,1,0,2,1] The output is [0,0,1,1,2,2] Try out the solution here: https://leetcode.com/problems/sort-colors/ Solution: Approach 1 – Count the number…
-
Find the element which appears more than half the array size
Given an array, find the majority element which appears more than half the size of the array. Constraints: For example, in the below array: [7,6,8,7,7,7] The output is 7 since it appears 4 times which is more than half the size of the array (3) Solution: Finding a solution with O(n) extra space is easier.…
-
Single non duplicate number in an array
Given an array with all elements repeated twice except one element find out that element. Constraints: The time complexity should be O(n) Space complexity should be O(1) For example: Given the array : [1,3,4,3,4] the output is 1 since it occurs only once. Try out the solution here: https://leetcode.com/problems/single-number Solution: If we can use extra…
-
Is a Tree Symmetric?
Given a binary tree find out if it is symmetric or not. A tree is symmetric if it is left sub tree is a reflection of its right sub tree. Note that symmetry doesn’t mean left sub tree is equal to right sub tree. The following tree is a symmetric tree: As you see the…