Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 189_Rotate_Array.java #291

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 189_Rotate_Array.java
Signed-off-by: Kriti Misra <90602521+kriti142003@users.noreply.github.com>
  • Loading branch information
kriti142003 committed Oct 21, 2023
commit c379d07c6f79533bcf93060fa33192583f937c3a
26 changes: 26 additions & 0 deletions Two Pointers/189_Rotate_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public void rotate(int[] nums, int k) {
if(nums == null || nums.length <= 1 || k % nums.length == 0) {
for(int i=0;i<nums.length;i++) {
System.out.println(nums[i]);
}
}
int n=nums.length;
k = k % n;
swap(nums,0,n-1);
swap(nums,0,k-1);
swap(nums,k,n-1);
for(int i=0;i<n;i++) {
System.out.println(nums[i]);
}
}
static void swap(int nums[],int s,int e) {
while(s<e) {
int temp=nums[s];
nums[s]=nums[e];
nums[e]=temp;
s++;
e--;
}
}
}