Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.programs.common.ClearCorrect;
- /**
- * Created by Sireesha on 8/6/19.
- */
- public class RotateArray {
- public static void main(String args[]) {
- RotateArray rotateArray = new RotateArray();
- int[] inputArr = {-1,-100,3,99};
- int noOfRotations = 2;
- int[] resultArr = rotateArray.rotate(inputArr, noOfRotations);
- for(int i = 0; i < resultArr.length; i++) {
- System.out.print(resultArr[i] +", ");
- }
- }
- public int[] rotate(int[] inputArr, int noOfRotations) {
- System.out.println("inputArr "+inputArr);
- int[] resultArr = new int[inputArr.length];
- if(noOfRotations == inputArr.length - 1) {
- resultArr = inputArr;
- } else {
- int k = 0;
- while(k < noOfRotations) {
- for(int i = 0; i <= inputArr.length - 1; i++) {
- if(i == (inputArr.length - 1)) {
- resultArr[0] = inputArr[inputArr.length - 1];
- } else {
- resultArr[i + 1] = inputArr[i];
- }
- }
- for(int i = 0; i < inputArr.length; i++) {
- inputArr[i] = resultArr[i];
- }
- k++;
- }
- }
- return resultArr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement