SChauhan.iitkgp
BAN USER
Comments (3)
Reputation 20
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote
Sorting would require minimum O(nlgn) time. Problem can be easily solved in O(n) time. Below is the java code:
public class Main {
private static String lrgstNm;
public static void main(String[] args) {
int[] inpArrs = new int[]{2,3,5,78};
lrgstNm=new Integer(inpArrs[0]).toString();
for(int i=1;i<inpArrs.length;i++)
findSubSolForN(inpArrs[i], i);
System.out.println(lrgstNm);
}
private static void findSubSolForN(int inpEle, int n){
Integer inpInt = new Integer(inpEle);
if(Integer.parseInt(inpInt+lrgstNm)>=Integer.parseInt(lrgstNm+inpInt)){
lrgstNm=inpInt+lrgstNm;
}
else
lrgstNm+=inpInt;
}
}
Comment hidden because of low score. Click to expand.
0
of 0 vote
isLessThan(...){
......
return p1<q1;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
It is possible in O(n^2). There are C(n,2) pairs involved. Calculate and cache the distance between each of them. Then for each point calculate the 3 min ditance in O(n) time.
- SChauhan.iitkgp December 25, 2011Total time=O(n^2)+O(n)=O(n^2)