Write a method which accepts an array of integers, A, and an integer, k, as a pa
ID: 3529465 • Letter: W
Question
Write a method which accepts an array of integers, A, and an integer, k, as a parameter. The method should perform a circular shift of the elements of A to the left by k positions. For example, if A = f 65, 34, 54, 78, 41, 43, 67, 89 g and k = 3, then the method should change A to f 78, 41, 43, 67, 89, 65, 34, 54 g. Your method should not return anything, just change the array passed to it. Very important: It is not enough if your code just works. Your goal should be to minimize both the amount of memory consumed by your program and the amount of computational processing involved.Explanation / Answer
import java.util.Scanner;
public class Left{
public static void circularLeft(int arr[], int k){
k%=arr.length;
int i,j;
int[] temp=new int[k];
for( i=0;i<temp.length;i++)
temp[i]=arr[i];
for( j=0;j<arr.length-k;j++)
arr[j]=arr[j+k];
for(i=0;i<k;i++)
arr[j++]=temp[i];
return ;
}
public static void main(String[] args) {
int [] A=new int[]{65, 34, 54, 78, 41, 43, 67, 89};
circularLeft(A,3);
printArr(A);
}
public static void printArr(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
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.