There are 99 unique numbers in an array. All numbers are from 1 to 100 with 1 random number missing. The numbers are not in order.
How can you figure out what the missing number is if your only allowed to pass through the array 1 time?
I realize this is probably too easy for most of you but this should wake up your brain on a Friday morning. Please don't post the answer if you've heard this before
Bonus: Post your answer in Java code - only 4 lines needed for my solution including the print statement which shows the answer.
SCJA
~Currently preparing for SCJP6
yousef atya
Greenhorn
Joined: May 29, 2009
Posts: 3
posted
0
I think you have to sort the array , then you can find which number dose not exist ;
I don't think I heard that before, but you just add up the 99 numbers and subtract the total from 5050. I know this is "Programming Diversions" but as you imply, the programming is trivial once you know what to do.
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
posted
0
If 5,050 is equal to 100! then you are correct Paul
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2127
posted
0
Well, 100! would be a somewhat larger number. But we know what you meant.
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
posted
0
Doh.... what's the mathematical sign for 100+99+98....+1 ? I thought it was 100! but I guess that is 100 * 99 * 98, etc.
Sridhar Gudipalli
Ranch Hand
Joined: Nov 02, 2005
Posts: 120
posted
0
package com.my;
public class SumDemo {
int digits[]= new int[99];
public SumDemo(int missingValue) {
//Initialize the array with 98 unique values.
for(int i=0;i<99; i++){
if(i==missingValue-1){
continue;
}
this.digits[i]=i+1;
}