Skip to main content

Pendulum Arrangement Program in Java

 import java.util.Scanner;
class pendulamArrangement
{
 public static void main(String args[])
 {
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 11 numbers: ");
  int[] A = new int[11];
  int n = A.length;
  for(int i=0;i<n;i++)
  {
   A[i]=sc.nextInt();
  }
  for(int i=0;i<n-1;i++)
  {
   for(int j=0;j<n-i-1;j++)
   {
    if(A[j]<A[j+1]) //arranging in descending order
    {
     int t = A[j];
     A[j]=A[j+1];
     A[j+1]=t;
    }
   }
  }
  //new array of equal size
  int[] B = new int[n];
  int r=1,l=1;
  //placing the highest value into the central position
  int mid=n/2;
  B[mid]=A[0];
  for(int i=1;i<n;i++)
  {
   if(i%2==0) //if index is even then place the element on the right side of the mid by increasing the r value
   {
    B[mid+r]=A[i];
    r=r+1;
   }
   else //similarly if the index is odd then place the element on the left side of the mid by increasing the l value
   {
    B[mid-l]=A[i];
    l=l+1;
   }
  }
  System.out.println("The new array in the particular arrangement is ");
  for(int i=0;i<B.length;i++)
  {
   System.out.println(B[i]);
  }
 }
}

Popular posts from this blog