6

I have a recent interview question, reordering of elements in an array with minimum memory usage. Not to use any additional variable or collections etc.

input:

value   65 7 1 68 90
index    0 1 2  3  4

output:

value   90 68 1 7 65
index    0  1 2 3  4
halfer
  • 19,824
  • 17
  • 99
  • 186
developer
  • 9,116
  • 29
  • 91
  • 150

3 Answers3

5

You can use XOR to swap between elements (first with last, second with second from the end, etc) as follows:

int [] arr = {65, 7, 1, 68, 90};
for(int i=0; i<arr.length/2; i++){
    // the following 3 lines swap between elements arr[i] and arr[arr.length-i-1]
    arr[i] = arr[i] ^ arr[arr.length-i-1];
    arr[arr.length-i-1] = arr[i] ^ arr[arr.length-i-1];
    arr[i] = arr[i] ^ arr[arr.length-i-1];
}

for(int i=0; i<arr.length; i++){
    System.out.print(arr[i]+" ");
}

OUTPUT

90 68 1 7 65 
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
5

The main issue here is swapping array elements without using temp variable. You can make use of XOR(^) operation like this:

For swapping a and b:

int a = 4;
int b = 5;
a ^= b
b ^= a
a ^= b

Now, you just need to iterate over the array from index = 0 to index = length / 2, and swap elements at beginning and end.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    The temporary values have to stored somewhere, so this won't use less memory than using a temporary variable. – Joni Aug 20 '13 at 18:49
  • 1
    @FDinoff. How would that make any difference? – Rohit Jain Aug 20 '13 at 18:51
  • See http://javarevisited.blogspot.cz/2013/02/swap-two-numbers-without-third-temp-variable-java-program-example-tutorial.html – Martin Podval Aug 20 '13 at 18:52
  • @Joni. I don't think the temporary results will be stored in any temporary variable. The result is directly assigned to existing variable. Check the byte code using `javap`. – Rohit Jain Aug 20 '13 at 18:57
  • The temporary result of XOR will be stored in a register, just like a temporary variable. You can check this by disassembling the output of the JIT compiler; byte code does not really tell you what the program is going to execute. – Joni Aug 20 '13 at 18:59
  • @Joni. Can you tell me how to disassemble the output of JIT compiler? I would really like to see what happens behind the scenes. – Rohit Jain Aug 20 '13 at 19:06
  • Welcome to the rabbit hole :) http://mechanical-sympathy.blogspot.com/2013/06/printing-generated-assembly-code-from.html – Joni Aug 20 '13 at 19:19
  • @Joni. Thank you so much. That is simply precious. But I see no Windows version there. I guess I need to open my Ubuntu now. :) – Rohit Jain Aug 20 '13 at 19:23
  • More info here, with a link to download the disassembler plugin for Windows x86: https://wikis.oracle.com/display/HotSpotInternals/PrintAssembly – Joni Aug 20 '13 at 19:37
  • @Joni. Thanks :) Was going through that link only. – Rohit Jain Aug 20 '13 at 19:41
2

It looks like the elements are simply reversed. You can do this without additional arrays, with whatever implementation of the swap that you feel like using:

int b = 0, e = array.length-1;
while (b < e) {
    array.swap(b, e);
    b = b + 1;
    e = e - 1;
}

For integers you can use "storageless swap" by computing a sum and subtracting, by XORing, etc. One would never do that in production, though - it's a useless interview trick invented at the time when hardware engineers doubled as programmers more often than they do now (I saw this problem formulated in terms of hardware logical gates some 25 years ago).

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523