akash.kotadiya2000@gmail.com
BAN USERIt would require 11 races. on each race 2 horses will be eliminated. After 11 races 11*2=22 horses eliminated and 3 will be fastest.
- akash.kotadiya2000@gmail.com June 29, 2011<pre lang="" line="1" title="CodeMonkey6518" class="run-this">package com.DataStructures;
import java.util.HashMap;
public class RemoveDuplicateFromLinkedList {
HashMap<Integer, Character > map=new HashMap<Integer, Character>();
public void removeDuplicates(LinkedListNodeInt current){
LinkedListNodeInt previous=null;
while(current!=null){
if(map.containsKey(current.value)){
if(current.value%2!=0)
previous.link=current.link;
else
previous=current;
}
else{
map.put(current.value,' ');
previous=current;
}
current=current.link;
}
}
public static void main(String args[]){
RemoveDuplicateFromLinkedList obj=new RemoveDuplicateFromLinkedList();
LinkedListNodeInt node=new LinkedListNodeInt();
node.insert(1);
node.insert(2);
node.insert(5);
node.insert(60);
node.insert(3);
node.insert(5);
node.insert(60);
node.insert(5);
obj.removeDuplicates(node.link);
node.traverse();
}
}
</pre><pre title="CodeMonkey6518" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey97440" class="run-this">package com.DataStructures;
public class StackUsingQueue {
Queue enQueue=new Queue();
Queue deQueue=new Queue();
public void push(int item){
enQueue.insert(item);
}
public int pop(){
int item = 0;
while(!enQueue.isEmpty()){
item=enQueue.remove();
if(!enQueue.isEmpty()){
deQueue.insert(item);
}
}
// Swapping References
Queue tmp=enQueue;
enQueue=deQueue;
deQueue=tmp;
return item;
}
public static void main(String[] args) {
StackUsingQueue obj=new StackUsingQueue();
obj.push(10);
obj.push(30);
obj.push(50);
obj.push(70);
obj.push(100);
System.out.println(obj.pop());
System.out.println(obj.pop());
System.out.println(obj.pop());
}
}
</pre><pre title="CodeMonkey97440" input="yes">Implemented as mentioned by Ankit Garg above.</pre>
<pre lang="" line="1" title="CodeMonkey71724" class="run-this">public int RepeatedThrice(int[] array){
QuickSort sortAlgo=new QuickSort();
sortAlgo.sort(array,0, array.length-1);
int count=0;
for(int i=0;i<array.length-3;i++){
count=0;
for(int j=i+1;j<array.length;j++){
if(array[i]==array[j]){
count++;
continue;
}
else
break;
}
if(count==2)
return array[i];
else
i=i+count;
}
return -1;
}</pre><pre title="CodeMonkey71724" input="yes">Simple solution would be : sort and count for exact 3 matches. Complexity would be around O(nlogn+n).</pre>
<pre lang="" line="1" title="CodeMonkey33975" class="run-this"> public void reversePair(){
LinkedListNode previous=head.link; // First Element
LinkedListNode current=head.link.link; // Second Element
LinkedListNode tmp=null;
LinkedListNode tmp2=null;
head.link=current;
while(previous.link!=null && current.link!=null){
tmp=current.link;
tmp2=previous;
previous.link=current.link.link;
current.link=previous;
previous=tmp;
current=tmp.link;
}
if(previous.link!=null){
previous.link=current.link;
current.link=previous;
}
else{
tmp2.link=previous;
}
}
</pre><pre title="CodeMonkey33975" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey50411" class="run-this">package com.DataStructures;
public class Trie {
Trie[] nodes;
boolean isEnd;
public Trie(){
nodes=new Trie[26];
isEnd=false;
}
public void insert(String word){
Trie current=this;
for(int i=0;i<word.length();i++){
int index=word.charAt(i)-'A';
if(current.nodes[index]==null){
current.nodes[index]=new Trie();
}
current=current.nodes[index];
}
current.isEnd=true;
}
public boolean isMatched(String word){
Trie current=this;
int index=0;
for(int i=0;i<word.length();i++){
index=word.charAt(i)-'A';
if(current.nodes[index]!=null){
current=current.nodes[index];
}
else{
return false;
}
}
if(!current.isEnd){
return false;
}
return true;
}
public boolean isMadeOfTwoWords(String word){
int begin=0;
int count=0;
for(int i=0;i<word.length();i++){
if(isMatched(word.substring(begin,i+1))){
count++;
begin=i+1;
}
if(count==2){
if(i==word.length()-1)
return true;
else
return false;
}
}
return false;
}
public static void main(String args[]){
Trie trie=new Trie();
trie.insert("NEWSPAPER");
trie.insert("NEWS");
trie.insert("PAPER");
trie.insert("GOOGLE");
trie.insert("HELLLO");
System.out.println(trie.isMadeOfTwoWords("NEWSPAPERS"));
}
}
</pre><pre title="CodeMonkey50411" input="yes">Its Complete implementation with TRIE. Basic assumption is Dictionary stored as TRIE.</pre>
nice solution. well done gulusworld1989.
- akash.kotadiya2000@gmail.com June 24, 2011<pre lang="" line="1" title="CodeMonkey95843" class="run-this">public boolean isBST(Node localRoot){
if(localRoot.leftChild!=null){
if(localRoot.leftChild.key < localRoot.key)
return isBST(localRoot.leftChild);
else
return false;
}
if(localRoot.rightChild!=null){
if(localRoot.rightChild.key > localRoot.key)
return isBST(localRoot.rightChild);
else
return false;
}
return true;
}</pre><pre title="CodeMonkey95843" input="yes">
Seems simple. </pre>
- akash.kotadiya2000@gmail.com September 12, 2011