Given an array a of n integers find all possible Pythagorean triplets from the array.
A Pythagorean triple consists of three positive integers a, b, and c, such that a2 + b2 = c2. this formula is derived from Pythagoras theorem: _“For every right angle triangle with side lengths a, b, and c=> a_2 + b2 = c2“.
Solution:
Conceptually it is similar to Find Non duplicate pairs that sum to S and 3 SUM problem
[Read More]
Maximum value in a Sliding Window
Problem Statement
Question: A long array A[] is given to you. There is a sliding window of size w, which is moving from the very left of the array to the very right. You can only see the w numbers in the window. Each time the sliding window moves rightwards by one position.
Example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.
Window position Max \--------------- ----- -3 5 3 6 7 3 1 5 3 6 7 3 1 3 3 6 7 5 1 3 -1 6 7 5 1 3 -1 -3 7 6 1 3 -1 -3 5 7 Input: A long array A[], and a window width w
[Read More]
Given a dictionary of word - Group all words into set of anagrams of each other
Given a set of words in a dictionary, write a program to group all words which are anagrams of each other in to sets.
input: “bat”, “tar”, “xyz” , “tab”, “tar” output:[[“bat”, tab”], [“tar”, rat”],”xyz” ]
Anagrams
Two words are anagrams if one of them has exactly same characters as that of the another word.
Example : Anagram & Nagaram are anagrams (case-insensitive).
Approach
The simpler logic would be to :
[Read More]
Rearranging linked list first odd next even numbers
Here is the implemenation in cpp
http://chanduthedev.blogspot.in/2012/11/c-program-rearrange-linkedlist-first-odd-next-even-elements.html
K – Reverse a linked list
This is one of the questions asked in Microsoft Interview.
Given a linked list, we need to write a function that reverses the nodes of a linked list ‘k’ at a time and returns modified linked list.
The following are the constraints given:
If the no. of nodes in a link list is not a multiple of k then left-out nodes in the end should remain as it is You have to retain the memory address of the nodes without modifying it i.
[Read More]
Find the point of transition from 0 to 1 in an infinite sorted array containings only 0 and 1 .
for(int j=arrIndex/2 ; j < arrIndex;j++) …
shanky - Nov 0, 2014
for(int j=arrIndex/2 ; j < arrIndex;j++)
{
if(array[j]==1)
return j;//this is our index
}
This makes the approach O(n)
Do a regular binary search after finding the cap , with range i/2 to i
Hi Shanky, you are right. That’s what I have done :)
Find the point of transition from 0 to 1 in an infinite sorted array containings only 0 and 1 .
Approach 1(bad): Iterating over the array until we find 1, as the array is sort. In worst case it will go till the last element, and hence this is bad approach.
Approach 2 : Club binary search approach and array’s random access property
Since the array is infinitely increasing i.e. we don’t know array size in advance, so we can’t directly apply traditional BinarySearch (in which we partition the problem size in half in each iteration, kind of top down approach).
[Read More]
Minimum number of coins to get particular amount, given multiple denomination
Question: You are given some denominations of coins in an array (int denom[])and infinite supply of all of them. Given an amount (int amount OR S), find the minimum number of coins required to get the exact amount.
Problem statement
“Given a list of N coins, their values (V1, V2, … , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way that they sum up to S.
[Read More]
Search an element in the sorted rotated array
Question: Implement a search function for a sorted rotated array. Duplicates are allowed. Returning any one of the duplicates is acceptable.
So for example we have sorted array as -
2,3,6,12, 15, 18.
Now suppose the array is rotated k times ( we don’t know k), such that array becomes
15, 18,2,3,6,12
Answer: We can do a binary search with some modified checks.
So lets take arr as array, start be start of the array, end be arr.
[Read More]
Longest increasing subsequence
Longest Increasing Subsequence has been a classic Dynamic Programming problem. O(N^2) has been around for a while but more interesting is the following O(n log n) solution.
Problem Input : Sequence of integers (or any comparable items)
Output : Longest increasing subsequence of the sequence, which may not be contiguous.
For example : We have sequence : 1,8,2,7,3,6,4,5 which has the longest increasing subsequence : 1,2,3,4,5.
Note that the numbers are non-contiguous.
[Read More]