Created
May 13, 2026 05:00
-
-
Save Risyandi/bd28f02ecd129aa8c9cc51232d968ec4 to your computer and use it in GitHub Desktop.
find the positive number in array numeric
This file contains hidden or 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
| import java.util.Arrays; | |
| class Solution { | |
| public int solution(int[] Array) { | |
| int n = Array.length; | |
| boolean[] seen = new boolean[n+2]; | |
| // debug | |
| System.out.println("value n:" + n); | |
| System.out.println("value seen[] 1:" + Arrays.toString(seen)); | |
| for (int x:Array) { | |
| System.out.println("value x:" + x); | |
| if(x > 0 && x <= n + 1){ | |
| seen[x] = true; | |
| } | |
| } | |
| // debug | |
| System.out.println("value seen[] 2:" + Arrays.toString(seen)); | |
| for (int index=1; index <= n + 1; index++){ | |
| // debug | |
| System.out.println("value index:" + index); | |
| System.out.println("value !seen[index]" + !seen[index]); | |
| if (!seen[index]){ | |
| return index; | |
| } | |
| } | |
| return n + 1; | |
| } | |
| // Main method to execute the function | |
| public static void main(String[] args) { | |
| Solution sol = new Solution(); | |
| int[] testArray = {1, 2, 4, 3, 6, 8}; // Example input | |
| int result = sol.solution(testArray); | |
| System.out.println("The smallest missing positive integer is: " + result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment