-2

i have one problem for example we have array

int a[]=new int[]{a1,a2,a3,a4,..........an}:

task is fill the same array by elements which are not in the array for example a={1,3,4,5,6,7} should be filled by any numbers {2,8,9,12,13,90}or others but not by elements which are in array this must not be{1,12,13,14,110} because 1 is in the array a thanks

  • Are we assuming an ordered array? – Jan K. Jun 17 '10 at 10:17
  • 3
    Is this homework? What have you so far? – Felix Kling Jun 17 '10 at 10:18
  • no order does not matter it may be 12,4,3,5,7,8,89 –  Jun 17 '10 at 10:18
  • 3
    I don't get it. When do you stop filling? Are you only given part of the array and its full size and you must complete it? Please detail what the input is. – IVlad Jun 17 '10 at 10:21
  • 1
    If any number is allowed, you could take the maximum value of the array, let's say N, and then fill the array with N+1, N+2, N+3, ... – Patrick Jun 17 '10 at 10:23
  • input are integers unordered integers –  Jun 17 '10 at 10:24
  • @Patrick that'll result in an infinite loop and crash when the size of the datatype is reached or memory is 0. – Jan K. Jun 17 '10 at 10:25
  • @Patrick great post it as answer –  Jun 17 '10 at 10:25
  • only one problem is that maximum element+1 may come with oveflow of integer types –  Jun 17 '10 at 10:27
  • 1
    @Davit I suggest you clarify the problem statement. We're shooting in the dark here. And your revelation - I just said that. – Jan K. Jun 17 '10 at 10:28
  • 2
    I'm guessing he wants to overwrite the original array with numbers that aren't in it. An interesting problem if constrained to `O(1)` memory and `O(n)` time. – IVlad Jun 17 '10 at 10:33
  • @IVlad: It is possible. See my answer. –  Jun 17 '10 at 14:44

2 Answers2

2

Interesting problem.

If the array is of signed integers, I believe it is possible in O(n) time and O(1) space, with no overflows, assuming the length is small enough to permit such a thing to happen.

The basic idea is as follows:

We have n numbers. Now on dividing those numbers by n+1, we get n remainders. So atleast one of the remainders in {0,1,2, ..., n} must be missing (say r). We fill the array with numbers whose remainders are r.

First, we add a multiple of n+1 to all negative numbers to make them positive.

Next we walk the array and find the remainder of each number with n+1. If remainder is r, we set a[r] to be -a[r] if a[r] was positive. (If we encounter negative numbers when walking, we use the negated version when taking remainder).

We also have an extra int for remainder = n.

At the end, we walk the array again to see if there are any positive numbers (there will be one, or the extra int for remainder = n will be unset).

Once we have the remainder, it is easy to generate n numbers with that remainder. Of course we could always generate just one number and fill it with that, as the problem never said anything about unique numbers.

If the array was of unsigned integers, we could probably still do this with better book-keeping.

For instance we could try using the first n/logn integers as our bitarray to denote which remainders have been seen and use some extra O(1) integers to hold the numbers temporarily.

For eg, you do tmp = a[0], find remainder and set the appropriate bit of a[0] (after setting it to zero first). tmp = a[1], set bit etc. We will never overwrite a number before we need it to find its remainder.

0

Just get the highest and lowest number in the array, create a new array with elements from your lower bound value to n.

Obtaining the highest and lowest number can be done in the same loop.

Assuming 12,4,3,5,7,8,89, it'll detect 3 as the lowest, 89 as the highest value. It then creates a new array and fills it with 3..89; then discard the old array.

Jan K.
  • 1,607
  • 2
  • 13
  • 22
  • i want do it without additional array –  Jun 17 '10 at 10:24
  • have to sorry it is constraint –  Jun 17 '10 at 10:30
  • You have to make an additional array because an array is static in size; well, depending on the language. Assuming that it's not static in size like in PHP, obtain the highest and lowest value, and simply overwrite your existing array. If that's what the bounds are. You still haven't clarified that. – Jan K. Jun 17 '10 at 10:34