Arrange Numbers In Array

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,.......4,2.


Sample Input 1 :
6
Sample Output 1 :
1 3 5 6 4 2

public class Solution {
    
    public static void arrange(int[] arr, int n) {
          
        int left=0, right=n-1, counter=1;
		while(left<=right)
        {
			if(counter%2==1) 
                       {
				arr[left++]=counter;

		        }
                        else
                        {
				arr[right--]=counter;
			  }
			counter++; 
}
}    }



Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern