Click here to Skip to main content
13,665,440 members

C / C++ / MFC

 
AnswerRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
Sampath5798-Aug-18 5:16
memberSampath5798-Aug-18 5:16 
QuestionRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
David Crow9-Aug-18 4:56
memberDavid Crow9-Aug-18 4:56 
AnswerRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
Stefan_Lang8-Aug-18 21:44
memberStefan_Lang8-Aug-18 21:44 
GeneralRe: MovePrev() throwing -2147217837 error code Pin
Sampath57912-Aug-18 5:29
memberSampath57912-Aug-18 5:29 
QuestionMFC Icon indicator (red/green) Pin
Maximilien6-Aug-18 7:20
memberMaximilien6-Aug-18 7:20 
AnswerRe: MFC Icon indicator (red/green) Pin
Victor Nijegorodov6-Aug-18 8:04
memberVictor Nijegorodov6-Aug-18 8:04 
AnswerRe: MFC Icon indicator (red/green) Pin
_Flaviu6-Aug-18 19:55
member_Flaviu6-Aug-18 19:55 
QuestionFind element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 10:16
memberTarun Jha5-Aug-18 10:16 
The author's code :

An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.
Quote:
Input : arr[] : {1, 2, 3, 4, 5}
ranges[] = { {0, 2}, {0, 3} }
index : 1
Output : 3
Explanation : After first given rotation {0, 2}
arr[] = {3, 1, 2, 4, 5}
After second rotation {0, 3}
arr[] = {4, 3, 1, 2, 5}
After all rotations we have element 3 at given
index 1.

// CPP code to rotate an array
// and answer the index query
#include <bits/stdc++.h>
using namespace std;

// Function to compute the element at
// given index
int findElement(int arr[], int ranges[][2],
               int rotations, int index)
{
    for (int i = rotations - 1; i >= 0; i--) {

        // Range[left...right]
        int left = ranges[i][0];
        int right = ranges[i][1];

        // Rotation will not have any effect
        if (left <= index && right >= index) {
            if (index == left)
                index = right;
            else
                index--;
        }
    }

    // Returning new element
    return arr[index];
}

// Driver
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };

    // No. of rotations
    int rotations = 2;

    // Ranges according to 0-based indexing
    int ranges[rotations][2] = { { 0, 2 }, { 0, 3 } };

    int index = 1;

    cout << findElement(arr, ranges, rotations, index);

    return 0;

}


Quote:
and the authors logic:


Method : Efficient
We can do offline processing after saving all ranges.
Suppose, our rotate ranges are : [0..2] and [0..3]
We run through these ranges from reverse.

After range [0..3], index 0 will have the element which was on index 3.
So, we can change 0 to 3, i.e. if index = left, index will be changed to right.
After range [0..2], index 3 will remain unaffected.

So, we can make 3 cases :
If index = left, index will be changed to right.
If index is not bounds by the range, no effect of rotation.
If index is in bounds, index will have the element at index-1.

i tried buy brute fore method and it took O(n^2) time complexity. So i want to understand this logic, can someone please explain the logic.

i have edited the information given, and it concludes all the information given by the question. Below is the link to the problem.
Find element at given index after a number of rotations - GeeksforGeeks[^]

Thank you

modified 6-Aug-18 4:19am.

QuestionRe: Find element at given index after a number of rotations Pin
David Crow5-Aug-18 12:39
memberDavid Crow5-Aug-18 12:39 
AnswerRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:21
memberTarun Jha5-Aug-18 22:21 
AnswerRe: Find element at given index after a number of rotations Pin
CPallini5-Aug-18 20:43
mvpCPallini5-Aug-18 20:43 
GeneralRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:21
memberTarun Jha5-Aug-18 22:21 
AnswerRe: Find element at given index after a number of rotations Pin
Richard MacCutchan5-Aug-18 21:24
protectorRichard MacCutchan5-Aug-18 21:24 
GeneralRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:20
memberTarun Jha5-Aug-18 22:20 
GeneralRe: Find element at given index after a number of rotations Pin
Richard MacCutchan6-Aug-18 2:49
protectorRichard MacCutchan6-Aug-18 2:49 
GeneralRe: Find element at given index after a number of rotations Pin
DRHuff10-Aug-18 6:31
memberDRHuff10-Aug-18 6:31 
QuestionTell me about the c programming languages Pin
Member 139375164-Aug-18 17:29
memberMember 139375164-Aug-18 17:29 
AnswerRe: Tell me about the c programming languages Pin
Victor Nijegorodov4-Aug-18 20:44
memberVictor Nijegorodov4-Aug-18 20:44 
AnswerRe: Tell me about the c programming languages Pin
jfbode10297-Aug-18 7:08
memberjfbode10297-Aug-18 7:08 
QuestionForward error correction in C-programming applied to non-streaming data? Pin
arnold_w4-Aug-18 19:23
memberarnold_w4-Aug-18 19:23 
AnswerRe: Forward error correction in C-programming applied to non-streaming data? Pin
leon de boer5-Aug-18 14:25
memberleon de boer5-Aug-18 14:25 
QuestionDid I Overlook Something, or is the LTCG Document Page Actually Outdated? Pin
David A. Gray4-Aug-18 11:32
memberDavid A. Gray4-Aug-18 11:32 
Questionhow to query a previous query Pin
piano00113-Aug-18 23:03
memberpiano00113-Aug-18 23:03 
AnswerRe: how to query a previous query Pin
Victor Nijegorodov4-Aug-18 2:02
memberVictor Nijegorodov4-Aug-18 2:02 
QuestionCondition variable question Pin
samzcs3-Aug-18 7:31
membersamzcs3-Aug-18 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 17 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid