swapo algorit
C++
#include <bits/stdc++.h>
using namespace std;
/*Prototype for utility functions */
void printArray(int arr[], int size);
void swap(int arr[], int fi, int si, int d);
void leftRotate(int arr[], int d, int n)
{
/* Return If number of elements to be rotated
is zero or equal to array size */
if(d == 0 || d == n)
return;
/*If number of elements to be rotated
is exactly half of array size */
if(n - d == d)
{
swap(arr, 0, n - d, d);
return;
}
/* If A is shorter*/
if(d < n - d)
{
swap(arr, 0, n - d, d);
leftRotate(arr, d, n - d);
}
else /* If B is shorter*/
{
swap(arr, 0, d, n - d);
leftRotate(arr + n - d, 2 * d - n, d); /*This is tricky*/
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
/*This function swaps d elements starting at index fi
with d elements starting at index si */
void swap(int arr[], int fi, int si, int d)
{
int i, temp;
for(i = 0; i < d; i++)
{
temp = arr[fi + i];
arr[fi + i] = arr[si + i];
arr[si + i] = temp;
}
}
// Driver Code
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
leftRotate(arr, 2, 7);
printArray(arr, 7);
return 0;
}
// This code is contributed by rathbhupendra
Also in C++:
- Title
- convert int to binary string c++
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- c++ ros publisher
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- preorder traversal
- Category
- C++
- Title
- map.erase in c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- c++ while true
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- Merge k sorted linked lists and return it as one sorted list.
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- initialize vector of vector c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- while loops
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- nearest integer rounding in c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- convert to lowercase c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ rainbow text
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- c++ set console title
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- visual studio 2019 c++ tutorial project
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- expected unqualified-id before 'if'
- Category
- C++
- Title
- cout value c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- __builtin_ctz
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++