Arrange Numbers in Array
Arrange Numbers in Array
Send Feedback
Arrange Numbers in Array
You need not print the array. You only need to populate it.
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first and the only line of each test case or query contains an integer 'N'.
For each test case, print the elements of the array/list separated by a single space.
Output for every test case will be printed in a separate line.
1 <= t <= 10^2
0 <= N <= 10^4
Time Limit: 1sec
1
6
1 3 5 6 4 2
Since the value of N is 6, the number will be stored in the array in such a fashion that 1 will appear at 0th index, then 2 at the last index, in a similar fashion 3 is stored at index 1. Hence the array becomes 1 3 5 6 4 2.
2
9
3
1 3 5 7 9 8 6 4 2
1 3 2
void arrange(int *arr, int n) { // Write your code here int start = 0, end = n - 1; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { arr[end] = i; end--; } else { arr[start] = i; start++; } }}
#include <iostream>using namespace std;
#include "solution.h"
void arrange(int *arr, int n){ //Write your code here int start=1,end=n-1; while(end>=start){ if(i%2!=0) { arr[start]=start } }}
int main(){ int t; cin >> t; while (t--) { int n; cin >> n;
int *arr = new int[n]; arrange(arr, n); for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; delete [] arr; }
}
Comments
Post a Comment