Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved Day 1 medium 1 problem #8

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion medium/day_1/solution.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
//Write your solution here
//Write your solution here
//Write your solution here
// CODE BY JAGPREET153

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

void add(vector<int>& v, int temp) { // Function for adding the element inside the vector
if (v.empty() ||v.back()<=temp) { // if vector is empty or last element is less than the temp
v.push_back(temp);
return;
}
int value = v[v.size()-1];
v.pop_back();
add(v, temp);
v.push_back(value);
}

void sorting(vector<int>& v) {
if (v.size() == 1) // if vector size is 1
return;
int temp=v.back();
v.pop_back(); // pop the last element
sorting(v);
add(v, temp); // add the last element in the vector
}

// first we will sort the vector by popping the last element and then calling the sorting function again and again till the size of vector is 1
// and then we will insert the last element in the vector after checking tha last element is greater than the temp or not
int main() {
int n,i,x,t;
cin>>t;
while(t--)
{
cin >> n;
vector<int> v;
for (i=0;i<n;i++) { // Taking input in vector
cin>>x;
v.push_back(x);
}
sorting(v); // Sorting the vector
for (int i=0;i<n;i++) {
cout<<v[i]<< " ";
}
cout<<endl;
}

return 0;
}

Loading