15,884,036 members
Home / Discussions / C / C++ / MFC

# C / C++ / MFC

 Re: MovePrev() throwing 265926 error code (End Of RowsSet). Sampath5798-Aug-18 5:16 Sampath579 8-Aug-18 5:16
 Re: MovePrev() throwing 265926 error code (End Of RowsSet). David Crow9-Aug-18 4:56 David Crow 9-Aug-18 4:56
 Re: MovePrev() throwing 265926 error code (End Of RowsSet). Stefan_Lang8-Aug-18 21:44 Stefan_Lang 8-Aug-18 21:44
 Re: MovePrev() throwing -2147217837 error code Sampath57912-Aug-18 5:29 Sampath579 12-Aug-18 5:29
 MFC Icon indicator (red/green) Maximilien6-Aug-18 7:20 Maximilien 6-Aug-18 7:20
 Re: MFC Icon indicator (red/green) Victor Nijegorodov6-Aug-18 8:04 Victor Nijegorodov 6-Aug-18 8:04
 Re: MFC Icon indicator (red/green) _Flaviu6-Aug-18 19:55 _Flaviu 6-Aug-18 19:55
 Find element at given index after a number of rotations Tarun Jha5-Aug-18 10:16 Tarun Jha 5-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 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 youmodified 6-Aug-18 4:19am.
 Re: Find element at given index after a number of rotations David Crow5-Aug-18 12:39 David Crow 5-Aug-18 12:39
 Re: Find element at given index after a number of rotations Tarun Jha5-Aug-18 22:21 Tarun Jha 5-Aug-18 22:21
 Re: Find element at given index after a number of rotations CPallini5-Aug-18 20:43 CPallini 5-Aug-18 20:43
 Re: Find element at given index after a number of rotations Tarun Jha5-Aug-18 22:21 Tarun Jha 5-Aug-18 22:21
 Re: Find element at given index after a number of rotations Richard MacCutchan5-Aug-18 21:24 Richard MacCutchan 5-Aug-18 21:24
 Re: Find element at given index after a number of rotations Tarun Jha5-Aug-18 22:20 Tarun Jha 5-Aug-18 22:20
 Re: Find element at given index after a number of rotations Richard MacCutchan6-Aug-18 2:49 Richard MacCutchan 6-Aug-18 2:49
 Re: Find element at given index after a number of rotations DRHuff10-Aug-18 6:31 DRHuff 10-Aug-18 6:31
 Tell me about the c programming languages Member 139375164-Aug-18 17:29 Member 13937516 4-Aug-18 17:29
 Re: Tell me about the c programming languages Victor Nijegorodov4-Aug-18 20:44 Victor Nijegorodov 4-Aug-18 20:44
 Re: Tell me about the c programming languages jfbode10297-Aug-18 7:08 jfbode1029 7-Aug-18 7:08
 Forward error correction in C-programming applied to non-streaming data? arnold_w4-Aug-18 19:23 arnold_w 4-Aug-18 19:23
 Re: Forward error correction in C-programming applied to non-streaming data? leon de boer5-Aug-18 14:25 leon de boer 5-Aug-18 14:25
 Did I Overlook Something, or is the LTCG Document Page Actually Outdated? David A. Gray4-Aug-18 11:32 David A. Gray 4-Aug-18 11:32
 how to query a previous query piano00113-Aug-18 23:03 piano0011 3-Aug-18 23:03
 Re: how to query a previous query Victor Nijegorodov4-Aug-18 2:02 Victor Nijegorodov 4-Aug-18 2:02
 Condition variable question samzcs3-Aug-18 7:31 samzcs 3-Aug-18 7:31
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Apr-24 2:00 Refresh ᐊ Prev1...262263264265266267268269270271 Next ᐅ