To make sure your calendar, event reminders, and other features are always
correct, please tell us your time zone (and other details) using the
drop-down menus below:
Set Date/Time format:
In 12 Hour format the hours will be displayed as 1 through 12 with “a.m.” and “p.m.”
displayed after the time (ex. 1:00p.m.). In 24 hour format the hours will be displayed as 00 through 23 (ex. 13:00).
You can always change your time zone by going to your Account Settings.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Visiting Rany Milton(username: itsrandy)
Tag
Please wait...
Select a Color
Manage Applications
Check the items that you want displayed. Uncheck all to hide the section.
Calendars
Files
Addresses
To Dos
Discussions
Photos
Bookmarks
The “Switch Navigator” button will no longer be available after February 14, 2017.
Please learn more about how to use the new Navigator by clicking this link.
01.DS.02.04. Rotate an Array by N Elements.html -- 1.9 meg
Rotate an Array by N Elements - Coderust: Hacking the Coding Interview
Normalize the rotations, so they do not exceed the length of the array. For example, for an array of length 101010, rotating by 141414 elements is the same as rotating by (14%10) 4 elements.
Convert negative rotations to positive rotations.
Reverse the elements of the original array.
Reverse the elements from 000 to n−1n-1n−1.
Reverse the elements from nnn to length−1length-1length−1.
Let’s run this on the array below for n=2n=2n=2:
Created with Fabric.js 3.6.6Let's run an example on the above array that requires n = 2 rotations.012345678911020059863211940
1 of 4
Created with Fabric.js 3.6.6Original array with all elements reversed.012345678940911328659020101
1 of 4
Created with Fabric.js 3.6.6Original array with all elements from 0 - (n-1) reversed.012345678994011328659020101
1 of 4
Created with Fabric.js 3.6.6Original array with all elements from n - (length-1) reversed. 012345678994011020059863211
Normalize the rotations, so they do not exceed the length of the array.
Convert negative rotations to positive rotations.
Store the last nnn elements into a temporary array.
Shift the original array towards the right by l−nl-nl−n places. Here, lll is the length of the array.
Copy the temporary array at the beginning of the original array.
Let’s run this on the array below for n=−3n=-3n=−3.
Created with Fabric.js 3.6.6012345678911020059863211940Let's run an example on the above array that requires n = -3 rotations.Input array
1 of 5
Created with Fabric.js 3.6.6012345678911020059863211940The number of positive number of shifts required, i.e R = (length - |n|) = (10 - |-3|) = (10 - 3) = 7 rotationsInput array
1 of 5
Created with Fabric.js 3.6.60123456789110200598632119400123456059863211940Temporary array with n = 7 elements copiedInput arrayTemporary array
1 of 5
Created with Fabric.js 3.6.60123456789110200598632110200123456059863211940Original array with (length - n = 3) elements shiftedInput arrayTemporary array
1 of 5
Created with Fabric.js 3.6.60123456789059863211940110200123456059863211940Temporary arrayInput array, rotated by -3Original array with temporary array copied in the beginning
1 of 5
C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<vector>
usingnamespace std;
void RotateArray(vector<int>& nums,int n){
int len = nums.size();
// Let's normalize rotations
n = n % len;
if(n <0){
n = n + len;
}
vector<int> temp(n);
// copy last 'n' elements of array into temp
for(int i =0; i < n; i++){
temp[i]= nums[len - n + i];
}
// shift original array
for(int i = len -1; i >= n; i--){
nums[i]= nums[i - n];
}
// copy temp into original array
for(int i =0; i < n; i++){
nums[i]= temp[i];
}
}
#
#include <iostream>
#include <vector>
using namespace std;
void RotateArray(vector<int>& nums, int n) {
int len = nums.size();
// Let's normalize rotations
n = n % len;
Attach this document to an event, task, or address
You can attach a link to this document to an event in your Calendar, a task in your To Do list or an Address. Check the boxes below for the data you want to
bring into the event’s or task’s description, and then click “Select text to copy” to have the next event or task you create or edit have the document text and link.