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

Description

Flashcards on Given an array of integers, rotate the array by 'N' elements., created by Suhas S on 12/02/2018.
Suhas S
Flashcards by Suhas S, updated more than 1 year ago
Suhas S
Created by Suhas S over 6 years ago
0
0

Resource summary

Question Answer
Given 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--; } } }
Show full summary Hide full summary

Similar

PHP Exam One
tr.badhan
Array Programs
Subash M
Array
alim586
Array
Tate Johnson
Test Card
Nick Wu
Single sell stock
Suhas S
Remove Duplicates from Sorted Array
Suhas S
Speed, Distance, Time
Bradley Sansom
Musical Terms
Abby B
The Changing Natural Environment Part 2
R S