Skip to content

added a file in hackerrank array-rotation #82

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
96 changes: 96 additions & 0 deletions Hackerrank/array-left-rotat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//link:https://github.com/koushkigupta/Competitive-Programming--Solution/tree/master/Hackerrank

#include <bits/stdc++.h>

using namespace std;

vector<string> split_string(string);

// Complete the rotLeft function below.
vector<int> rotLeft(vector<int> a, int d) {

vector<int> newa;
for(int i=d;i<a.size();i++)
{
newa.push_back(a[i]);
}
for(int i=0;i<d;i++ )
{
newa.push_back(a[i]);
}

return newa;

}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string nd_temp;
getline(cin, nd_temp);

vector<string> nd = split_string(nd_temp);

int n = stoi(nd[0]);

int d = stoi(nd[1]);

string a_temp_temp;
getline(cin, a_temp_temp);

vector<string> a_temp = split_string(a_temp_temp);

vector<int> a(n);

for (int i = 0; i < n; i++) {
int a_item = stoi(a_temp[i]);

a[i] = a_item;
}

vector<int> result = rotLeft(a, d);

for (int i = 0; i < result.size(); i++) {
cout << result[i];

if (i != result.size() - 1) {
cout << " ";
}
}

cout << "\n";



return 0;
}

vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});

input_string.erase(new_end, input_string.end());

while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}

vector<string> splits;
char delimiter = ' ';

size_t i = 0;
size_t pos = input_string.find(delimiter);

while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));

i = pos + 1;
pos = input_string.find(delimiter, i);
}

splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

return splits;
}