
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print left rotation of array in O(n) time and O(1) space in C Program.
We are given an array of some size n and multiple integer values, we need to rotate an array from a given index k.
We want to rotate an array from a index k like −
Examples
Input: arr[] = {1, 2, 3, 4, 5} K1 = 1 K2 = 3 K3 = 6 Output: 2 3 4 5 1 4 5 1 2 3 2 3 4 5 1
Algorithm
START Step 1 -> Declare function void leftRotate(int arr[], int n, int k) Declare int cal = k% n Loop For int i=0 and i<n and i++ Print arr[(cal+i)%n] End Step 2 -> In main() Declare array a[]={ 1,2,3,4} Declare int size=sizeof(a)/sizeof(a[0]) Declare int k=1 Call leftRotate(a, size, k) Set k=2 Call leftRotate(a, size, k) Set k=3 leftRotate(a, size, k) STOP
Example
#include <bits/stdc++.h> using namespace std; void leftRotate(int arr[], int n, int k){ int cal = k % n; for (int i = 0; i < n; i++) cout << (arr[(cal + i) % n]) << " "; cout << "
"; } int main(){ int a[] = { 1,2,3,4}; int size = sizeof(a) / sizeof(a[0]); int k = 1; leftRotate(a, size, k); k = 2; leftRotate(a, size, k); k = 3; leftRotate(a, size, k); return 0; }
Output
if we run above program then it will generate following output
2 3 4 1 3 4 1 2 4 1 2 3
- Related Articles
- Find median of BST in O(n) time and O(1) space in C++
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Find median of BST in O(n) time and O(1) space in Python
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Print n x n spiral matrix using O(1) extra space in C Program.
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Check for balanced parentheses in an expression - O(1) space - O(N^2) time complexity in C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Print BST keys in given Range - O(1) Space in C++
- Check for balanced parentheses in an expression O(1) space O(N^2) time complexity in Python
- A product array puzzle (O(1) Space) in C++?

Advertisements