TCS Digital Coding Question 1
Cyclically Rotate the array by K
Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K.
Example :
5 —Value of N
{10, 20, 30, 40, 50} —Element of Arr[ ]
2 —Value of K
Output : 40 50 10 20 30

Algorithm :
- Take the value of N, from the user.
- Create a vector of N size.
- Take the elements of the vector from the user.
- Take the value of K , from the user.
- First handle the case : if K >=N, for that set K= K%N.
- Set , K to the position which comes first after the K rotation i.e. K=N-K.
- Now , reverse the vector from 0 To N-K position and again reverse the vector from N-K to N.
- Finally reverse the entire vector to get the desired rotated vector.
C++ Program Based on Above Algorithm :

#include<bits/stdc++.h>
using namespace std;
int main ()
{
int N;
cin >> N;
vector < int >Arr (N);
for (int i = 0; i < N; i++)
cin >> Arr[i];
int K;
cin >> K;
K = K % N; //(if K>=N )
K = (N - K);
reverse (Arr.begin (), Arr.begin () + K);
reverse (Arr.begin () + K, Arr.end ());
reverse (Arr.begin (), Arr.end ());
for (int i = 0; i < N; i++)
cout << Arr[i] << " ";
return 0;
}
Input :
5
10 20 30 40 50
2
Output :
40 50 10 20 30
#Java Code
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.*;
class Main{
public static void rotate(int[] a, int s, int e){
while (s<e){
int t = a[s];
a[s] = a[e];
a[e] = t;
s++;
e–;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
int k = sc.nextInt();
k%=n;
rotate(arr, n-k, n-1);
rotate(arr, 0, n-k-1);
rotate(arr, 0, n-1);
for(int i=0; i<n; i++){
System.out.print(arr[i]+ " ");
}
}
}
#Python code Easy Method. #Gangasekhar
arr = [10, 20, 30, 40, 50]
n = 5
k = 2
for i in range(0, n):
k = k % n
result = arr[-k:] + arr[:-k]
print(result)
Kindly join our Discord Channel for technical queries.
#Python code Easy Method. #Anil Kumar
n = int(input())
arr = list(map(int,input().split()))
k = int(input())
rotated_array = arr[-k:] + arr[:-k]
result_str = ” “.join(map(str,rotated_array))
print(result_str)
#include
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i>arr[i];
}
int k;
cin>>k;
k=k%n;
int a[n];
int j=n-k;
for(int i=0;i<k;i++){
a[i]=arr[j];
j++;
}
j=0;
for(int i=k;i<n;i++){
a[i]=arr[j];
j++;
}
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}
Hey!!
Join discord for all your queries.
public class Main {
public static void kew(int[]arr,int n,int k){
int arr2[]=new int[n];
for(int i=0;i<k;i++){
arr2[i]=arr[n-k+i];
}
int o=0;
for(int z=k;z<n;z++){
arr2[z]=arr[o];
o=o+1;
}
for(int y=0;y<k;y++){
int temp=arr2[y];
arr2[y]=arr2[k-y-1];
arr2[k-y-1]=temp;
}
System.out.println(Arrays.toString(arr2));
}
public static void main(String[] args) {
int arr[]={10, 20, 30, 40};
int n=arr.length;
int k=1;
kew(arr,n,k);
}
}
Hey,
For all your technical queries Join Here
def rotate_array(arr, k, n):
k = k % n # To handle cases where k > n
# Reverse the entire array
arr.reverse()
# Reverse the first K elements
arr[:k] = reversed(arr[:k])
# Reverse the remaining N-K elements
arr[k:] = reversed(arr[k:])
return arr
# Example usage
n=5
arr = [10, 20, 30, 40, 50]
k = 2
rotated_arr = rotate_array(arr, k, n)
print(rotated_arr)
#Vikas Jain
x=list(map(int,input(“”).split()))
k=int(input(“”))
x1=x[len(x)-a:]
x2=x[:len(x)-a]
x=x1+x2
print(x)
arr=[10,20,30,40,50]
a=arr[::-1]
print(a)
b=a.pop(1)
print(b)
print(a)
index=1
num=b
a.insert(index,num)
print(a
n=int(input())
x=[]
p=[]
x=list(map(int,input().split()))
c = int(input())
q = x[:-c]
d = x[-c:]
print(d+q)
Python solution:
num = int(input())
lis = list(map(int,input().split()))
k = int(input())
lis = lis[-k:] + lis[:-k]
print(lis)