Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // precondition: 0 <= pos < A.length
- // postcondition: returns the smallest index k such that
- // (pos <= k A.Length) and (A[k] == 0),
- // or -1 if there is no such index
- public int findZero(int[] A, int pos){
- //initalize stuff
- int length = A.length;
- boolean zero = false;
- //check array for zero(s)
- for(int x = 0; x <= length; x++){
- if(A[x] == 0){
- zero = true;
- }
- }
- //if zero(s) are present, then continue
- //otherwise, return -1
- if(zero == false){
- return -1;
- }
- //start at pos, search array for zeros
- for(int x = pos; x <= length; x++){
- if(A[x] == 0){
- //when it finds the first zero after pos, return the index
- return x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement