• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quick problem to wake up your brain

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have to sort the array , then you can find which number dose not exist ;
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If 5,050 is equal to 100! then you are correct Paul
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, 100! would be a somewhat larger number. But we know what you meant.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

}

public static void main(String[] args) {

SumDemo sDemo = new SumDemo(5);

int actualTotalValue = (99 * (99+1))/2; //sum of n natural numbers (n*(n+1))/2

int arrayTotalValue=0;
for(int i=0; i<sDemo.digits.length;i++){
arrayTotalValue += sDemo.digits[i];
}

System.out.println(" Missing value is: "+(actualTotalValue - arrayTotalValue));


}
}

///Output: Missing value is: 5>
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Legg wrote:Doh.... what's the mathematical sign for 100+99+98....+1 ?



There isn't one as far as I know.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice work Sridhar
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Brian Legg wrote:Doh.... what's the mathematical sign for 100+99+98....+1 ?


There isn't one as far as I know.


No direct formula or symbol, but its looks like an Arithmetic Progression and can sum as :
Sum = n(a1 + a2)/2 = 100(1 + 100)/2 = 5050

And Sridhar , please quote your source code !
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Legg wrote: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.



Isn't it something like :

100
Σ b
b=1
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

George Harris wrote:

Brian Legg wrote: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.



Isn't it something like :

100
Σ b
b=1



Yes, that's what I would write.
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we can also write the product of the first 100 positive integers as

100
∏ n
n = 1

But there's also a single symbol for this, the factorial operator. There's no such single symbol for a sum, as far as I know.
reply
    Bookmark Topic Watch Topic
  • New Topic