devenster
BAN USER- 0of 0 votes
AnswersTwo numbers are missing from the first hundred numbers. They are NOT sorted. How to find them?
- devenster in India
You can't sort.. and can't iterate one by one.. has to be less than O(N)? Can't use stack , set or any collection interface or in fact any other data structure or array!| Report Duplicate | Flag | PURGE
Siemens Java Developer Algorithm
Following Code works perfectly:
public class SplitWords {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] dict={"jungleegames","that","what","is"};
String query="thatjungleegameswhatisthat";
String query1;
int index;
int beginIndex=0;
for(String s: dict){
if(query.contains(s)){
beginIndex=query.indexOf(s);
index=beginIndex+s.length();
System.out.println("BeginIndex=" + beginIndex + " " + s+ " Index=" + index);
query1=query.substring(beginIndex,index);
if(beginIndex!=0){
query= query.substring(0,beginIndex)+ query1 + " " + query.substring(index);
beginIndex=index;
}
else
{
query=query1+" " + query.substring(index);
}
// beginIndex=index;
}
}
System.out.println(query);
}
}
import java.util.*;
//Assuming first set is larger
public class SortedSetMatching {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Integer> s = new HashSet<Integer>();
s.add(1);
s.add(2);
s.add(3);
int count=0;
Set<Integer> s1=new HashSet<Integer>();
s1.add(1);
s1.add(2);
s1.add(5);
s1.add(3);
Object[] a1=(s1.toArray());
//Integer[] a2=(Integer[])a1;
Iterator<Integer> it = s1.iterator();
//System.out.println(al);
while(it.hasNext())
{
if(count==a1.length)
{
break;
}
if(s.add(it.next()))
{
count++;
}
else
{
System.out.println(a1[count++]);
}
}
}
- devenster March 06, 2013
Following code in Java works:
- devenster August 22, 2013