We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Arrays
- Left Rotation
- Discussions
Left Rotation
Left Rotation
Sort by
recency
|
3546 Discussions
|
Please Login in order to post a comment
Here's a solution in C
My solution. What do you think?
i had a problem with the variable idx, the problem says about calculate two idx, the first in the query one (idx = (x ^ last_answer)) and the second in query two (idx = (x ^ last_answer) % n) i solve the problem with variable idx = (x ^ last_answer) % n in the up of my for cycle and delete other idx code:
def dynamicArray(n, queries): array_2D = [] last_answer = 0 results = [] for i in range(len(queries)): array_2D.append([]) for types,x,y in queries: idx = (x ^ last_answer) % n if types == 1: #idx = (x ^ last_answer) array_2D[idx].append(y) else: #idx = (x ^ last_answer) % n last_answer = array_2D[idx][y % len(array_2D[idx])] results.append(last_answer) return results
Concise O(n) Python solution using slicing
vector rotateLeft(int d, vector arr) { int n = arr.size(); if(d>n){ d = d%n; } reverse(arr.begin(),arr.begin()+d); reverse(arr.begin()+d,arr.end()); reverse(arr.begin(),arr.end()); return arr; }