Rotate Array by K Places
Logic Building: Rotations & Sets DSA practice problem on Onlearn.
Difficulty: easy.
Topics: Rotate an array by K elements, Arrays, Time Complexity, Space Complexity, Big O Notation, Loops, Conditional Statements, in-place algorithms, complexity analysis, mathematical operations, array manipulation, auxiliary space, Array Rotation, Array Manipulation, Linked List Reversal, Modular Arithmetic.
Array Rotation Problem Statement Given an array arr of N integers, you need to rotate the array elements by K positions. The rotation can be either to the left or to the right. Input Specification N: The size of the array. arr[]: The array of integers. K: The number of positions to rotate. direction: A string indicating the direction of rotation (either "left" or "right"). Output Specification Print the rotated array. Examples Example 1: Input: N = 7, array[] = {1, 2, 3, 4, 5, 6, 7}, K = 2, direction = "right" Output: 6 7 1 2 3 4 5 Explanation: The array is rotated to the right by 2 positions. Example 2: Input: N = 6, array[] = {3, 7, 8, 9, 10, 11}, K = 3, direction = "left" Output: 9 10 11 3 7 8 Explanation: The array is rotated to the left by 3 positions.