Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Find element at given index after a number of rotations

Find element at given index after a number of rotations

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++algorithmsdata-structures
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    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

    D C L 3 Replies Last reply
    0
    • T Tarun Jha

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What is a "range" or a "rotate range?" When I hear the term rotate, I envision each element in the array could move one position to the left and the element that is in position 0 now moves to the N-1 position in the array. Similarly, each element in the array could move one position to the right and the element that is in position N-1 now moves to position 0 in the array. Am I way off?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      L T 2 Replies Last reply
      0
      • D David Crow

        What is a "range" or a "rotate range?" When I hear the term rotate, I envision each element in the array could move one position to the left and the element that is in position 0 now moves to the N-1 position in the array. Similarly, each element in the array could move one position to the right and the element that is in position N-1 now moves to position 0 in the array. Am I way off?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Yes it's an array rotation

        In vino veritas

        1 Reply Last reply
        0
        • T Tarun Jha

          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

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Please elaborate: it is not clear what you are trying to accomplish. Note: it could be NOT necessary to actually perform the rotations, you could possibly compute the result without actually rotate the array.

          T 1 Reply Last reply
          0
          • T Tarun Jha

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The logic is there in the code and description, maybe you need to read more of the book you are studying.

            T 1 Reply Last reply
            0
            • L Lost User

              The logic is there in the code and description, maybe you need to read more of the book you are studying.

              T Offline
              T Offline
              Tarun Jha
              wrote on last edited by
              #6

              if you know the logic can you elaborate. please

              L D 2 Replies Last reply
              0
              • C CPallini

                Please elaborate: it is not clear what you are trying to accomplish. Note: it could be NOT necessary to actually perform the rotations, you could possibly compute the result without actually rotate the array.

                T Offline
                T Offline
                Tarun Jha
                wrote on last edited by
                #7

                please check again i have elaborated it and have also provided link to the original question

                1 Reply Last reply
                0
                • D David Crow

                  What is a "range" or a "rotate range?" When I hear the term rotate, I envision each element in the array could move one position to the left and the element that is in position 0 now moves to the N-1 position in the array. Similarly, each element in the array could move one position to the right and the element that is in position N-1 now moves to position 0 in the array. Am I way off?

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  T Offline
                  T Offline
                  Tarun Jha
                  wrote on last edited by
                  #8

                  please check the question again i have edited it and also provided with the link to the original problem

                  1 Reply Last reply
                  0
                  • T Tarun Jha

                    if you know the logic can you elaborate. please

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Like I said, the logic is in the code. You need to study the code and the comments to understand the logic.

                    1 Reply Last reply
                    0
                    • T Tarun Jha

                      if you know the logic can you elaborate. please

                      D Offline
                      D Offline
                      DRHuff
                      wrote on last edited by
                      #10

                      You are not rebuilding the array to match what it would look like after all the rotations. You are looking at a single index in the array. The logic applies to what is the effect of a rotation to that index alone - not the whole array.

                      Socialism is the Axe Body Spray of political ideologies: It never does what it claims to do, but people too young to know better keep buying it anyway. (Glenn Reynolds)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups