learner
BAN USER- 0of 2 votes
AnswersGiven an infinite sequence of integers which are repeated many times. WAP to print "beep" if an integer appears ODDth time else print "no beep".
- learner in United States
example: input: a[] = { 1,4,2,4,3,2,4}
output: beep, beep, beep, no beep, beep, no beep, beep
Space complexity - O(1)| Report Duplicate | Flag | PURGE
Microsoft Intern Arrays
The question is to find the inorder successor of the given node.
1. if the node has a rigth child then it is the next node.
2. else if the parent.data is greater than the node.data then it is the next node
3. else if the parent.data is less than the node.data
a) follow the parent till it is null or its data is greater
than the node.data.
i) if a parent node is found whose data is greater then it is the next node
ii) if it is null then there is no next node hence return null
if ( node.right !=null )
return node.right
else if ( node.parent.data > node.data )
return node.parent
else if ( node.parent.data < node.data ) {
temp = node.parent
while(temp !=null &&temp.data < node.data)
temp = node.parent
if(temp > node.data)
return temp
else if(temp==null)
return null
}
I gave the following solution using bit vector:
public static void Beep(int[] a) {
int prev = 0;
int res;
for (int i = 0; i < a.length; i++) {
res = prev ^ (1 << a[i]);
if (res > prev) {
System.out.println("beep");
} else {
System.out.println("no beep");
}
prev = res;
}
}
I have XOR because when the number is repeated even times then 1 flips to 0 which causes decrease in the value. correct me if am wrong. :)
- learner February 10, 2013
RepJeanSwest, Data Engineer at Achieve Internet
I am a nuclear power reactor operator who is responsible for the flow of energy at a power plant. I ...
RepCallaRita, techsupport at Knowledge Systems
Hi, I am an Administrative person . It is to develop and sustain an early childhood organisation. I was a teacher ...
Repjunehudson, Associate at Advisory Board Company
I am passionate about fashion and love to explore black magic to take revenge.Being a fashion designer involves more ...
Repelmothure, Backend Developer at ABC TECH SUPPORT
Social work is a practice-based profession that promotes social change, development, cohesion and the empowerment of people and communities. Social ...
Rep
I think hash table is better as it is thread safe.
- learner February 15, 2013