spammer
BAN USER- 0of 0 votes
AnswersGiven a sorted list but it is rotated. Find the start point in that list
- spammer in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - -1of 1 vote
AnswersPrint the sum of a binary tree
- spammer in United States| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm - 0of 0 votes
AnswersReverse a String any lang you like
- spammer in United States| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm - 1of 1 vote
AnswersGiven AAABBGFF should get an output 3{A} 2{B}1{G}2{F}
- spammer in United States| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm
int arr1[]={2,4,6,7,3};
int arr2[]={6,3,7,4,2};
HashMap<Integer,Integer> hm2=new HashMap<Integer,Integer>();
//1) length 1 = length 2
//2) Every integer shud be there in the table twice
// Handle cases where both of them can have two two values
if(arr1.length == arr2.length)
{
for(int p=0;p<arr1.length;p++)
{
if(hm2.containsKey(arr1[p]))
{
Integer temp = (Integer)hm2.get(arr1[p]);
hm2.remove(arr1[p]);
hm2.put(arr1[p], temp+1);
}
else
hm2.put(arr1[p], 1);
}
for(int p=0;p<arr1.length;p++)
{
if(hm2.containsKey(arr2[p]))
{
Integer temp = (Integer)hm2.get(arr2[p]);
hm2.remove(arr2[p]);
hm2.put(arr2[p], temp-1);
}
else
hm2.put(arr2[p], 1);
}
System.out.println(hm2.values());
int count=0;
for(int l : hm2.values())
{
if(l == 0)
{
count += 1;
}
}
if(count == arr1.length)
System.out.println("They are permutations");
else
System.out.println("They are not permutations");
}
//I am doing it in 3n steps, Can some one do it better
//This approach gives you answer in n/2 steps. This function returns the index of the point where it is rotated
int func(int arr[])
{
int p1,p2;
for(p1=0, p2=arr.length-1; p1>=0,p2<arr.length; p1++,p2-- )
{
if(arr[p1]>arr[p1+1])
return p1;
if(arr[p2]<arr[p2-1])
return p2;
}
}
Ya it can be done in logn time. Here is the code
- spammer May 20, 2012public double power(double d,int i)
{
if(d%2==0)
{
return power(d*d,i/2);
}
else
{
return d*power(d*d,i/2);
}
}