Skip to content

Instantly share code, notes, and snippets.

@hoangbits
Last active November 9, 2016 04:52
Show Gist options
  • Save hoangbits/70c62d986356082a8baeacd47eefe563 to your computer and use it in GitHub Desktop.
Save hoangbits/70c62d986356082a8baeacd47eefe563 to your computer and use it in GitHub Desktop.
INNOVATUBE small test
//package com.example;
public class Solution {
public static void main(String[] args) {
int array[] = new int[4];
array[0] = 2;
array[1] = 3;
array[2] = 1;
array[3] = 5;
System.out.println(Solution.solution(array)); //it should return 4
}
public static int solution(int[] firstArray) {
int i;
//create the second array to compare
int[] secondArray = new int[firstArray.length + 1];
for (i = 0; i < firstArray.length; i++) {
secondArray[i] = firstArray[i] + 1;
}
//find distinct element
int missingElement = 0;
for (i = 0; i < firstArray.length; i++) {
missingElement += (firstArray[i] - secondArray[i]);
}
return Math.abs(missingElement - secondArray[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment