Sort by

recency

|

3518 Discussions

|

  • + 0 comments

    Here is my Java solution:

        public static List<Integer> rotateLeft(int d, List<Integer> arr) {
        // Write your code here
            List<Integer> subList = arr.subList(d, arr.size());
            subList.addAll(arr.subList(0, d));
            return subList;
        }
    
  • + 0 comments

    My solution was pretty simple, after trying looping over the array without success, I tried the most simplistic approach, dividing the array and then putting it all together, as the order doesn't change I just needed to extract the portion of items where d would be the "zero" index and append to it the rest of the array. One line needed, python 3, O(n) complexity

    def rotateLeft(d, arr):
     return arr[d:]+arr[:d]
    
  • + 0 comments

    Next Java solution consider avoiding extra rotations.

    public static List<Integer> rotateLeft(int d, List<Integer> arr) {
        // Write your code here
            if(arr.size() == d) return arr;
            //Calculate right rotation and 
            //avoid extra rotations 
            //i.e. d = 16, arr.size() = 5 It's necessary just 1 movement to the right
            int rotation = Math.abs(arr.size()-d)%arr.size();
            System.out.println("rotation:"+rotation);
            //execute rotation
            Integer[] rotatedArr = new Integer[arr.size()];
            int index = 0;
            for(Integer val : arr){
                int newIndex = (index+rotation)%(arr.size());
                System.out.println("newIndex: "+newIndex);
                rotatedArr[newIndex] = val;
                index++;
            }
            
            return Arrays.asList(rotatedArr);
        }
    
  • + 0 comments

    Here is my c++ solution, you can have the video explanation here : https://youtu.be/lyUDSB4Sg7Y

    vector<int> rotateLeft(int d, vector<int> arr) {
        int n = arr.size();
        vector<int> result(n);
        for(int i = 0; i < n; i++){
            int newIndex = (i - d + n) % n;
            result[newIndex] = arr[i];
        }
        return result;
    }
    
  • + 0 comments

    Solution in Java

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
        /*
         * 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
         */
    
        public static List<Integer> rotateLeft(int d, List<Integer> arr) {
        // Write your code here
        if(d == arr.size()) return arr;
        Integer[] newArr = new Integer[arr.size()];
        
        int shift = arr.size() - d;
        
        for (int i = 0; i < arr.size(); i++) {
                int newIndex = (i + shift) % arr.size();
                newArr[newIndex] =  arr.get(i);
    
            }
            return Arrays.asList(newArr);
        }
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            int n = Integer.parseInt(firstMultipleInput[0]);
    
            int d = 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();
        }
    }