Sort by

recency

|

3552 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.stream.Stream;
    
    public class ArrayLeftRotation {
    
        public static void main(String[] args) throws IOException {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
                int[] firstLine = Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
                int n = firstLine[0];
                int d = firstLine[1];
                int[] array = Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
                for (int i = 0; i < n; i++) {
                    bw.write(array[(i + d) % n] + " ");
                }
                bw.newLine();
                bw.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
  • + 0 comments

    Data structures are the backbone of efficient programming! They help organize and store data in ways that make problem-solving faster and more effective. Betinexchange247

  • + 0 comments

    With out extra space , use reverse function

    vector<int> rotateLeft(int d, vector<int> arr) {
        int n = arr.size();
        d = n - d%n;
        if(d==n) return arr;
        reverse(arr.begin(), arr.end());
        reverse(arr.begin(), arr.begin()+d);
        reverse(arr.begin()+d, arr.end());
        return arr;
    }
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    
        int n = in.nextInt();  // number of elements
        int d = in.nextInt();  // number of left rotations
    
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
    
        // Perform left rotations efficiently
        int[] rotated = new int[n];
        for (int i = 0; i < n; i++) {
            rotated[i] = arr[(i + d) % n];
        }
    
        // Print result
        for (int i = 0; i < n; i++) {
            System.out.print(rotated[i] + (i < n - 1 ? " " : ""));
        }
    
        in.close();
    }
    

    }

  • + 0 comments

    Here's a solution in Javascript

    function rotateLeft(d, arr) {
      let arrSlice = arr.slice(0, d);
      let rotateArr = arr.slice(d)
      let finalArr = [...rotateArr, ...arrSlice];
      return finalArr;
    }