Cerinta completa

A operation on a circular array shifts each of the array’s elements unit to the left. The elements that fall off the left end reappear at the right end. Given an integer , rotate the array that many steps to the left and return the result.

Example

After rotations, .

Function Description

Complete the function with the following parameters:

  • : the amount to rotate by
  • : the array to rotate

Returns

  • : the rotated array

Input Format

The first line contains two space-separated integers that denote , the number of integers, and , the number of left rotations to perform.
The second line contains space-separated integers that describe .

Constraints

Sample Input

STDIN      Function
-----      --------
5 4         n = 5 d = 4
1 2 3 4 5  arr = [1, 2, 3, 4, 5]

Sample Output

5 1 2 3 4

Explanation

To perform left rotations, the array undergoes the following sequence of changes:


Limbajul de programare folosit: cpp14

Cod:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int N, d; cin >> N >> d;
    vector<int> v(N);
    for (size_t i = 0; i < v.size(); ++i) {
        cin >> v[i];
    }
    d = d % N;
    for (int i = d; i < N; ++i)
        cout << v[i] << ' ';
    for (int i = 0; i < d; ++i)
        cout << v[i] << ' ';
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464651318

Link challenge: https://www.hackerrank.com/challenges/array-left-rotation/problem

Left Rotation