September 21, 2019

Find the Missing number in the sequence array with minimum complexity

Algorithms to find the missing numbers from a sequences of consecutive number array using java recursion -  a complete code

/**
 * Find missing number in a sequence Minimum complexity (n Log n)
 *
 * input : {1,2,3,5}
 * output : 4
 */
package com.codecreeks.solutions;

/**
 * @author Konzernites
 *
 */
public class MissingNumberArray {

public static void main(String[] args) {
int[] a = {1,2,3,5,6};
MissingNumberArray m = new MissingNumberArray();
int value = m.findMissingNumber(a, 0, a.length);
System.out.println(value);

}

private int findMissingNumber(int[] array, int startPstn, int EndPstn) {
int arraySize = EndPstn - startPstn;
int position = startPstn + arraySize / 2;
if (arraySize == 1) {
return array[position] - 1;
}
else if (array[position] == position + 1) {
return findMissingNumber(array, position + 1, EndPstn);
} else {
return findMissingNumber(array, startPstn, position);
}
}
}


Output

4



No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments