-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrangeNumbersInArray.java
64 lines (55 loc) · 1.17 KB
/
ArrangeNumbersInArray.java
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
53
54
55
56
57
58
59
60
61
62
63
64
/*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,5,.......,6,4,2.*/
package Array_1D;
import java.util.Scanner;
public class ArrangeNumbersInArray
{
public static void numberinArray(int size)
{
int[] arr = new int[size];
if(size%2==0)
{
for(int i=0;i<size/2;i++)
{
arr[i] = 2*i + 1;
}
int p = size;
for(int i=size/2;i<size;i++)
{
arr[i] = p;
p -= 2;
}
printArray(arr);
}
else
{
for(int i=0;i<=size/2;i++)
{
arr[i] = 2*i + 1;
}
int p = size-1;
for(int i=(size+2)/2;i<size;i++)
{
arr[i] = p;
p -= 2;
}
printArray(arr);
}
}
public static void printArray(int arr[])
{
for(int i= 0; i<arr.length; i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();
}
public static void main(String[] args)
{
System.out.print("Enter the number : ");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
numberinArray(size);
}
}