Given an array of integers 1 to 100 (inserted randomly), and one integer is taken out of the array. What is the most efficient way of finding the integer that is missing?

As you know the integers, make a sum of all of them:

```
(1+N)*N/2 = (1+100)*100/2 = 5050
```

And now substract the sum of those that are in the array (** S'**). The difference will be the one missing number you seek (so

`x = 5050 - S'`

).Time complexity is O(N) and can't be solved faster, because you definitely need to read the array once.

MZetko already answer the basic case but here are 4 other solutions to this where the array can be sorted or unsorted

Similar Questions

What's the most efficient way to select multiple entities by primary key? public IEnumerable<Models.Image> GetImagesById(IEnumerable<int> ids) { //return ids.Select(id => Images.Find(id

I have an array of Strings: String[] stringArray = {x, y, z, x, x, y, a}; What is the quickest/most efficient way to order this into a smaller Collection in order of how frequent each S

I am working on a project that will run many thousands of comparisons between dates to see if they are in the same month, and I am wondering what the most efficient way of doing it would be. This isn

I have a string in this format: ABC_123_ I want to end up with and integer variable that is just the number portion of the string. What is the most efficient way to accomplish that in C#?

It's simple to do a webrequest, get the page and parse the html to get the title of the page. But I'm wondering if there is a more efficient way that is faster, and doesn't require downloading the wh

What is the most efficient way to continually add two numbers together? For instance, I have something like the following: totalFloat = totalFloat + someFloatVar There's gotta be a more efficient way

Could please anyone provide some help in finding a way to process user input (from a post), which is held as variables (obviously) and in which variables' names are correspondent with extracted from d

Given two inclusive integer ranges [x1:x2] and [y1:y2], where x1 <= x2 and y1 <= y2, what is the most efficient way to test whether there is any overlap of the two ranges? A simple implementatio

What's the most efficient way to rewrite the following: mv *.jpg ~/Pictures && mv *.gif ~/Pictures && mv *.png ~/Pictures

I am working on a jQuery Mobile Web App where a company will have the ability to message certain groups of users (based on their profile preferences). I am debating on what is the most efficient way t

An array a[] contains all of the integers from 0 to N, except one. However, you cannot access an element with a single operation. Instead, you can call get(i, k) which returns the kth bit of a[i] or y

I've been wondering what the most efficient way to reverse the order of a BitArray in C#. To be clear, I don't want to inverse the Bitarray by calling .Not(), I want to reverse the order of the bits i

What's the most efficient way to serialize finite (non-recursive) algebraic-data-types which are comprised only of constructors? e.g. p = A | B q q = C | D r | E r = F | G Manually enumerating all v

I'm working with processing, and I was wondering what the best (most efficient) way of ordering an array was. I basically want to be able to write a function that could take an array with, say, the in

What is the most efficient way to implement a phonetic search in C++ and/or Java? By phonetic search I mean substituting vowels or consonants that sound similar. This would be especially useful for na

Suppose an array named myArray containing several values but no duplicates. Suppose I want to push a value into it only if it won't lead to duplicates presence. How I determinate duplicates => by c

Wondering what is the best way to log Simulink signals which outputs large data sets (couple of gigs worth) in a way that is RAM efficient and with minimal sacrifice to simulation speed?

I have a loop that goes from 3 to the phi of two prime user-inputted numbers (these numbers can be any length), finds all the prime numbers between the two, and stores them into an array. My code for

Possible Duplicate: What is the best way to deal with DBNull's What is the most efficient syntax in .NET to set a variable that has a value of DBNull (aka SQL null value being returned)? I remember

What is the most efficient way to insert one Vector into another at specific position? For example: var aa:Vector.<int> = Vector.<int>([1, 2, 3]); var bb:Vector.<int> = Vector.<in

I've been trying to figure out the most efficient way where many threads are altering a very big byte array on bit level. For ease of explaining I'll base the question around a multithreaded Sieve of

Given an input String, what is the most efficient way to make just the first character lower case? I can think of a number of ways to do this. For example, using charAt and subString: String string=

What would be the most efficient way to multiply each column of a data frame by a vector? e.g. data frame (df) has the following columns (col1, col2, col3, col4) and vector (v) has the following eleme

What would be the most efficient way to SELECT this, then DELETE it immediately. SELECT * from `usersOnline` WHERE timestamp>NOW()-INTERVAL 5 SECOND ORDER BY rand() LIMIT 1; How could I take this

What is the most efficient way to groupby objects in an array? For example, given this array of objects: [ { Phase: Phase 1, Step: Step 1, Task: Task 1, Value: 5 }, { Phase: Phase 1, Step:

Unfortunately I need to iterate over all the DOM elements of a page and i'm wondering what the most efficient technique is. I could probably benchmark these myself and might if I have the time but i'm

I am getting the index of a row by doing this: row.parent().children(tr).index(row) Is there a more efficient way to find the index? I have hundreds of rows so it is killing my performance that I h

What's this easiest / most efficient way to initialize these blocks of doubles, preferably at compile time: #define N 1000 double mul1[N][N] __attribute__ ((aligned (64))); double mul2[N][N] __attribu

I have an object that contains two arrays, the first is a slope array: double[] Slopes = new double[capacity]; The next is an array containing the counts of various slopes: int[] Counts = new int[cap

I need to get just the first item (actually, just the first key) off a rather large associative array in JavaScript. Here's how I'm doing it currently (using jQuery): getKey = function (data) { var fi

elasticsearch has several APIs for submitting documents (http, thrift, memcached). What's the most efficient way to submit a document in terms of resources used? My use case is about to have quite int

What is the most efficient way, in terms of cost and scalability, to pull stats on large volumes of data? Let's take a concrete example, there are 1000 companies, each with 10000+ customers. These com

I can get integer permutations like this: myInt = 123456789 l = itertools.permutations(str(myInt)) [int(''.join(x)) for x in l] Is there a more efficient way to get integer permutations in Python, sk

What is the most memory efficient way to loop through an NSMutableArray of custom objects? I need to check a value in each object in the array and return how many of that type of object is in the arra

I want to convert an integer to the fractional part of a number using javascript. For example: 10030 -> 0.10030 123 -> 0.123 I've come up with two ways of doing this: var convertIntegerPartToF

This question already has an answer here: How to find the kth largest element in an unsorted array of length n in O(n)? 22 answers I am wondering if this is the most efficient way to find the n

I'm looking for a more efficient way to fill a 2d numpy array than a double for-loop. The issue I am having is that the array values are dependent on several other arrays. In the following code k and

I'm just wondering what the opinion is on the most efficient way to migrate data to new schema in SQL server 2008. The database has 7 tables and is 8gb in size. I briefly tried using a .NET app and th

Given an array of strings, such as $a: $a = array(zero, one, cat); I'm looking to create array $b, populated along the diagonal with values from $a: $b[0] = [zero, -, -] $b[1] = [-,

Considering developer's perspective, what's the most efficient way to create, maintain, and improve a complex Web UI. I'm familiar with a bunch of toolkits like ext.net, telerik, devx. Silverlight is

What's the most efficient way to remove the text 2010-04-07 14:25:50,773 DEBUG This is a debug log statement - from a log file like the extract below using Vim? 2010-04-07 14:25:50,772 DEBUG This is

Is there a known 'most efficient' version of the A* search algorithm? I know some people write papers on the most efficient way to compute common operations, has this been done for A*? specifically th

Let's say I have an array of quantity ranges: [{min=1, max=500}, {min=2, max=1000}, ...] What is the most efficient way to validate that the ranges do not overlap (the above would fail validation)?

I have a pre-defined 6 digit number, and an Entry model with a 'number' field (integer) that will have several thousand randomly generated entries per month. I need to find the best way to find the en

I have a multidimensional array. $array[0] = array(1, 8, 2); $array[1] = array(5, 6, 15); $array[2] = array(-8, 2, 1025); I am wondering what the most efficient way to order the parent array by a par

Out of the following, which is the best way to hide a view? Setting frame to CGRectZero Hidden Property to YES alpha Property to 0.0 Does one have benefits that the other does not? In terms of savin

I was wondering what is the most efficient way to parse JSON in C#? And by efficient I mean the one with the lower response time. I am trying to parse a large amount of data using a couple of methods,

Is this a right and most efficient way to remove blank string from NSArray? int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *myStrings =

What is the most efficient way to duplicate a row in an Sqlite3 database exactly except with an updated PrimaryKey?

This question already has an answer here: Find the Smallest Integer Not in a List 19 answers Given an unsorted set A what is the most efficient solution for finding the smallest integer x which