-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathSplitArray.cpp
More file actions
43 lines (36 loc) · 725 Bytes
/
SplitArray.cpp
File metadata and controls
43 lines (36 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// CPP program to split array and move first
// part to end.
#include <bits/stdc++.h>
using namespace std;
// Function to split array and
// move first part to end
void splitArr(int arr[], int length, int rotation)
{
int tmp[length * 2] = {0};
for(int i = 0; i < length; i++)
{
tmp[i] = arr[i];
tmp[i + length] = arr[i];
}
for(int i = rotation; i < rotation + length; i++)
{
arr[i - rotation] = tmp[i];
}
}
// Driver code
int main()
{
int arr[] = { 12, 10, 5, 6, 52, 36 };
int n = sizeof(arr) / sizeof(arr[0]);
int position = 2;
splitArr(arr, n, position);
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
return 0;
}
/*
Output
5 6 52 36 12 10
Time complexity: O(n)
Space complexity: O(2*n)
*/