1

I need some help inserting the number 8 into an array that gives me random values. The array must be in order. For example if I had an array of (1,5,10,15), I have to insert the number 8 between 5 and 10. I am having a problem on how I can figure our a way to find the index where 8 will be placed because the array is random, it can be anything. Here is my code so far :

public class TrickyInsert {

public static void main(String[] args) {

    int[] mysteryArr = generateRandArr();

    //print out starting state of mysteryArr:
    System.out.print("start:\t");
    for ( int a : mysteryArr ) {
        System.out.print( a + ", ");
    }
    System.out.println();


    //code starts below

    // insert value '8' in the appropriate place in mysteryArr[]
    int[] tmp = new int[mysteryArr.length + 1];
    int b = mysteryArr.length;
    for(int i = 0; i < mysteryArr.length; i++) {
        tmp[i] = mysteryArr[i];

    }
    tmp[b] = 8;
    for(int i =b ; i<mysteryArr.length; i++) {
        tmp[i+1] = mysteryArr[i];
    }
    mysteryArr = tmp;

any tips? thanks!

3
  • Yes, the array is random but with ordered numbers. My job is to write code that can figure out where 8 can be placed to make the array in order. My code above is wrong. Just wanted to give you guys an idea of what I had first :/ Commented Feb 11, 2015 at 1:22
  • I believe it has something to do with int b, that's the part im really stuck on Commented Feb 11, 2015 at 1:23
  • do you need to sort the array or figure out where the 8 would go? Commented Feb 11, 2015 at 1:48

5 Answers 5

5

Simply add the number then use Arrays.sort method,

int b = mysteryArr.length;
int[] tmp = new int[b + 1];    
for(int i = 0; i < b; i++) {
    tmp[i] = mysteryArr[i];
}
tmp[b] = 8;
mysteryArr = Arrays.sort(tmp);
Sign up to request clarification or add additional context in comments.

Comments

1

In your example the random array is sorted. If this is the case, just insert 8 and sort again.

Comments

1

Simply copy the array over, add 8, and sort again.

        int[] a = generateRandArr();

        int[] b = Arrays.copyOf(a, a.length + 1);

        b[a.length] = 8;

        Arrays.sort(b);

Comments

0
int findPosition(int a, int[] inputArr)
 {
  for(int i = 0; i < inputArr.length; ++i)
   if(inputArr[i] < a)
    return i;
   return -1;
 }

int[] tmpArr = new int[mysteryArr.length + 1];
int a = 8;   // or any other number
int x = findPosition(a, mysteryArr);
if(x == -1)
 int i = 0;
 for(; i < mysteryArr.length; ++i)
   tmpArr[i] = mysteryArr[i];
 tmpArr[i] = a;
else
 for(int i = 0; i < mysteryArr.length + 1; ++i)
  if(i <  x)
   tmpArr[i] = mysteryArr[i];
  else if(i == x)
   tmpArr = a;
  else
   tmpArr[i] = mysteryArr[i - 1];

Comments

0

I will suggest using binary search to find the appropriate index. Once you locate the index, you can use

System.arraycopy(Object src, int srcIndex, Obj dest, int destIndex, int length)

to copy the left half to your new array (with length one more than the existing one) and then the new element and finally the right half. This will stop the need to sort the whole array every time you insert an element.

Also, the following portion does not do anything.

for(int i =b ; i<mysteryArr.length; i++) {
    tmp[i+1] = mysteryArr[i];
}

since int b = mysteryArr.length;, after setting int i =b ;, i<mysteryArr.length; will be false and hence the line inside this for loop will never execute.

Comments

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.