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
#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)
Python code for cyclically rotating the array by k:
n = int(input(“Enter size of an array:”))
arr = []
for i in range(n):
inp = input(“Enter element:”)
arr.append(inp)
k = int(input(“Enter rotation count:”))
if k>n:
k = k%n
for i in range(k):
j = n-1
a = arr[j]
while j>0:
arr[j]=arr[j-1]
j -= 1
arr[0]= a
print(arr)
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 :
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