manojsankethi1
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
I suppose Moore’s Voting Algorithm will assume at least half the number of elements are the same.
- manojsankethi1 August 01, 2013Comment hidden because of low score. Click to expand.
0
of 0 vote
public class arrayElementDiff {
public static void main(String[] args){
int[]num = {1, 2, 4, 6, 7, 18, 21, 39, 43, 98, 154};
int givenNum = 150;
int left = 0, right = num.length, temp = 0, diff = 0, nearestNum = 0;
diff = Math.abs(givenNum - num[left]);
for (int i = 0; i < right; i++){
temp = Math.abs(givenNum - num[left]);
if (temp <= diff){
nearestNum = num[i];
diff = temp;
}
left++;
}
System.out.println ("Nearest = " + nearestNum);
}
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Java Solution
- manojsankethi1 August 02, 2013