void arrange(int *arr, int n)
{
int i = 0;
while(i < n)
{
// Place the next element on the left side.
arr[i >>1] = ++i;
if (i >= n) break;
// Place the next element on the right side.
arr[n - (i >>1) - 1] = ++i;
}
}
Problem of the day
You have been given an empty array(ARR) and its size N. The only input taken from the user will be N and you need not worry about the array.
Your task is to populate the array using the integer values in the range 1 to N(both inclusive) in the order - 1,3,5,.......,6,4,2.
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'.
Output Format :
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
Interview problems
Using only one variable "i"
void arrange(int *arr, int n)
{
int i = 0;
while(i < n)
{
// Place the next element on the left side.
arr[i >>1] = ++i;
if (i >= n) break;
// Place the next element on the right side.
arr[n - (i >>1) - 1] = ++i;
}
}
Interview problems
Using only 1 loop
#include<bits/stdc++.h>
void arrange(int *arr, int n)
{
//Write your code here
int x = 1 , y = n , mid = ceil(n/2) -1;
if (n % 2 == 1) {
y = n - 1;
mid++;
}
bool flag = 1;
for (int i = 0; i<n; i++)
{
arr[i] = x;
if (i<mid)
x+=2;
else if (flag)
{
x = y;
flag = 0;
}
else
x -= 2;
}
}
Interview problems
c++
void arrange(int *arr, int n)
{
//Write your code here
//Write your code here
if(n%2==0){
for(int i=0;i<n;i++){
if(i<n/2)
arr[i]=2*i+1;
else
arr[i]=(n-i)*2;
}
}
else{
for(int i=0;i<n;i++){
if(i<=n/2)
arr[i]=2*i+1;
else
arr[i]=(n-i)*2;
}
}
}
Interview problems
Short and easy solution
int count=1;
for(int i=0; i<n/2; i++){
arr[i]=count++;
arr[n-1-i]=count++;
}
if(n%2!=0){
arr[n/2]=count;
}
Interview problems
arrange numbers in array
public class Solution {
public static void arrange(int[] arr, int n) {
//Your code goes here
int i=0;
int j=n-1;
int k=1;
while(i<j){
arr[i]=k;
k++;
arr[j]=k;
k++;
i++;
j--;
}
if(i==j) {
arr[i]=k;
}
}
}
easy jaava solution
public class Solution {
public static void arrange(int[] arr, int n) {
//Your code goes here
int a=1,i=0;
while(a<=n){
arr[i]=a;
a+=2;
i++;
}
if(n%2==0){
int b=n;
while((i<n)&&(b>0)){
arr[i]=b;
i++;
b-=2;
}
}
else{
int b=n-1;
while((i<n)&&(b>0)){
arr[i]=b;
b-=2;
i++;
}
}
}
}
Interview problems
Solution in python
def arrange(arr, n) :
if n <= 2:
return
val=0
start = 0
end = n-1
while start<=end:
if val%2==0:
arr[start]=val+1
val += 1
start += 1
else:
arr[end]=val+1
val += 1
end -= 1
#Your code goes here
Interview problems
C++ Solution >>
void arrange(int *arr, int n)
{
int val=1;
int start = 0 ,end = n-1;
while(start<=end){
if(val%2==1){
arr[start]=val;
val++;
start++;
}
else{
arr[end]=val;
val++;
end--;
}
}
}