C++ STL | Set 1 (vector) | Practice | GeeksforGeeks

Implement different operations on a vector A .
 
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of input contains an integer Q denoting the no of queries . Then in the next line are Q space separated queries .
A query can be of five types 
1. a x (Adds an element x to the vector A at the end )
2. b (Sorts the vector A in ascending order )
3. c (Reverses the vector A)
4. d (prints the size of the vector)
5. e (prints space separated values of the vector)
5. f  (Sorts the vector A in descending order)


Output:
The output for each test case will  be space separated integers denoting the results of each query . 

Constraints:
1<=T<=100
1<=Q<=100

Example:
Input
2
6
a 4 a 6 a 7 b c e
4
a 55 a 11 d e
 
Output
7 6 4
2 55 11

Explanation :
For the first test case
There are six queries. Queries are performed in this order
1. a 4 { Vector has 4  }
2. a 7 {vector has 7 }
3. a 6 {vector has 6}
4. b {sorts the vector in ascending order, vector now is 5 6 7}
5. c {reverse the vector }
6. e {prints the element of the vectors 7 6 4}

For the sec test case 
There are four queries. Queries are performed in this order
1. a 55  (vector A has 55}
2. a 11  (vector A has 55 ,11}
3. d      (prints the size of the vector A ie. 2 )
4. e      (prints the elements of the vector A ie 55 11)


Note:The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
tanayshah25584 months ago
EASY SOLUTION:  
void add_to_vector(vector<int> &A,int x){//Your code hereA.push_back(x);}
/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){//Your code heresort(A.begin(),A.end());}
/*reverses the vector A*/void reverse_vector(vector<int> &A){   //Your code herereverse(A.begin(), A.end());}
/*returns the size of the vector  A */int size_of_vector(vector<int> &A){//Your code herereturn A.size();}
/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){//Your code heresort(A.rbegin(),A.rend());}
/*prints space separated elements of vector A*/void print_vector(vector<int> &A){//Your code herefor(int i=0;i<A.size();i++){    cout<<A[i]<<" ";}}
0
gpkini20026 months ago
/*You are required to complete below methods*/
/*inserts an element x at the back of the vector A */void add_to_vector(vector<int> &A,int x){//Your code here   A.push_back(x);}
/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){//Your code heresort(A.begin(),A.end());}
/*reverses the vector A*/void reverse_vector(vector<int> &A){//Your code herereverse(A.begin(),A.end());}
/*returns the size of the vector  A */int size_of_vector(vector<int> &A){//Your code hereA.size();}
/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){//Your code heresort(A.rbegin(),A.rend());}
/*prints space separated elements of vector A*/void print_vector(vector<int> &A){//Your code here    for (auto i = A.cbegin(); i != A.cend(); ++i)       cout << *i << " ";}This is my solution to the above problem:)
0
Ashu10 months ago
Ashu
Easy solution: c++
0
Gurveer1 year ago
Gurveer
correct solution
/*inserts an element x at the back of the vector A */void add_to_vector(vector<int> &A,int x){//Your code hereA.push_back(x);}/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){//Your code heresort(A.begin(),A.end());}/*reverses the vector A*/void reverse_vector(vector<int> &A){reverse(A.begin(),A.end());}/*returns the size of the vector  A */int size_of_vector(vector<int> &A){return A.size();}/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){//Your code heresort(A.begin(),A.end(),greater<int>());}/*prints space separated elements of vector A*/void print_vector(vector<int> &A){//Your code herefor(int x: A ){    cout<<x <<"="" ";="" }="" }="" <="" code="">
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
/*inserts an element x at the back of the vector A */void add_to_vector(vector<int> &A,int x){    A.push_back(x);}/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){sort(A.begin(), A.end());}/*reverses the vector A*/void reverse_vector(vector<int> &A){reverse(A.begin(), A.end());}/*returns the size of the vector  A */int size_of_vector(vector<int> &A){return A.size();}/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){sort(A.begin(), A.end(), greater<int>());}/*prints space separated elements of vector A*/void print_vector(vector<int> &A){for(int x: A){    cout << x << " ";}}
0
sagar dhankar2 years ago
sagar dhankar
void add_to_vector(vector<int> &A,int x){//Your code hereA.push_back(x);}
/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){    sort(A.begin(), A.end());//Your code here}
/*reverses the vector A*/void reverse_vector(vector<int> &A){    reverse(A.begin(), A.end());//Your code here}
/*returns the size of the vector  A */int size_of_vector(vector<int> &A){    int x = A.size();    return x;//Your code here}
bool myCmp(int a, int b){    return (a > b);}
/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){    sort(A.begin(), A.end(), myCmp);//Your code here}
/*prints space separated elements of vector A*/void print_vector(vector<int> &A){    for(int i=0; i<a.size(); ++i)="" cout<<="" a[i]="" <<="" "="" ";="" your="" code="" here="" }="">
0
penny53402 years ago
penny5340
shortest sol ET=  0.01upvote me for more .https://ide.geeksforgeeks.o...
0
Yash Parmar2 years ago
Yash Parmar
void add_to_vector(vector<int> &v,int x){//Your code herev.push_back(x);}
/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &v){//Your code heresort(v.begin(),v.end());}
/*reverses the vector A*/void reverse_vector(vector<int> &v){//Your code herereverse(v.begin(),v.end());}
/*returns the size of the vector  A */int size_of_vector(vector<int> &v){//Your code herereturn v.size();}
/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &v){//Your code heresort(v.begin(),v.end());reverse_vector(v);}
/*prints space separated elements of vector A*/void print_vector(vector<int> &v){//Your code herefor(int i=0;i<v.size();i++){ cout<<v[i]<<"="" ";="" }="" }="">
0
samiksha mahajan2 years ago
samiksha mahajan
Hi,
My code fails
What is wrong with my solution? The code fails - "The first test case where your code failed:
Input:34a 50 e d b f b e a 650 b c b c b f e a 427 e e f c d d c c c b b b f a 23 a 70 d b e
Its Correct output is:50 1 50 650 50 650 50 427 650 50 427 3 3 5 23 50 70 427 650
And Your Code's output is:50
"====================code ====================/*inserts an element x at the back of the vector A */void add_to_vector(vector<int> &A,int x){//Your code hereA.push_back(x);}
/*sort the vector A in ascending order*/void sort_vector_asc(vector<int> &A){//Your code heresort(A.begin(), A.end());}
/*reverses the vector A*/void reverse_vector(vector<int> &A){//Your code herereverse(A.begin(), A.end());}
/*returns the size of the vector  A */int size_of_vector(vector<int> &A){//Your code herereturn A.size();}bool f(int a, int b){    return a > b;}/*sorts the vector A in descending order*/void sort_vector_desc(vector<int> &A){//Your code heresort(A.begin(), A.end(), f);}
/*prints space separated elements of vector A*/void print_vector(vector<int> &A){//Your code here//for(auto i = A.begin(); i < A.end(); i++)    for(auto x: A){    cout << x << " ";}cout << endl;}
0
H_ R2 years ago
H_ R
can anyone tell me the time complexity of this problem..
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.