Question 2

13 comments on “Question 2”


  • Harshal

    / /rotate array k times

    void rotate(vector arr, int n, int k, int q, vector query)
    {
    // k = 3 n = 7
    // 1 2 3 4 5 6 7
    // 0 1 2 3 4 5 6

    reverse(arr.begin(), arr.begin() + (n – k));
    reverse(arr.begin() + (n – k), arr.end());
    reverse(arr.begin(), arr.end());

    for (int i = 0; i < q; i++)
    cout << arr[query[i]] <> n >> k >> q;

    vector arr(n);
    vector query(q);

    for (int i = 0; i > arr[i];

    for (int i = 0; i > query[i];

    rotate(arr, n, k, q, query);
    return 0;
    }


  • Abhishek

    def circularArrayRotation(a,k,queries):
    f = []
    b = []
    if len(a) len(a):
    k = k -len(a)
    for i in range(len(a)-k,len(a)):
    f.append(a[i])
    for i in range(0,len(a)-k):
    f.append(a[i])

    else:
    for i in range(len(a)-k,len(a)):
    f.append(a[i])
    for i in range(0,len(a)-k):
    f.append(a[i])
    for i in queries:
    b.append(f[i])
    return b

    nkq = input().split()
    n = int(nkq[0])
    k = int(nkq[1])
    q = int(nkq[2])

    a = list(map(int,input().rstrip().split()))
    queries = []
    for _ in range(q):
    item = int(input())
    queries.append(item)

    result = circularArrayRotation(a,k,queries)
    for i in result:
    print(i)


  • Aniket

    def rotate(li,k):
    list_num = len(li) -1
    for i in range(k):
    ct = li[list_num]
    li.pop(list_num)
    li.insert(0,ct)
    return li

    def display(li,liq):
    for i in liq:
    print(li[i])

    nkq = input().split()
    n = int(nkq[0])
    k = int(nkq[1])
    q = int(nkq[2])
    li = list(map(int,input().split()))
    liq = []
    for i in range(q):
    liq.append(int(input()))
    if len(li)==n and len(liq)==q:
    rotate(li,k)
    display(li,liq)
    else:
    print(“Invalid Input”)


  • PRASHANT

    a=list(map(int,input().strip().split()))
    arr=list(map(int,input().strip().split()))
    arr2=[]
    for _ in range(a[2]):
    arr2.append(int(input()))

    n=a[0]
    k=a[1]
    k=k%n
    arr = arr[n-k:] + arr[:n-k]

    print(arr)
    for i in range(a[2]):
    print(arr[arr2[i]])


  • Pinaki Prasanna

    from typing import List

    nkq = input().split()
    n = int(nkq[0])
    k = int(nkq[1])
    q = int(nkq[2])
    a = []
    list_items = input().split()
    for i in range(n):
    a.append(list_items[i])
    queries = []

    for _ in range(q):
    queries_item = int(input())
    queries.append(queries_item)

    def circular_array_rotation(a: List, k, queries):
    for _ in range(k):
    temp = a[-1]
    a.pop(-1)
    a.insert(0, temp)

    for i in a:
    print(int(i))

    circular_array_rotation(a=a, k=k, queries=queries)


  • Pratik

    def circularArrayRotation(a,k,queries):
    k = k%n #to reduce repeated rotation
    result = []
    for i in queries:
    result.appen(a[(n-k+i)%n])
    return result


  • Vinayak

    #include

    int main()
    {
    int x=0, y=0,z=0,b;
    scanf(“%d”,&x);
    scanf(“%d”,&y);
    scanf(“%d”,&z);
    int a[x];
    for(int i=0;i<x;i++)
    {
    scanf("%d",&a[i]);
    }
    int r[z];
    for(int j=0;j<z;j++)
    {
    scanf("%d",&r[j]);

    }
    for( int k=0;k<z;k++)
    {
    b=r[k]-y;
    if(b<0)
    {
    b=b+x;
    }
    printf("%d\n",a[b]);
    }

    return 0;


  • Rudra

    static int[] circularArrayRotation(int[] a, int k, int[] queries) {
    int [] result = new int [queries.length];
    List n = Arrays.stream(a).boxed().collect(Collectors.toList());
    int noOfRotations=0;
    int s=a.length;

    if(k>=s){
    if(k % s == 0){

    }
    else{
    noOfRotations=k-s;
    }
    }else{
    noOfRotations = k;
    }
    Collections.rotate(n,noOfRotations);
    for(int i =0;i<result.length;i++){
    result[i] = n.get(queries[i]);
    }
    return result;
    }


  • Deepjyoti

    import java.util.Scanner;

    public class Main {

    public static void main(String[] args) {
    // write your code here
    Scanner input = new Scanner(System.in);
    int size = input.nextInt();
    int rotateCount = input.nextInt();
    int queries = input.nextInt();
    int[] elements = new int[size];
    int[] index = new int[queries];
    for (int i=0; i<size; i++)
    {
    elements[i] = input.nextInt();
    }
    for (int i=0; i0; i–)
    {
    elements[i] = elements[i-1];
    }
    elements[0] = lastElem;
    rotateCount–;
    }
    for (int i=0; i<queries; i++)
    {
    System.out.println(elements[index[i]]);
    }
    }
    }


  • Tarit

    N, K, Q =(int(i) for i in input().split())
    arr=[int(i) for i in input().split()]

    for _ in range(Q):
    print(arr[(int(input())-K)%(len(arr))])


  • Dipayan

    #include
    #define RIGHT 1
    #define LEFT 0
    int circularRotation(int a[], int size, int dir, int r);
    int main()
    {
    int i,j,n,k,q,t;
    scanf(“%d%d%d”,&n,&k,&q);
    int a[n],b[q];
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }
    for(i=0;i<q;i++){
    scanf("%d",&b[i]);
    }
    circularRotation(a, n, RIGHT, k);
    printf("\n");
    for(i=0;i=1;i–){
    a[i] = a[i-1];
    }
    a[0] = temp;
    r–;

    }
    }
    return 0;
    }


  • Dipayan

    #include
    #define RIGHT 1
    #define LEFT 0
    int circularRotation(int a[], int size, int dir, int r);
    int main()
    {
    int i,j,n,k,q,t;
    scanf(“%d%d%d”,&n,&k,&q);
    int a[n],b[q];
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }
    for(i=0;i<q;i++){
    scanf("%d",&b[i]);
    }
    circularRotation(a, n, RIGHT, k);
    printf("\n");
    for(i=0;i=1;i–){
    a[i] = a[i-1];
    }
    a[0] = temp;
    r–;

    }
    }
    else
    {
    while(r){
    temp = a[0];
    for(i=0;i<size – 1;i++){
    a[i] = a[i-1];
    }
    a[size-1] = temp;
    r–;

    }
    }
    return 0;
    }