1

Suppose I create an int array of size N. Then I fill up the array with sorted numbers until index x, where 0 <= x < N-1. How can I find the index x?

Here's one of the examples of the array: {0,1,2,3,4,0,0,0,0,0}, and here's how I generated the array:

int[] arr = new int[10];
for (int i = 0; i < 5; i++) {
    arr[i] = i;
}

Is there any Java syntax for this? I am specifically talking about Array not ArrayList.

====UPDATE====

Sorry for the confusion, the above array was just an example. Suppose the partially initialized array is given, such that we don't know how it was generated. Again, to be clear, the array can also be initialized as {0,0,0,1,2,6,7,0,0,0,0} where the last 0,0,0,0 part is the part from being uninitialized, whereas the first 0,0,0 is deliberately written by somebody else.

3
  • here i is the index or you can use a counter Commented Nov 4, 2014 at 17:54
  • I did, I even upvoted the question. That's the reason why I said I specifically talking about Array and not ArrayList Commented Nov 4, 2014 at 18:03
  • If the partially initialized array is given, then simply take it's length and the one after that is the first non-sorted one, right? Commented Nov 4, 2014 at 18:04

6 Answers 6

1

You should use Integer class and not int primitive data type. Read about more things you can do with Integer class

Integer arr[] = new Integer[10];

This is initialized to null for each element. Now you can add number to it.

public int indexOfArray(Integer [] arr) {
    int i=0;
    while(arr[i]!=null) {
        i++;
    }
    return i;
}
Sign up to request clarification or add additional context in comments.

6 Comments

x from the question is not a parameter but the index searched for.
There is no value to look for.
Nice idea, but I think you just want to return the index of the first null in the array.
This is a great idea. I wonder if I can still do it with primitive int though. P.S: I don't need to find the value. So, I just simply need: Integer arr[] = new Integer[10], suppose some of the elements are initialized in sorted order afterwards, I just need While(arr[i] != null) i++; Then, System.out.println("Last initialized element is at index: " + i);
@user949300 fixed that miss understood !
|
1

You said your array is sorted. So, you can search for the biggest element into the array and then uses Arrays.asList(arr).indexOf(biggestElement);

int biggetsElement = arr[0];
for(int i=1; i < arr.length; i++){
    if(arr[i] > biggestElement){
        biggestElement = arr[i];
    }
}

int index = Arrays.asList(arr).indexOf(biggestElement);

I'm not sure if it will work with int elements, if not you can use Integer elements instead.

2 Comments

[-5,-4,-3,-2,-1,0,0,0,0] what would happen in this case ?
this is a way of doing what he asked. He can adapt the code to fit into his needs. But I am afraid he cannot do what he wants if negative numbers will be used. Just like @user949300 said.
1

Not possible as specified. What if the "sorted values" are all negative and end at -1, e.g.:

[-5,-4,-3,-2,-1,     0,0,0,0]

vs. "sorted values" that are all non-positive but end in 0 (spaces added to emphasize the end of the initial sorted numbers)

[-5,-4,-3,-2,-1,0,    0,0,0]

There's no way to tell afterwards which 0 is the "first" unsorted one.

Comments

0

Assuming you dont know what x is, and assuming that the only value of uninitialized elements is 0, and that all initialized elements cant have the value 0 (which is not true in your case), then you will just have to do a linear search for it -

int i=0;
while(arr[i]!=0) i++;
return i

4 Comments

It won't enter the loop !
@StackFlowed I know, I mentioned that in my preface.
It array was initialized to 0 what if the had an element 0 as well ?
@StackFlowed Did you even read my preface? and that all initialized elements cant have the value 0
0

Create for loop and put in it condition that will check if next element is sorted

Code:

int x

for(int i=0;i<arr.length;i++)
   if(arr[i+1]-arr[i]!=1)
      x=i;

2 Comments

This assumes that at least one of the initial numbers was positive. If they were all negative the loop wont stop. Also, not clear that they differ by 1, I think they are simply sorted.
I just provided an example for that first array. Loop will stop in any case, it iterates only arr.length times.
0

You can use this :

int index = Arrays.binarySearch(theArray, 0);

Condition : the array must be sorted

edit

Use Integer :

Integer[] theArray =  {0, 0, 0, 1, 2, 4, 7, null, null, null};
 int index = Arrays.binarySearch(theArray, null);

1 Comment

-1 Arrays.binarySearch does not necessarily return the first element (null in your case) found. "If the array contains multiple elements with the specified value, there is no guarantee which one will be found."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.