











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
Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K. keep the first position of array unaltered.
PYTHON
def Rotate(arr, k, output):
if not output: output.append(arr.pop(0))
if len(arr)==k:
for _ in range(k):
if arr: output.append(arr.pop(-1))
for i in range(k):
if arr: output.append(arr.pop(0))
if arr: Rotate(arr, k, output)
return(output)
arr = [10,20,30]
k = 4
output = []
Rotate(arr, k, output)
n=int(input())
l=[]
for i in range(n):
l.append(int(input()))
k=int(input())
ln=len(l)
i=ln-k
r=[]
for j in range(i,ln):
r.append(l[j])
for h in l:
if h not in r:
r.append(h)
print(r)
//rotate array clockwise //C++
#include
using namespace std;
int main()
{
int arr[]={10,20,30,40,50};
int arr2[5]={0};
int n=5;
int k=1;
int store=k;
int temp=0;
while(k!=0)
{
arr2[temp]=arr[n-k];
–k;
++temp;
}
for(int i=0;i<n-store;i++)
{
arr2[temp]=arr[i];
++temp;
}
for(int i=0;i<n;i++)
cout<<arr2[i]<<" ";
}
int main() {
int n;
cin>>n;
int arr[n+1];
for(int i=1;i>arr[i];
int k;
cin>>k;
int index=1,val=arr[1];
for(int i=1;in)
index=(index+k)%n;
else
index=index+k;
int temp_val=arr[index];
arr[index]=val;
val=temp_val;
}
for(int i=1;i<=n;i++)
cout<<arr[i]<>n;
int arr[n+1];
for(int i=1;i>arr[i];
int k;
cin>>k;
int index=1,val=arr[1];
for(int i=1;in)
index=(index+k)%n;
else
index=index+k;
int temp_val=arr[index];
arr[index]=val;
val=temp_val;
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
return 0;
}
//O(n) time , O(1) space C++ solution
Python:
list = [1,2,3,4,5]
n = int(input(“rotation: “))
length = len(list)
y = n – length
first_half = list[:-y]
second_half = list[-n:]
third_comp = second_half + first_half
print(first_half)
print(second_half)
print(third_comp)
Python :
N = int(input())
arr = list(map(int,input().split()))[:N]
K = int(input())
K = K%N
for i in range(K):
x = arr.pop(-1)
arr.insert(0,x)
print(*arr)
In Python:
num = int(input())
lis = list(map(int,input().split(‘,’)))
k = int(input())
res = []
for i in range(num):
if i+k >= num:
res.insert(abs(num-i-k), lis[i])
else:
res.insert(i+k, lis[i])
print(res)
#solution in python
def rotatearr(a,n,k):
a[:]=a[n-k:n]+a[0:n-k]
return a
n=int(input(“Please enter number of elements\n”))
a=list(map(int,input(“enter the elements:”).strip().split()))[:n]
k=int(input(“enter a value”))
res=rotatearr(a,n,k)
str_res=[str(i) for i in res]
final_res=” “.join(str_res)
print(final_res)
a = int(input())
x = [int(i) for i in input().split()]
k = int(input())
y = x[:len(x)-k]
x = set(x)
y = set(y)
z = x – y
z = list(z)
x = list(x)
y = list(y)
print(z+y)
# In Python