problem no 41:(array problem):rotate the array of size n by d in clockwise direction.

 QUESTION:

Given an unsorted array arr[] of size N, rotate it by D elements (clockwise). 

 

Input:
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.

Output:
For each testcase, in a new line, output the rotated array.

Constraints:
1 <= T <= 200
1 <= N <= 107
1 <= D <= N
0 <= arr[i] <= 105

Example:
Input:
2
5 2
1 2 3 4 5 
10 3
2 4 6 8 10 12 14 16 18 20

Output:
3 4 5 1 2
8 10 12 14 16 18 20 2 4 6

 

ANSWER:

#include <iostream>
using namespace std;

int main() {
int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        int d;cin>>d;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        
        for(int j=d;j<n;j++)
        {
            cout<<arr[j]<<" ";
        }
        for(int k=0;k<d;k++)
        {
            cout<<arr[k]<<" ";
        }
        cout<<"\n";
    }
   
    return 0;
}

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.