O(N) time complexity, O(1) extra space
In this , you have to make the flow diagram and debug it again and again to understand this better.\nSuppose,\n1 2 3 4 5 6 7 is the array and the number of rota
In this , you have to make the flow diagram and debug it again and again to understand this better.\nSuppose,\n1 2 3 4 5 6 7 is the array and the number of rotations is 3.\nWe have taken iterations as 0.\nAfter 1st iteration, \n4 2 3 1 5 6 7 and we have the index of 4 as well because we are always moving 3 steps ahead which our index variable is taking care of.\nAfter 2nd iteration,\n7 2 3 1 5 6 4\nAfter 3rd iteration,\n3 2 7 1 5 6 4\nAfter 4th iteration,\n6 2 7 1 5 3 4 \nAfter 5th iteration,\n2 6 7 1 5 3 4\nAfter 6th iteration,\n5 6 7 1 2 3 4 which is our answer and now as iterations == array.length so loop will not further move.\n\nSimilarly, for -1 , 100, 3, 99\nAfter 1st iteration,\n3,100,-1,99\nAfter 2nd iteration,\n3 100 -1 99   i==1\nAfter 3rd iteration,\n3 99 -1 100\nNow iterations == array.length so this will not further move. \n\n**Important**\nIt is very important that you do not move more than the length of the array as it will ruin the indices that you corrected while rotating.\n\n```\n public void rotate(int[] nums, int k) {\n//        Marks the correct index of a number\n        int index = 0;\n//        Marks the index of the array.\n        int i = 0;\n//        Marks the number of iterations in the array.\n        int iterations = 0;\n        while(iterations < nums.length){\n           if((index+k)% nums.length == i){\n               i++;\n               index = i;\n               iterations++;\n           }else{\n               swap(nums,i,(index+k)%nums.length);\n               index = (index+k)%nums.length;\n               iterations++;\n           }\n        }\n    }\n\n    void swap(int [] nums , int i , int j){\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n```\n\nIf you liked the solution or found it different , do upvote :)\nThank you
['Two Pointers', 'Binary Search', 'Sorting', 'Binary Tree', 'Java']