Last active
November 9, 2016 04:52
-
-
Save hoangbits/70c62d986356082a8baeacd47eefe563 to your computer and use it in GitHub Desktop.
INNOVATUBE small test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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