Question

In: Computer Science

Given an array a0, a1 ,a2,....an-2, an-1, call function Rearrange to rearrange the array elements so...

Given an array a0, a1 ,a2,....an-2, an-1, call function Rearrange to rearrange the
array elements so that the new arranged array is: a0, an-1 ,a1, an-2......
If the array was
5,7,13,4,9
Then after calling function Rearrange Array elements are: 5, 9, 7, 4, 13
in java

Solutions

Expert Solution

// Java program to create and test rearrange method

public class Rearrange {

       // method to rearrange the array elements

       public static void rearrange(int array[])

       {

             int temp[] = new int[array.length]; // create a temporary array of same length as input array

             int i=0, j=0; // set i and j to start index

             // loop that continues till we reach middle of the array

             while(i<array.length/2)

             {

                    temp[j] = array[i]; // set ith element of array to jth element of temp

                    j++; // increment j

                    temp[j] = array[array.length-1-i]; // set ith element from last of array to jth element of temp

                    j++; // increment j

                    i++; // increment i

             }

            

             // if array contains odd elements , copy the middle element of array to temp

             if(array.length%2 != 0)

             {

                    temp[j] = array[array.length/2];

             }

            

             // copy the element from temp back to array

             for(i=0;i<array.length;i++)

             {

                    array[i] = temp[i];

             }

            

       }

       public static void main(String[] args) {

      

             // test the method

             int arr[] = {5,7,13,4,9};

             int arr1[] = {5,7,13,4,9,10};

            

             System.out.println("Before : "+Arrays.toString(arr));

             rearrange(arr);

             System.out.println("After rearrange : "+Arrays.toString(arr));

             System.out.println("Before : "+Arrays.toString(arr1));

             rearrange(arr1);

             System.out.println("After rearrange : "+Arrays.toString(arr1));

               }

}

//end of program

Output:


Related Solutions

given an array A = {a1, a2, ... , an} find the number of the continuous...
given an array A = {a1, a2, ... , an} find the number of the continuous subarrays of gcd one continuous subarrays of gcd one means gcd(ai, ai+1, ... , aj) = 1 for example {2, 4, 6, 3} or {1} are continuous subarrays of gcd one
1. Define the elements of the following equation: P = a0 ‒ a1 × Qd. 2....
1. Define the elements of the following equation: P = a0 ‒ a1 × Qd. 2. Given P = $150 ‒ 0.005 × Qd as the demand for a professional sports team: a. If P = $60, what is Qd? b. If P = $40, what is Qd? 3. Imagine these two possible changes from the demand curve listed in Question 2: a. P = $175 ‒ 0.005 × Qd b. P = $125 ‒ 0.005 × Qd For each,...
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following...
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following codes running: ori $a0, $0, 11 ori $a1, $0, 19 addi $a1, $a1, -7 slt $t2, $a1, $a0 beq $t2, $0, label addi $a2, $a1, 0 sub $a3, $a1,$a0 j end_1 label: ori $a2, $a0, 0 add $a3, $a1, $a0 end_1: xor $t2, $a1, $a0 *Values in $a0, $a1, $a2, $a3 after the above instructions are executed.
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
A four-bit binary number is represented as A3A2A1A0, where A3, A2, A1, and A0 represent the...
A four-bit binary number is represented as A3A2A1A0, where A3, A2, A1, and A0 represent the individual bits and A0 is equal to the LSB. Design a logic circuit that will produce a HIGH output whenever the binary number is greater than 0010 and less than 1000. how can I do this by using sum of product, not K map
When finding the minimum positive subsequence sum (mpss) for an array a = a0, a1, ....
When finding the minimum positive subsequence sum (mpss) for an array a = a0, a1, . . . , an−1 of integers, one can use the following divide-and-conquer algorithm. Divide the array into two equal subarrays aleft and aright, and recursively compute the mpss of both subarrays. Then compute the mpss, call if mpssmid, of any subsequence that crosses the boundary between aleft and aright. Finally, take the minimum positive value of the three answers. Answer the following questions pertaining...
We say that an infinite sequence a0,a1,a2,a3,… of real numbers has the limit L if for...
We say that an infinite sequence a0,a1,a2,a3,… of real numbers has the limit L if for every strictly positive number ε, there is a natural number n such that all the elements an,an+1,an+2,… are within distance ε of the value L. In this case, we write lim a = L. Express the condition that lim a = L as a formula of predicate logic. Your formula may use typical mathematical functions like + and absolute value and mathematical relations like...
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Solve the following recurrence relation (A) an = 3an-1 + 4an-2 a0 =1, a1 = 1
  Solve the following recurrence relation   (A) an = 3an-1 + 4an-2 a0 =1, a1 = 1   (B) an = 2an-1 - an-2, a0 = 1, a2= 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT