Algorithm
Problem Name: Data Structures -
In this HackerRank in Data Structures -
A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result.
Example
d = 2
arr = [1,2,3,4,5]
After 2 rotations, arr' = [3,4,5,1,2]
Function Description
Complete the rotateLeft function in the editor below.
rotateLeft has the following parameters:
- int d: the amount to rotate by
- int arr[n]: the array to rotate
Returns
- int[n]: the rotated array
Input Format
The first line contains two space-separated integers that denote n, the number of integers, and d, the number of left rotations to perform.
The second line contains n space-separated integers that describe arr[].
Constraints
- 1 <= n <= 10**5
- 1 <= d <= n
- 1 <= a[i] <= 10**6
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
Explanation
To perform d = 4 left rotations, the array undergoes the following sequence of changes:
[1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] -> [4,5,1,2,3] -> [5,1,2,3,4]
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
int d;
scanf("%d",&d);
int *arr = malloc(sizeof(int) * n);
for(int arr_i = 0; arr_i < n; arr_i++){
scanf("%d",&arr[arr_i]);
}
int *arr2 = malloc(sizeof(int) * n);
int curr=0;
for(int i=d; i < n; i++) {
arr2[curr] = arr[i];
curr++;
}
for(int i=0; i < d; i++) {
arr2[curr] = arr[i];
curr++;
}
for(int i=0; i < n; i++) {
printf("%d ",arr2[i]);
}
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
C++ Programming
#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;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scanner = null;
int[] array = null;
int rotations = 0;
try {
scanner = new Scanner(System.in);
String firstLineStr = scanner.nextLine();
String[] firstLineSplit = firstLineStr.split(" ");
int arrayLength = Integer.parseInt(firstLineSplit[0]);
array = new int[arrayLength];
rotations = Integer.parseInt(firstLineSplit[1]);
String secondLineStr = scanner.nextLine();
String[] secondLineSplit = secondLineStr.split(" ");
for(int i = 0; i < secondLineSplit.length; i++) {
array[i] = Integer.parseInt(secondLineSplit[i]);
}
} finally {
scanner.close();
}
for(int i = 0; i < array.length; i++) {
int index = (i + rotations) % array.length;
System.out.print(array[index] + " ");
}
}
}
Copy The Code &
Try With Live Editor
#7 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int i, j, z;
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
int[] temparray = new int[2*n];
//constraints
if(n >= 100000 || n < 1 )
{
System.Environment.Exit(1>;
}
if(k > n || n < 1 )
{
System.Environment.Exit(1);
}
for(i = 0; i < n; i++>
{
if(a[i] > 1000000 || a[i] < 1 )
{
System.Environment.Exit(1);
}
}
for(j = 0; j < n; j++)
{
z = (j - k) %n;
if(z != 0)
{
z= (n + z) %n;
}
temparray[z] = a[j];
}
//view array
for(i = 0; i < n; i++)
{
Console.Write(temparray[i] + " " >;
}
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Javascript Programming
Code -
Javascript Programming
function processData(input) {
//Enter your code here
let lines = input.split('\n');
let arr = lines[1].split(' ').map(Number);
let l = lines[0].split(' ');
let n = l[0];
let d = l[1];
let len = arr.length;
if (d === len) {
console.log(copy.join(' '));
} else {
if (d > len) d = d % len;
let left = arr.slice(0, d);
let right = arr.slice(d);
let result = [...right, ...left].join(' ');
console.log(result);
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Copy The Code &
Try With Live Editor
#5 Code Example with Python Programming
Code -
Python Programming
n,d = (int(x) for x in input().split())
l = [int(x) for x in input().split()]
length = len(l)
new = [0 for x in l]
for i in range(length):
o = i-d
new[o] = str(l[i])
print(" ".join(new))
Copy The Code &
Try With Live Editor
#6 Code Example with PHP Programming
Code -
PHP Programming
1000000 || $k<0|| $k>1000000 || count($a) <=0)
exit;
for($i = 0; $i < $k%$n; $i++){
//for($i = 0; $i < $k; $i++){
for($j = 0; $j < $n-1; $j++){
$temp = $a[$j];
$a[$j] = $a[$j + 1];
$a[$j + 1] = $temp;
}
}
//array_walk($a,'intval');
//print implode(" ", $a);
/*foreach($a as $key=>$value){
printf("%d", $value);
if($key<$n - 1)
printf(" ");
}*/
for($i = 0; $i < $n; $i++){
printf("%d", $a[$i]);
if($i < $n - 1)
printf(" ">;
}
?>
Copy The Code &
Try With Live Editor