Skip to main content

Find missing integer problem

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

Popular posts from this blog

Even Integer Iterator

Problem : Given an iterator of integers, can you give me back another iterator which iterates over the given iterator of integers such that the new iterator gives only even integers. There a couple of different ways to solve this problem. * The first option is to create a new list of integers and add only the even integers from the given iterator and then return an iterator by calling list.iterator(). This has a time complexity of O (n) but also has a space complexity of O (n) in the worst case, which is not desirable. Here is the code for it - public static Iterator<Integer> giveEvenIntegersIterator(Iterator<Integer> iterator) { List<Integer> evenIntegers = new ArrayList<Integer>(); if (iterator != null) { while(iterator.hasNext()) { Integer number = iterator.next(); if (number != null && number % 2 == 0) { evenIntegers.add(number); } } } retu

Sum of Subsets - Find how many pairs in the given array sum to a given number

Problem : Given an array of integers intArray[] and another integer sum, find how many pairs of numbers in the given array will sum up to the given sum. This problem is one of the most sought after question in interviews. There may be slight variations of this problem. There are many ways to solve this problem. The most efficient solution considering both time and space complexity is the one discussed below - /** * This will find how many pairs of numbers in the given array sum * up to the given number. * * @param array - array of integers * @param sum - The sum * @return int - number of pairs. */ public static int sumOfSubset(int[] array, int sum) {          // This has a complexity of O ( n lg n )         Arrays.sort(array);         int pairCount = 0;         int leftIndex = 0;         int rightIndex = array.length - 1;          // The portion below has a complextiy of         //  O ( n ) in the worst case.         while (array[rightIndex] >= su

Minimum Maximum Stack

Problem : Design a stack of Numbers which will give you the minimum and maximum of all the elements it contains. This problem is also known as MinStack or MaxStack problem which expects you to find only the minimum or maximum. It is restated here to capture both. A stack typically has push(), pop() and size() operations. In order to find the minimum and the maximum, we will need to iterate over the elements of the stack each time we want to find the minimum and maximum. This will result in O (n) time complexity. And typically stack does not let us iterate over the elements. Many do not know that the java.util.Stack implementation does allow it as it extends java.util.Vector. We can make it more efficient by storing the minimum and maximum as instance variables in our stack and update them when ever we push a new number on to the stack. This will result in O(1) complexity for updating the max and min when pushing new numbers on stack, but when we pop the number which is currently set a