Find the element at given index | Practice | GeeksforGeeks

Given an array Arr[] of N elements. Perform K right circular rotations on given ranges [L...R]. After performing these rotations, we need to find element at a given index X.
Example 1:
Input:
N = 5, X = 1, K = 2
Arr[] = {1, 2, 3, 4, 5}
Ranges[][] = {{0, 2}, {0, 3}}
Output: 3
Explanation: Rotating the elements in range 
0-2 and 0-3, we have array as 4 3 1 2 5. 
Element at first position is 3.

Example 2:
Input:
N = 3, X = 2, K = 1
Arr[] = {1, 2, 3}
Ranges[][] = {{0, 1}}
Output: 3

Your Task:
You don't need to read input or print anything. Your task is to complete the function findElement() which takes the array of integers arr, n, x, ranges and k as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(K)
Expected Auxiliary Space: O(1)
Constraints:
2 <= N <= 105
1 <= Arr[i] <= 105
1 <= K <= 105
X < N
0 <= L <= R
 
0
AKSHAT AGGARWAL9 months ago
AKSHAT AGGARWAL
I have earned 110 geek bits and I want to redeem them.How can I do that please help!
0
abhikarsh gupta1 year ago
abhikarsh gupta
Problem statement is kind of confusing :-  " Perform K right circular rotations on given ranges [L...R].".
Here K is the size of ranges array. what I initially thought was, for each range, we have to perform K rotations.. but that is not the case.
Had to see the hints as the brute force that i tried( rotating the array for all ranges gave TLE.
Anyhow, here is the code :-
int findElement(int arr[], int n, int x, int ranges[][2], int k) {        for(int i = k - 1; i >= 0; i--)        {            if(x > ranges[i][0] && x <= ranges[i][1])                x--;            else if(x == ranges[i][0])                x = ranges[i][1];        }        return arr[x];    }
0
vishal jat2 years ago
vishal jat
simple c++ solution O(k) time complexity....using STLhttps://ide.geeksforgeeks.o...
0
Shivam Gohri2 years ago
Shivam Gohri
0.41 c++
#include <iostream>#include <vector>
using namespace std;
int main() {
int t;cin>> t;
while(t--){
    int n, x, k;    cin>> n >> x >> k;
    vector<int> v(n,0);
    for(int i=0; i<n; i++)="" cin="">> v[i];
        int temp = k;
        while(temp--){
            int start, end;            cin>> start >>end;
            int x = v[end];            v.erase(v.begin()+end);            v.insert(v.begin()+start, x);
        }
        cout<< v[x] <<endl; }="" return="" 0;="" }="">
-1
Bicky2 years ago
Bicky
simple cpp soln 0.09 s
https://ide.geeksforgeeks.o...
0
Shivanshu Tiwari2 years ago
Shivanshu Tiwari
Hint-idea is that you have to think in reverse direction.only consider given range and change accordingly.if the index is in given range decrement it by 1.only breakpoint is if index==left then convert it in to right;you can check my code here .https://ide.geeksforgeeks.org/B2BH4...
0
Astha Gupta2 years ago
Astha Gupta
@quandray:disqus can u help me with this? https://ide.geeksforgeeks.o...
0
Thomas White
This comment was deleted.
+1
Thomas White4 years ago
Thomas White
need to clarify this description.  the rotation is occuring in the "sub arrays" given by the ranges NOT rotating the range within the entire array.  For example,[1 2 3 4 5 6 7] given the range {0 2} means [1 2 3] becomes [3 1 2] and the array is then [3 1 2 4 5 6 7].
0
Astha Jindal4 years ago
Astha Jindal
How the   test case is working?
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.