Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Arrange Numbers in the Array

Easy
0/40
16 upvotes

Problem statement

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.


Note:
You need not print the array. You only need to populate it.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^4

Time Limit: 1sec
Sample Input 1 :
1
6
Sample Output 1 :
1 3 5 6 4 2
Explanation of Sample Input 1 :
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.
Sample Input 2 :
2
9
3
Sample Output 2 :
1 3 5 7 9 8 6 4 2
1 3 2
Arrange Numbers in the Array
All tags
Sort by
Search icon

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;

    }

}

3 views
0 replies
0 upvotes

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;

    }

}

6 views
0 replies
0 upvotes

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;

        }

    }

}

programming

beginners

120 views
0 replies
1 upvote

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;

        }

java

124 views
0 replies
1 upvote

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;

        }

    }

}

99 views
0 replies
0 upvotes

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++;

            }

        }

 

    }

}

java

74 views
0 replies
1 upvote

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

python

106 views
0 replies
0 upvotes

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--;
        }
    }
}
202 views
0 replies
0 upvotes

Interview problems

Arrange number Array

Solution

159 views
0 replies
5 upvotes

Interview problems

Arrange numbers in the array

Arrange Numbers in the Array

120 views
0 replies
0 upvotes
Full screen
Console