Sort by

recency

|

3554 Discussions

|

  • + 0 comments

    Quick in Python!

    def rotateLeft(num_bits: int, arr: list[int]) -> list[int]:
        """Shift bits in the list/array to the left by num_bits."""
        left_shift_bits = num_bits % len(arr)
        return [*arr[left_shift_bits:], *arr[:left_shift_bits]]
    
  • + 0 comments

    include

    using namespace std;

    int main() { int n, d; cin >> n >> d; vector a(n); for (int i = 0; i < n; i++) cin >> a[i];

    d %= n;
    vector<int> res(n);
    for (int i = 0; i < n; i++)
        res[i] = a[(i + d) % n];  
    
    for (int i = 0; i < n; i++)
        cout << res[i] << (i == n - 1 ? '\n' : ' ');
    
    return 0;
    

    }

  • + 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;
    }