+ 2
Plz help me in this code to rotate aray by specified number ,not getting expected output
public static void main(String[] args){ System.out.print("Enter size of array : "); Scanner input = new Scanner(System.in); int n=input.nextInt(); System.out.println("Enter elemnt of array : "); int arr[] = new int[n+1]; for(int i=0;i<n;i++){ arr[i]=input.nextInt(); } int k=input.nextInt(); int temp=arr[0]; for(int j=1;j<k;j++){ arr[j-1]=arr[j]; arr[n-1]=temp; } for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
1 Resposta
+ 1
If I got it correctly you want to do something like this: https://code.sololearn.com/cogSyu0oBWpw
I've added a bunch of comments but feel free to ask if you have any further question.