@@ -0,0 +1,1863 @@
+package com.sunny.grokkingalgorithms.basic_2020.warmup;%0A%0Aimport java.util.Arrays;%0A%0Aimport com.sunny.grokkingalgorithms.gpn.findpath.Solution;%0A%0Apublic class PermMissingElement %7B%0A%09%0A%09/*%0A%09 * An array A consisting of N different integers is given. The array contains integers in the range %5B1..(N + 1)%5D, which means that exactly one element is missing.%0A%0AYour goal is to find that missing element.%0A%0AWrite a function:%0A%0Aclass Solution %7B public int solution(int%5B%5D A); %7D%0A%0Athat, given an array A, returns the value of the missing element.%0A%0AFor example, given array A such that:%0A%0A  A%5B0%5D = 2%0A  A%5B1%5D = 3%0A  A%5B2%5D = 1%0A  A%5B3%5D = 5%0Athe function should return 4, as it is the missing element.%0A%0AWrite an efficient algorithm for the following assumptions:%0A%0AN is an integer within the range %5B0..100,000%5D;%0Athe elements of A are all distinct;%0Aeach element of array A is an integer within the range %5B1..(N + 1)%5D.%0A%09 */%0A%09%0A%09public static long solutionAlt(int%5B%5D A) %7B%0A%09%09long n  = A.length + 1;%0A%09%09long sum  = 0;%0A%09%09for(int i = 0; i %3C A.length ; i++) %7B%0A%09%09%09sum  += A%5Bi%5D;%09%09%09%0A%09%09%7D%0A%09%09//System.out.println(sum);%0A%09%09long val = n*(n+1)/2;%0A%09%09//System.out.println(val);%0A%09%09return (val - sum);%0A%09%7D%0A%0A%09public static long solution(int%5B%5D A) %7B%0A%09%09Arrays.parallelSort(A);%0A%09%09if(A.length == 0) %7B%0A%09%09%09return 1;%09%09%09%0A%09%09%7D%0A%09%09if(A.length == 1) %7B%0A%09%09%09if(A%5B0%5D == 1) %7B%0A%09%09%09%09return 2;%09%09%09%09%0A%09%09%09%7Delse %7B%0A%09%09%09%09return 1;%0A%09%09%09%7D%09%09%09%0A%09%09%7D%0A%09%09int missing = 0;%0A%09%09for(int i = 1; i %3C A.length ; i++) %7B%0A%09%09%09if(A%5Bi%5D - A%5Bi - 1%5D %3E 1) %7B%0A%09%09%09%09missing = A%5Bi - 1%5D + 1;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return missing;%0A%09%7D%0A%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09int%5B%5D input = new int%5B%5D %7B2,3,1,5%7D;%0A%09%09System.out.println(solution(input));%0A%09%09input = new int%5B%5D %7B%7D;%0A%09%09System.out.println(solution(input));%0A%09%09input = new int%5B%5D %7B1%7D;%0A%09%09System.out.println(solution(input));%0A%09%09input = new int%5B%5D %7B2%7D;%0A%09%09System.out.println(solution(input));%0A%09%7D%0A%0A%7D%0A

Find missing element