Coding-ninjas
Coding-ninjas copied to clipboard
Arrange Numbers In Array
For the below input : 1 11
orginal output required is : 1 3 5 7 9 11 10 8 6 4 2
But with your code , the output is incorrect !!! Your Output 1 3 5 7 9 0 10 8 6 4 2
NB : IF N IS EVEN NUMBER , the program o/p is correct. FOR ODD NUMBER, the output is incorrect.
public static int[] Arrange(int n)
{
int arr[] = new int[n];
int k =0;
if (n%2==1)
{ for (int i=1 ;k<((n+1)/2);i = i+2)
{
arr[k] = i;
k++;}
for(int i= n-1;k<n;i=i-2)
{
arr[k] = i;
k++;
}
}
else if (n%2==0)
{for (int i=1 ;k<n/2;i = i+2)
{
arr[k] = i;
k++;
}
for(int i= n;k<n;i=i-2)
{
arr[k] = i;
k++;
}}
return arr;
}
public static void main(String args[])
{
int arr4[] = Arrange(69);
for(int i=0;i<69;i++)
{
System.out.print(arr4[i] + " ");
}
}
}
it we use n=4 then output is 1 3 4 2 0 0 0 0 please solution of this problem
if we use n=4 then output is 1 3 4 2 0 0 0 0 please solution of this problem
Change for loop it is set to run upto 69 times.