fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. void leftRotate(int arr[], int d, int n)
  11. {
  12. for (int i = 0; i < d; i++)
  13. leftRotatebyOne(arr, n);
  14. }
  15.  
  16. void leftRotatebyOne(int arr[], int n)
  17. {
  18. int i, temp;
  19. temp = arr[0];
  20. for (i = 0; i < n - 1; i++)
  21. {
  22. arr[i] = arr[i + 1];}
  23.  
  24. arr[n-1] = temp;
  25.  
  26. }
  27.  
  28.  
  29. void printArray(int arr[], int n)
  30. {
  31. for (int i = 0; i < n; i++)
  32. System.out.print(arr[i] + " ");
  33. }
  34.  
  35. // Driver program to test above functions
  36. public static void main (String[] args) throws java.lang.Exception
  37.  
  38. {
  39. Ideone rotate = new Ideone();
  40. int arr[] = { 1, 2, 3, 4};
  41. rotate.leftRotate(arr, 2, 4);
  42. rotate.printArray(arr, 4);
  43. }
  44. }
  45.  
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
3 4 1 2