ALLAN AVELARTechnology Solutions
🍰Good Afternoon,<username class="jsx-1036735364">Guest</username>!Welcome to my website!Progressive Web App (PWA)Install in your deviceChromecast Controlsstatus: not playing or showingVolumeDown50%VolumeUpPlayPausestoppedStopResetFullscreenExpandRestricted accessLogin / RegisterEnter with your credentialsRestrict Access ManagerRequest your Access CodeAccount DetailsYour personal and access dataProjects / ServicesYour contract historyProducts / DownloadsYour purchase historyChromecast AppsYour streaming contractsCredits / PurchasesYour balance and credit historyAccess ManagerYour restricted content accessNotificationsNotifications enabled for youDisconnectDisconnect your userAvailable contentHomeAbout Me6Personal ResumeA little bit of how it all startedProfessional ResumeA little about my careerWork MethodologyHow I make it happenPersonal MemoriesRestricted ContentCurriculum VitaeRestricted ContentTrips & PhotosRestricted ContentAreas of Expertise10All my servicesSummary of all areasCTO as a ServiceGet a part-time basis C-LevelCorporate ArchitectureAgility and technology as a philosophySoftware & TechnologyComing soonFrontend DevelopmentComing soonBackend DevelopmentComing soonStrategy & ConsultingComing soonPlatform & DevSecOpsComing soonCustomer ExperienceComing soonAgile CultureComing soonInnovation Hub6Innovation CultureAdvantages and how to implementDistributed LT & BlockchainComing soonData Science & AnalyticsComing soonCybersecurity PlatformComing soonIntelligent AutomationComing soonAI & Machine LearningComing soonCourses & Training9Distributed LT & BlockchainComing soonCybersecurity PlatformComing soonIntelligent AutomationComing soonFrontend DevelopmentComing soonBackend DevelopmentComing soonPlatform & DevSecOpsComing soonAI & Machine LearningComing soonProgressive Web AppsComing soonAgile CultureComing soonContact MeDEVELOPED PROJECTSAll my projectsSummary of all areasFinancial Institutions3Santander Totta BankRestricted ContentSantander Brazil BankRestricted ContentSafra Bank S.ARestricted ContentWeb3 / Blockchain15Bitcoin (BTC)Restricted ContentEthereum VM (ETH)Restricted ContentRipple (XRP)Restricted ContentHyperledger.orgRestricted ContentInternet ComputerRestricted ContentStarknet (Cairo)Restricted ContentPolkadot (DOT)Restricted ContentDeFi DashboardsRestricted ContentToken MarketplacesRestricted ContentCrypto CheckoutsRestricted ContentCold / Hot WalletsRestricted ContentWallet ConnectorsRestricted ContentNon Fungible Tokens (NFT)Restricted ContentDescentralized Identities (DID)Restricted ContentDescentralized Organizations (DAO)Restricted ContentOld Projects29TIM | Interactive Voice ResponseAdobe Flex, Actionscript 3, Java, SpringACS | Follow EnergyAngularjs, HTML5, CSS3, C#, REST APIACS | Gateway CE50Angularjs, HTML5, CSS3, C, C#, LUASKY | Selinho de NatalActionscript 3, .NET, REST API, JSONIntel | Futuro de PresenteActionscript 3, .NET, REST API, JSONNestlé | Baú de DiversõesAS 3, .NET, REST, Facebook OpengraphMarlboro | Red RacingAdobe Flash, Actionscript 3, JSONHP | Cartucho PirataAdobe Flash, Actionscript 3, REST APIHP | Youtube StoreAdobe Flash, AS3, YouTube Data APICasas Bahia | BahianinhoActionscript 3, .NET, REST API, JSONAdria | TortinhasAdobe Flash, Actionscript 3, PHPUniverso Online | UOL FinanceActionscript 3, REST API, JSONUniverso Online | UOL MaisActionscript 3, REST API, JSONChevrolet | CaptivaActionscript 3, PHP, MYSQLGafisa | Edifício ColaborativoAdobe Flash, Actionscript 3Pão de Açucar | Sommeliers ClubAdobe Flash, Actionscript 3Pão de Açucar | Desafio ColeçõesAdobe Flash, Actionscript 3Antarctica | Circuito OriginalMacromedia Flash, Actionscript 2, XMLNestlé | Nescau 2.0Macromedia Flash, Actionscript 2, XMLSchin RefrigerantesActionscript 2, FMS 2, PHP, MYSQLMercedes-Benz | SLK 2005Actionscript 2, XMLMercedes-Benz | Classe CActionscript 2, XMLMercedes-Benz | Classe AActionscript 2, XMLMercedes Benz | SprinterActionscript 2, XMLMercedes Benz | Motores EletrônicosActionscript 2, XMLBanda NX ZeroMacromedia Flash, Actionscript 2Lew´lara TBWAActionscript 2, PHP, MYSQLMaison CriolaActionscript 2, PHP, MYSQLBondage MusicActionscript 2, PHP, MYSQLBlog PostsHackerRank5Sock MerchantWarm-Up ChallengesCounting ValleysWarm-Up ChallengesRepeated StringWarm-Up ChallengesJumping On CloudsWarm-Up ChallengesLeft RotationWarm-Up ChallengesLegal TermsTerms and ConditionsPrivacy PolicyDisclaimer
Username / PasswordBlockchainForgot your password?LoginDon't have an account? Look at a demonstration!
🚀 April 23, 2018 ♻ May 19, 2018reading 6 min reading

Warm-up Challenges ⚡ Left Rotation

I'm posting about my solutions to the HackerRank challenges as a way to improve my learning, reinforce my knowledge, and establish an understanding of the concepts covered. If I help someone by sharing along the way, even better!

Left Rotation is one of the initial challenges with 'EASY' difficulty.


What's the challenge?

A left rotation operation on an array shifts each array element to the left. For example, if two left rotations are performed on the array [1, 2, 3, 4, 5], the array becomes [3, 4, 5, 1, 2].

Given an array a of n integers and a number, d, of left rotations in the array. Return the updated array to be printed as a single line of integers separated by spaces.

What do we have to do?

A function that returns an array of integers after the left rotations have been performed.

Parameters
  • a = an array of integers
  • d = number of rotations
Limits
  • 1 <= n <= 10 ^ 5
  • 1 <= d <= n
  • 1 <= a [i] <= 10 ^ 6

Sample Input

1 2 3 4 5
4

Sample Output

5 1 2 3 4

Solution

function leftRotation(a, d) {
	return a.concat(a.splice(0, d));
}

Logic

All the logic is contained in this single line, but we can divide it into three parts:

I first used .splice() to get the first d items in the a[0 - d] array. At the beginning of the challenge, the following limit is informed that defines this logic 1 <= d <= n, that is, I don't need to worry about rotating the matrix to the left more times than the items in the array.

I then used .concat() to concatenate (or merge) what's left of a, or everything after a[d], with what I separated in the first step a[0 - d].

And then I finish the function by returning this newly sorted array. 🖖


🕵Extra Solutions

Other languages that I speak


💎 Ruby

def leftRotation(a, d)
	a.rotate(d)
end

🐍 PYTHON 2 & 3

def leftRotation(a, d):
	return a[a:] + a[:a]

golang GOLANG

func leftRotation(a[] int32, d int32) []int32 {
	return append(a[d:], a[:d]...)
}

scala SCALA

def leftRotation(a: Array[Int], d: Int): Array[Int] = {
	return a.drop(d)++a.take(d);
}

javaJAVA 7 & 8

static int leftRotation(int[] a, int d) {
	int[] rotated = new int[a.length];
	for (int i = 0; i <  a.length; i++) {
				rotated[(i + a.length - d) % a.length] = a[i];
	}
	return rotated;
}

php PHP

function leftRotation($a, $d) {
	return array_merge(array_splice($a, $d, count($a)), array_splice($a, 0, $d));
}

Thought: We can judge the heart of a man by his treatment of animals.
Emmanuel Kant

💡 Latest Insights:

I don't claim that these answers are the best or most efficient, they are simply creations of my mind to solve the problem at that time. Probably today I would do different...
About MeProfessional ResumeWork MethodologyPersonal MemoriesCurriculum VitaePhotos & Travel
AREAS OF EXPERTISECTO as a ServiceCorporate ArchitectureSoftware & TechnologyFrontend DevelopmentBackend DevelopmentStrategy & ConsultingPlatform & DevSecOpsCustomer ExperienceAgile Culture
Innovation HubDistributed LT & BlockchainData Science & AnalyticsCybersecurity PlatformIntelligent AutomationAI & Machine Learning
COURSES & TRAININGBlockchain & Distributed LTCybersecurity PlatformIntelligent AutomationFrontend DevelopmentBackend DevelopmentPlatform & DevSecOpsAI & Machine LearningProgressive Web AppsAgile Culture
Hackathons & Prizes
DEVELOPED PROJECTSWEB3 / BLOCKCHAIN 2017 ~ 2024BitcoinEthereumRippleStarknetDIDInternet ComputerMarketplacesNFTHyperledgerWalletsDAODeFi DashboardsCrypto Checkoutsecurity tokensBANKS 2016 ~ 2021Santander TottaSafra S.A.Santander BrazilOld Projects 2000 ~ 2016SKYIntelACSNinho SoleilHP YouStoreHP Cartucho PirataMarlboro RacingMercedes BenzTortinhas AdriaChevrolet CaptivaGPA Sommeliers ClubUOL FinanceGafisaSprinterCasas BahiaJVCLew´laraPão de AçucarSchin RefrigerantesNescau 2.0PapaizTV CulturaSpacekidsBondage MusicMaison CriolaAntarctica OriginalNX ZeroSLK 2005Classe AClasse CPetrobrasBraskemNíveaJohnson & JohnsonAbyaraTIM (URA)NestléUOL
Blog PostsHACKERRANK 2017 ~ 2018#sockMerchant#countingValleys#repeatedstring#jumpingOnClouds#leftRotation
Contact Me
Terms and ConditionsPrivacy PolicyDisclaimer
This PWA was built with ❤ in my spare time 🤸‍♂️ through of the years...