Hello coders, today we are going to solve Array Rotation Codechef Solution.

Problem
You are given an array A of N integers. You are to fulfill M queries. Each query has one of the following three types:
- L d : Rotate the array A to the left by d units.
- R d : Rotate the array A to the right by d units.
- Q d : Query the element currently at the d-th index in the array, after all earlier rotations have been carried out.
Input:
The first line contains two numbers – N and M respectively.
The next line contains N space separated Integers, denoting the array A.
Each of the following M lines contains a query in the one of the forms described above.
Output:
For each query of type Q output the answer on a separate line.
Constraints
- 1≤N≤1000001≤N≤100000
- 1≤M≤1000001≤M≤100000
- 1≤d≤N1≤d≤N, in all the queries
- 1≤elementsofA≤10000001≤elementsofA≤1000000
- The array A and the queries of the type R follow 1-based indexing.
Example
Sample Input 1
5 5
5 4 3 3 9
Q 1
L 4
Q 5
R 3
Q 2
Sample Output 1
5 3 3
Array Rotation CodeChef Solution in JAVA
import java.util.Arrays; import java.util.Scanner; import java.util.stream.Collectors; public class Main { static final int MODULUS = 1_000_000_007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] A = new int[N]; for (int i = 0; i < A.length; ++i) { A[i] = sc.nextInt(); } int Q = sc.nextInt(); int[] x = new int[Q]; for (int i = 0; i < x.length; ++i) { x[i] = sc.nextInt(); } System.out.println(solve(A, x)); sc.close(); } static String solve(int[] A, int[] x) { int[] result = new int[x.length]; result[0] = multiplyMod(Arrays.stream(A).reduce(Main::addMod).getAsInt(), 2); for (int i = 1; i < result.length; ++i) { result[i] = multiplyMod(result[i - 1], 2); } return Arrays.stream(result).mapToObj(String::valueOf).collect(Collectors.joining("\n")); } static int mod(long x) { return (int) ((x % MODULUS + MODULUS) % MODULUS); } static int addMod(int x, int y) { return mod(x + y); } static int multiplyMod(int x, int y) { return mod((long) x * y); } }
Array Rotation CodeChef Solution in CPP
#include<bits/stdc++.h> using namespace std; #define ll long long const int mod = 1000000007; //-------------------------------------------------------- void solve(){ ll int n; cin>>n; ll int sum=0; while(n--){ ll int x; cin>>x; sum=(sum+x+mod)%mod; } ll int q; cin>>q; while(q--){ ll int x; cin>>x; sum=(2*sum)%mod; cout<<sum<<"\n"; } return ; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif solve(); }
Array Rotation CodeChef Solution in Python
import io, os, time,math from sys import stdin ,stdout def take_input(): return stdin.readline() def display_arr(arr): stdout.write(" ".join(map(str, arr)) + "\n") def display_num(num): stdout.write(str(num)+ "\n") mod = 10**9+7 n = int(take_input()) arr = list(map(int,take_input().split())) test = int(take_input()) queries = list(map(int,take_input().split())) summe = 0 for i in range(n): summe += arr[i] summe = summe%mod for _ in range(test): summe = (summe+summe)%mod print(summe)
Disclaimer: The above Problem (Array Rotation) is generated by CodeChef but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purpose.