Reverse all elements of given circular array starting from index K | C ++ program for the above approach ; Function to print the array arr [ ] ; Print the array ; Function to reverse elements of given circular array starting from index k ; Initialize two variables as start = k and end = k - 1 ; Initialize count = N / 2 ; Loop while count > 0 ; Swap the elements at index ( start % N ) and ( end % N ) ; Update the start and end ; If end equals to - 1 set end = N - 1 ; Print the circular array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void reverseCircularArray ( int arr [ ] , int N , int K ) { int start = K , end = K - 1 ; int count = N / 2 ; while ( count -- ) { int temp = arr [ start % N ] ; arr [ start % N ] = arr [ end % N ] ; arr [ end % N ] = temp ; start ++ ; end -- ; if ( end == -1 ) { end = N - 1 ; } } printArray ( arr , N ) ; } int main ( ) { int arr [ ] = { 3 , 5 , 2 , 4 , 1 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; reverseCircularArray ( arr , N , K ) ; return 0 ; }