Java Question: PROBLEM 2: Do the same translation for this in-place reverse func
ID: 3882253 • Letter: J
Question
Java Question:
PROBLEM 2: Do the same translation for this in-place reverse function in-place means: you may not create an extra array * * * * * * * * * * * * You should write a helper method. You may not use any "fields" to solve this problem (a field is a variable that is declared "outside" of the function declaration --- either before or after). * You may not use any other methods Your helper function must be parameterized to allow a smaller problem to * be specified. How do you reverse an array of size N? * (the answer is NOT: reverse an array of size N-1 !) */ public static void reverseIterative (int[] a) { int hi = a.length - 1; int lo = 0; while (loExplanation / Answer
import java.util.Scanner;
public class RevArray
{
int sum = 0;
public static void main(String[] args)
{
int[] arr = new int[]{1,2,3,4,5};
reverseRecursion(arr);
for(int i=0;i<5;i++)
System.out.println(arr[i]+" ");
}
public static void reverseRecursion(int[] x){
reverse(x, 0, x.length-1);
}
static void reverse(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverse(arr, start+1, end-1);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.