-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharrange_numbers_in_array.cpp
More file actions
52 lines (43 loc) · 978 Bytes
/
arrange_numbers_in_array.cpp
File metadata and controls
52 lines (43 loc) · 978 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
44
45
46
47
48
49
50
51
52
// Given an array of size N, we need to append the values from 1 to N in the array such that the odd values gets filled from starting and even values thereafter
// Ex: the numbers from 1 to 6 will get stored in the array in this fashion - 1,3,5,6,4,2
#include <iostream>
using namespace std;
void arrange(int *arr, int n)
{
int lft=0;
int rt=n-1;
int counter=1;
while(lft<=rt){
if(counter%2==1){
arr[lft]=counter;
counter++;
lft++;
}
else {
arr[rt]=counter;
counter++;
rt--;
}
}
}
int main()
{
int t;
cout<<"Enter the number of test cases to run : ";
cin >> t;
while (t--)
{
int n;
cout<<"Enter the size of the array : ";
cin >> n;
int *arr = new int[n];
arrange(arr, n);
cout<<"Enter the elements : ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
delete [] arr;
}
}