Click here to Skip to main content
15,886,362 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
Sampath5798-Aug-18 5:16
Sampath5798-Aug-18 5:16 
QuestionRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
David Crow9-Aug-18 4:56
David Crow9-Aug-18 4:56 
AnswerRe: MovePrev() throwing 265926 error code (End Of RowsSet). Pin
Stefan_Lang8-Aug-18 21:44
Stefan_Lang8-Aug-18 21:44 
GeneralRe: MovePrev() throwing -2147217837 error code Pin
Sampath57912-Aug-18 5:29
Sampath57912-Aug-18 5:29 
QuestionMFC Icon indicator (red/green) Pin
Maximilien6-Aug-18 7:20
Maximilien6-Aug-18 7:20 
AnswerRe: MFC Icon indicator (red/green) Pin
Victor Nijegorodov6-Aug-18 8:04
Victor Nijegorodov6-Aug-18 8:04 
AnswerRe: MFC Icon indicator (red/green) Pin
_Flaviu6-Aug-18 19:55
_Flaviu6-Aug-18 19:55 
QuestionFind element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 10:16
Tarun 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
David Crow5-Aug-18 12:39 
AnswerRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:21
Tarun Jha5-Aug-18 22:21 
AnswerRe: Find element at given index after a number of rotations Pin
CPallini5-Aug-18 20:43
mveCPallini5-Aug-18 20:43 
GeneralRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:21
Tarun Jha5-Aug-18 22:21 
AnswerRe: Find element at given index after a number of rotations Pin
Richard MacCutchan5-Aug-18 21:24
mveRichard MacCutchan5-Aug-18 21:24 
GeneralRe: Find element at given index after a number of rotations Pin
Tarun Jha5-Aug-18 22:20
Tarun Jha5-Aug-18 22:20 
GeneralRe: Find element at given index after a number of rotations Pin
Richard MacCutchan6-Aug-18 2:49
mveRichard MacCutchan6-Aug-18 2:49 
GeneralRe: Find element at given index after a number of rotations Pin
DRHuff10-Aug-18 6:31
DRHuff10-Aug-18 6:31 
QuestionTell me about the c programming languages Pin
Member 139375164-Aug-18 17:29
Member 139375164-Aug-18 17:29 
AnswerRe: Tell me about the c programming languages Pin
Victor Nijegorodov4-Aug-18 20:44
Victor Nijegorodov4-Aug-18 20:44 
AnswerRe: Tell me about the c programming languages Pin
jfbode10297-Aug-18 7:08
jfbode10297-Aug-18 7:08 
QuestionForward error correction in C-programming applied to non-streaming data? Pin
arnold_w4-Aug-18 19:23
arnold_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
leon 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
David A. Gray4-Aug-18 11:32 
Questionhow to query a previous query Pin
piano00113-Aug-18 23:03
piano00113-Aug-18 23:03 
AnswerRe: how to query a previous query Pin
Victor Nijegorodov4-Aug-18 2:02
Victor Nijegorodov4-Aug-18 2:02 
QuestionCondition variable question Pin
samzcs3-Aug-18 7:31
samzcs3-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.