Skip to content

Instantly share code, notes, and snippets.

@tylersloeper
Last active April 15, 2020 17:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylersloeper/9577d23196d0544e419253017acc72fb to your computer and use it in GitHub Desktop.
Save tylersloeper/9577d23196d0544e419253017acc72fb to your computer and use it in GitHub Desktop.
Hacker Rank: Arrays: Left Rotation, (in c, c#, php, and javascript)
/**
The challenge:
https://www.hackerrank.com/challenges/ctci-array-left-rotation
**/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n;
int k;
int shift;
int temp;
int temp1;
int a_i;
int i;
scanf("%d %d",&n,&k);
int *a = malloc(sizeof(int) * n);
//get
for(a_i = 0; a_i < n; a_i++)
{
scanf("%d",&a[a_i]);
}
if(n < 1 || n > 100000)
{
return 1;
}
if(k < 1 || k > n)
{
return 1;
}
//move
shift = k % n;
//printf("shift:%d *", shift);
for(i = 0; i<k; i++)
{
temp = a[0];
for(a_i = 0; a_i > -(n+1); a_i--)
{
temp1 = a[abs(n+a_i) %n];
a[abs(n+a_i) %n] = temp;
temp = temp1;
}
}
//print
for(a_i = 0; a_i < n; a_i++)
{
printf("%d ", a[a_i]);
}
return 0;
}
/**
The challenge:
https://www.hackerrank.com/challenges/ctci-array-left-rotation
**/
/**
The challenge:
https://www.hackerrank.com/challenges/ctci-array-left-rotation
**/
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] + " " );
}
}
}
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var k = parseInt(n_temp[1]);
a = readLine().split(' ');
a = a.map(Number);
//custom variables
var i;
var j;
var temp1;
var holdeverything;
//constraints
if(n < 1 || k < 1 || n > 100000 || k > n)
{
return;
}
for(i=1; i<n;i++)
{
if(a[i] > 1000000)
{
return;
}
}
//reassignment of values in the array according to specifications.
for(j=0; j<k;j++)
{
for(i=0; i<n-1;i++)
{
temp1 = a[i];
a[i] = a[i+1];
a[i+1] = temp1;
}
}
//console.log is the only way to printf in javascript, and the function unfortunately automatically includes a new line in every call. This means to have an output of an unknown number of unknown elements, some variable (in this case "holdeverything") is needed to hold all the data so that it can all be print in a single call of console.log
holdeverything = a[0];
for(i=1; i<n;i++)
{
holdeverything += " " + a[i];
}
console.log(holdeverything);
}
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d %d",$n,$k);
$a_temp = fgets($handle);
$a = explode(" ",$a_temp);
array_walk($a,'intval');
$i;
$j;
$temp;
if($n<1 || $n>100000 || $k<1|| $k>1000000 )
{
exit;
}
for($j=0;$j<$k;$j++)
{
for($i=0;$i<$n-1;$i++)
{
$temp = $a[$i];
$a[$i] = $a[$i +1];
$a[$i+1] = $temp;
}
}
for($i=0;$i<$n;$i++)
{
printf("%d", $a[$i]);
if($i<$n-1)
{
printf(" ");
}
}
?>
@tylersloeper
Copy link
Author

screenshot 2016-12-08 16 21 05

@dangreenisrael
Copy link

So, I came across this after discovering that this

const rotateLeft = i =>{ 
  const first = a.shift()
  a.push(first)
  if (i<k) rotateLeft(i+1);
}
rotateLeft(1);
console.log(a.join(" "));

Failed the last 2 tests.

Is there a way to solve this problem using a recursive function?

@MagedAhmad
Copy link

MagedAhmad commented Apr 15, 2020

My solution for this challenge was

function rotLeft($a, $d) {
    $temp = [];
    for($i = 0; $i < $d; $i++) {
        $temp[$i] = $a[$i];
    }
    $a = array_slice($a, $d);
    $result = array_merge($a, $temp);
    return $result;
}

and passed all tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment