Problem : Given an array of size 99 which has integers from 1 to 100 and with no integer being repeated, can you find the integer which is not in the array ?
There are many differeny ways of solving this problem. The most efficient solution is to just find the difference between the sum of all integers from 1 to 100 and the sum of all integers in the array. This algorithm has a running complexity of O (n) and a space complexity of O (1) since it just needs 2 variables to store the sums. Here is the code for this -
/**
* This will find the missing integer in the given array of size 99.
*
* The array has integers between 1 and 100 with one integer
* missing.
*
* @param array - Array of integers b/w 1 and 100 of size 99.
* @return int - the missing integer number.
*/
public static int findMissingInteger(int[] array)
{
int sum = 0;
// This has the complexity of O (n)
for (int i = 0; i <>
{
sum += array[i];
}
int sumOneToHundred = (100 * 101) / 2;
return sumOneToHundred - sum;
}
Comments