Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. package com.programs.common.ClearCorrect;
  2.  
  3. /**
  4. * Created by Sireesha on 8/6/19.
  5. */
  6. public class RotateArray {
  7.  
  8. public static void main(String args[]) {
  9.  
  10. RotateArray rotateArray = new RotateArray();
  11.  
  12. int[] inputArr = {-1,-100,3,99};
  13. int noOfRotations = 2;
  14.  
  15. int[] resultArr = rotateArray.rotate(inputArr, noOfRotations);
  16. for(int i = 0; i < resultArr.length; i++) {
  17.  
  18. System.out.print(resultArr[i] +", ");
  19. }
  20.  
  21.  
  22. }
  23. public int[] rotate(int[] inputArr, int noOfRotations) {
  24.  
  25. System.out.println("inputArr "+inputArr);
  26. int[] resultArr = new int[inputArr.length];
  27.  
  28. if(noOfRotations == inputArr.length - 1) {
  29.  
  30. resultArr = inputArr;
  31. } else {
  32.  
  33. int k = 0;
  34. while(k < noOfRotations) {
  35.  
  36. for(int i = 0; i <= inputArr.length - 1; i++) {
  37.  
  38. if(i == (inputArr.length - 1)) {
  39.  
  40. resultArr[0] = inputArr[inputArr.length - 1];
  41.  
  42. } else {
  43.  
  44. resultArr[i + 1] = inputArr[i];
  45. }
  46. }
  47. for(int i = 0; i < inputArr.length; i++) {
  48. inputArr[i] = resultArr[i];
  49. }
  50. k++;
  51. }
  52.  
  53. }
  54. return resultArr;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement