You are given an array of integers a. A new array b is generated by rearranging the elements of a in the following way:
b[0] is equal to a[0];
b[1] is equal to the last element of a;
b[2] is equal to a[1];
b[3] is equal to the second-last element of a;
and so on.
Your task is to determine whether the new array b is sorted in strictly ascending order or not.
Example
For a = [1, 3, 5, 6, 4, 2], the output should be alternatingSort(a) = true.
The new array b will look like [1, 2, 3, 4, 5, 6], which is in strictly ascending order, so the answer is true.
For a = [1, 4, 5, 6, 3], the output should be alternatingSort(a) = false.
The new array b will look like [1, 3, 4, 6, 5], which is not in strictly ascending order, so the answer is false.
Input/Output
[execution time limit] 3 seconds (java)
[input] array.integer a
The given array of integers.
Guaranteed constraints:
1 ≤ a.length ≤ 105,
-109 ≤ a[i] ≤ 109.
[output] boolean
A boolean representing whether the new array bwill be sorted in strictly ascending order or not.

Respuesta :

Answer:

Explanation:

Take note to avoid Indentation mistake which would lead to error message.

So let us begin by;

import java.util.*; ------ using Java as our programming language

class Mutation {

public static boolean alternatingSort(int n , int[] a)

{

int []b = new int[n];

b[0]=a[0];

int j=1,k=0;

for(int i=1;i<n;i++)

{

if(i%2==1)

{

b[i]=a[n-j];

j++;

}

else

b[i]=a[++k];

}

for(int i=0;i<n;i++)

      {

      System.out.print(b[i]+" ");

      }

      System.out.print("\n");

for(int i=1;i<n;i++)

      {

      if(b[i]<=b[i-1])

      return false ;

      }

     

return true;

}

  public static void main (String[] args) {

      Scanner sc = new Scanner(System.in);

      int n= sc.nextInt();

      int []a = new int [n];

     

      for(int i=0;i<n;i++)

      a[i]=sc.nextInt();

      System.out.println(alternatingSort(n,a));

  }

}

This code should give you the desired result (output).

The program is an illustration of Arrays.

Arrays are variables that are used to hold multiple values using one variable name

The program in Java, where comments are used to explain each line is as follows:

import java.util.Scanner;

public class Main {

//This defines the alternatingSort function

public static boolean alternatingSort(int n , int[] a){

   //This creates an array b

   int b[] = new int[a.length];

   //This populates the first element of array b

   b[0]=a[0];

   //This initializes two counter variables

   int count1 =1,count2 = 0;

   //This iterates from 1 to n - 1

   for(int i=1;i<a.length;i++){

       //This populates array b for odd indices

       if(i%2==1){

           b[i]=a[n-count1];

           count1++;

       }

       //This populates array b for even indices

       else{

           b[i]=a[++count2];

       }

   }

   //This iterates from 0 to n - 1

   for(int i=0;i<a.length;i++){

       //This prints the elements of array b

       System.out.print(b[i]+" ");

   }

   //This prints a new line

   System.out.println();

   //This iterates from 1 to n - 1

   for(int i=1;i<a.length;i++){

       //This checks if the elements of array b are strictly in ascending order

       if(b[i]<=b[i-1]){

           return false ;

       }

     }

     return true;

}

//The main function begins hers

 public static void main (String[] args) {

     //This creates a Scanner object

     Scanner input = new Scanner(System.in);

     //This gets input for the length of the array

     int n= input.nextInt();

     //This creates the array a

     int a[] = new int [n];

     //The following iteration populates array a

     for(int i=0;i<n;i++){

         a[i]=input.nextInt();

     }

     //This calls the alternatingSort function

     System.out.println(alternatingSort(n,a));

 }

}

Read more about arrays at:

https://brainly.com/question/15683939