Coding-ninjas icon indicating copy to clipboard operation
Coding-ninjas copied to clipboard

Arrange Numbers In Array

Open jgpulk opened this issue 4 years ago • 3 comments

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.

jgpulk avatar Jun 29 '21 12:06 jgpulk

    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] + " ");
                }
    }
}

surajsharma68 avatar Nov 28 '21 16:11 surajsharma68

it we use n=4 then output is 1 3 4 2 0 0 0 0 please solution of this problem

dikshasharma1622 avatar Apr 15 '22 17:04 dikshasharma1622

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.

GGauravKKumar avatar Jul 05 '22 12:07 GGauravKKumar