fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /**
  5.  * @brief Swap the first and last half of an array, in place
  6.  * @param data The input/output array
  7.  * @param len The number of values in the array
  8.  *
  9.  * For example, the input [0, 1, 2, 3, 4, 5] would swap to [3, 4, 5, (half)0, 1, 2]
  10.  */
  11. void SwapArray(int *data, int len) {
  12. // TODO(interviewee): Fill in here
  13. int half = len/2;
  14.  
  15. for (int ii = 0; ii<half; ii++)
  16. {
  17. int temp = data[ii];
  18. data[ii] = data[ii+half];
  19. data[ii+half] = temp;
  20. }
  21. }
  22.  
  23. int main() {
  24. // your code goes here
  25. int data[10];
  26. for (int i = 0; i < 10; i++) {
  27. data[i] = i;
  28. }
  29.  
  30. SwapArray(data, 10);
  31.  
  32. for (int i = 0; i < 10; i++)
  33. printf("%d: %d\n", i, data[i]);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
0: 5
1: 6
2: 7
3: 8
4: 9
5: 0
6: 1
7: 2
8: 3
9: 4