GoConqr has a new look! Let us know what you think.

Flashcards

Given an array of integers, rotate the array by 'N' elements.

Suhas S English 1 card

Cards in this deck (1)

  1. 1Given an array of integers, rotate the array by 'N' elements.class Solution {
    public void rotate(int[] nums, int k) {
    k = k%nums.length;
    reverse(nums, 0 , nums.length -1);
    reverse(nums, 0 , k - 1);
    reverse(nums, k , nums.length -1 );
    }
    public void reverse(int[] nums,int start,int end){
    while( start < end){
    int temp = nums[start];
    nums[start] = nums[end];
    nums[end] = temp;
    start++;
    end--;
    }
    }
    }

Information

Description
Flashcards on Given an array of integers, rotate the array by 'N' elements., created by Suhas S on 12/02/2018.
Created by
Suhas S
Views
8
Tags
array
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?