Search
 
SCRIPT & CODE EXAMPLE
 

CPP

rotate an array of n elements to the right by k steps

public void rotate(int[] nums, int 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--;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ average vector 
Cpp :: reverse an array in c++ stl 
Cpp :: explicit c++ 
Cpp :: count sort algorithm 
Cpp :: enum c++ 
Cpp :: length of a string c++ 
Cpp :: heap buffer overflow in c 
Cpp :: string reverse iterator c++ 
Cpp :: c++ vector 
Cpp :: pass map as reference c++ 
Cpp :: print in c ++ 
Cpp :: cyclic array rotation in cpp 
Cpp :: one away coding question 
Cpp :: C++ program for Celsius to Fahrenheit and Fahrenheit to Celsius conversion using class 
Cpp :: constrain function in arduino 
Cpp :: z transfrom mathlab 
Cpp :: cmd color text c++ 
Cpp :: iterate const vector 
Cpp :: c++ add input in 
Cpp :: c++ stl vector get iterator from index 
Cpp :: max heap insertion c++ 
Cpp :: c++ multiline string 
Cpp :: compare values within within a vector c++ 
Cpp :: free a pointer c++ 
Cpp :: c++ copy constructor 
Cpp :: << in C++ 
Cpp :: pause the console c++ 
Cpp :: how to show constellations in starry night orion special edition 
Cpp :: recuva recovery software for pc with crack 
Cpp :: varint index 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =