0

I am actually trying to solve the K rotate question where we have to rotate the key number of elements to the right and place them on the left. I have checked the whole code using a normal array instead of a vector and it works fine but the function with the vector array never returns anything when i run this. I have checked all the online resources and cannot identify what exactly is the error as the logic and syntax are both correct. pls, help out with this !!

#include<bits/stdc++.h>
using namespace std;

vector<int> rotate_array(vector<int> arr, int n, int key)
{
    int i,j=0;
    vector<int> subst;

    for(i=n-1; i>=n-key; i--)
    {
        subst[j] = arr[i];
        j++;
    }

    j=0;
    
    for(i=key; i<n; i++)
    {
        subst[i] = arr[j];
        j++;
    }

    return subst;
}

int main()
{
    vector<int> arr =  {1, 2, 3, 4, 5};
    // output for this should be -- {4, 5, 1, 2, 3}
    int n = arr.size();
    int key = 2;
    vector<int> array = rotate_array(arr, n, key);
    for(int i=0; i<n; i++)
    {
        cout<<array[i]<<" ";
    }
}
2
  • 2
    std::rotate() exists. Commented Jul 17, 2022 at 16:58
  • You'd better avoid al those garbage "coding" websites. The only thing they teach is how to write really bad code. Commented Jul 17, 2022 at 17:36

3 Answers 3

2

The vector subst in the function rotate_array has no elements, so accessing its "elements" (subst[j] and subst[i]) is illegal.

You have to allocate elements. For example, you can do that using the constructor:

vector<int> subst(n); // specify the number of elements to allocate
Sign up to request clarification or add additional context in comments.

Comments

0

Adding to MikeCAT's solution, a good practice would be to pass the vector to rotate_array() function as a reference. Just change your function signature like so:

vector<int> rotate_array(vector<int>& arr, int n, int key)

Follow this practice when passing data containers as this will reduce your code's memory usage when you submit the code. For example here in your case, there is absolutely no need to create a new copy of the vector for the rotate_array() function.

Comments

0

Hey what I am understanding from your code is that you have make a vector subst which will have initial capacity of 0 and you are adding value to vector using this syntax

subst[j] = arr[i];

which will work if the capacity is greater than the index at which you are providing a value. but in this case it will store the value of arr[i] at a memory which is not there in your vector.

To add a item to vector use

subst.push_back(arr[i])

If you are sure that provided index is present there in your vector then you can use

subst[index]=newValue // to update, to add(in case capacity>index)
cout<<subset[index] // to access an element at index i

To get a value at index i you should use at()

subst.at(index);   // It throws an exception if the position value is invalid

For other functions refer https://cplusplus.com/reference/vector/vector/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.