Click here to Skip to main content
14,301,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have this code to find the smallest missing positive value in an array. In the the method findMissingPositive i don't fully understand how the index is being turned to negative to show that the value of that index has been seen before. This code is from Find the smallest positive number missing from an unsorted array | Set 1 - GeeksforGeeks[^]

This is the code for the method to find smallest missing positing value in an unsorted array.
<pre>    
public static void main(String[] args) {  
      int [] arr = {4, 9, 11,  8, 67};
        
        System.out.println(findMissingPositive(arr, 5));
        
    }   
static int findMissingPositive(int [] arr, int size){
        int i;
        //Mark arr[i] as visited by making 
        // arr[arr[i] - 1] negative. Note that 
        // 1 is subtracted because index start from 0
        // and positive numbers starts from 1.
        
        for(i = 0; i < size; i++){
            int x = Math.abs(arr[i]);
            if(x - 1 < size && arr[x - 1] > 0)
                arr[x - 1] = -arr[x - 1];
        }
        
        // Return the first index value which
        // is positive
        for(i = 0; i < size; i++){
            if(arr[i] > 0){
                return i + 1; // 1 is added because indexes start from 0              
            }
        }   
        return size + 1;
    }


What I have tried:

My issue is in testing the first for loop for my input int [] arr = {4, 9, 11, 8, 67};
First iteration of the first for loop should be - int x = Math.abs(arr[4]).
then, if(4 - 1 < 5 && arr[4- 1] > 0) since this is true, the first iteration would go through and index 3 would be mark negative but on the second iteration, int x = Math.abs(arr[9]).
The if statement should be if(9 - 1 < 5 && arr[9 - 1]) since this would fail, how come it's index would be marked negative? This is where i am missing it, thanks for any help.
Posted
Updated 29-Aug-19 20:23pm
v2
Comments
Maciej Los 30-Aug-19 2:11am
   
What part of statement you don't understand?
// Mark arr[i] as visited by making arr[arr[i] - 1] negative. 
// Note that 1 is subtracted because index start 
// from 0 and positive numbers start from 1 
UT7 30-Aug-19 5:56am
   
@Maciej, i understand everything in the comments. My issue is testing the the first for loop to see how each iteration works. I am having problem with the second iteration. Thanks.
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's not "making the index negative" - that would "point it" at a different part of memory altogether!
Read the comments (and code) again:
//Mark arr[i] as visited by making
// arr[arr[i] - 1] negative. Note that
// 1 is subtracted because index start from 0
// and positive numbers starts from 1.

arr[x - 1] = -arr[x - 1];
What they are saying is to make the value at that index negative, not to try and change the index!
   
Comments
UT7 30-Aug-19 6:01am
   
Thanks.
OriginalGriff 30-Aug-19 6:33am
   
You're welcome!
UT7 30-Aug-19 22:16pm
   
Hello everyone, please don't be offended by my question(s). @OriginalGriff, @Maciej, i get it that it's the value at that index that a negative sign would be added to but for the 2nd, 3rd, 4th and 5th iterations the first part of the if statement should fail, how then would the values at those indexes be turned to negative? Please show me what i am missing.
2nd iteration in the first for loop -
int x = Math.abs(arr[9])
if(9 - 1 < 5 && arr[9 - 1] > 0)
the first condition should fail, how would that value be turned to negative?
Sorry for asking my stupid question, please help me. Many thanks.
OriginalGriff 31-Aug-19 2:01am
   
What I suggest you do is run it in the debugger: you can follow the code through and look at the variable values to find out exactly what is going on. You will be able to see for yourself exactly what it is doing, and why!
UT7 31-Aug-19 2:09am
   
Okay, will do. Thanks
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
i don't fully understand how the index is being turned to negative to show that the value of that index has been seen before.

Index is not changed, value at position of index is changed.
Use the debugger to watch the code performing, it is an incredible learning tool.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
UT7 30-Aug-19 6:02am
   
Thanks.
Patrice T 30-Aug-19 21:59pm
   
you're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100