We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.concurrent.*;importjava.util.function.*;importjava.util.regex.*;importjava.util.stream.*;importstaticjava.util.stream.Collectors.joining;importstaticjava.util.stream.Collectors.toList;classResult{/* * Complete the 'rotateLeft' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts following parameters: * 1. INTEGER d * 2. INTEGER_ARRAY arr */publicstaticList<Integer>rotateLeft(intd,List<Integer>arr){// Write your code hereif(d==arr.size())returnarr;Integer[]newArr=newInteger[arr.size()];intshift=arr.size()-d;for(inti=0;i<arr.size();i++){intnewIndex=(i+shift)%arr.size();newArr[newIndex]=arr.get(i);}returnArrays.asList(newArr);}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));String[]firstMultipleInput=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");intn=Integer.parseInt(firstMultipleInput[0]);intd=Integer.parseInt(firstMultipleInput[1]);List<Integer>arr=Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split(" ")).map(Integer::parseInt).collect(toList());List<Integer>result=Result.rotateLeft(d,arr);bufferedWriter.write(result.stream().map(Object::toString).collect(joining(" "))+"\n");bufferedReader.close();bufferedWriter.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Left Rotation
You are viewing a single comment's thread. Return to all comments →
Solution in Java