Created
May 2, 2021 18:29
-
-
Save arsho/c7e7af845698101cef175bcb27fdb4bc to your computer and use it in GitHub Desktop.
Solution of hackerearth challenge (https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/mergesort/) and hackerrank challenge (https://www.hackerrank.com/challenges/ctci-merge-sort/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
long merge_sorted_arrays(long ar[], long temp[], long left, long mid, long right) { | |
long inversion_count = 0; | |
long left_pos = left; | |
long right_pos = mid + 1; | |
long current_pos = left; | |
while (left_pos <= mid && right_pos <= right) { | |
if (ar[left_pos] <= ar[right_pos]) { | |
temp[current_pos++] = ar[left_pos++]; | |
} | |
else { | |
temp[current_pos++] = ar[right_pos++]; | |
inversion_count += (mid - left_pos + 1); | |
} | |
} | |
while (left_pos <= mid) { | |
temp[current_pos++] = ar[left_pos++]; | |
} | |
while (right_pos <= right) { | |
temp[current_pos++] = ar[right_pos++]; | |
} | |
for(long i = left; i<= right; i++) { | |
ar[i] = temp[i]; | |
} | |
return inversion_count; | |
} | |
long perform_merge_sort(long ar[], long temp[], long left, long right) { | |
long inversion_count = 0; | |
if (left < right) { | |
long mid = (right + left) / 2; | |
inversion_count += perform_merge_sort(ar, temp, left, mid); | |
inversion_count += perform_merge_sort(ar, temp, mid+1, right); | |
inversion_count += merge_sorted_arrays(ar, temp, left, mid, right); | |
} | |
return inversion_count; | |
} | |
long get_inversions_count(long ar[], long n) { | |
long temp[n]; | |
for (long i = 0; i < n; i++) { | |
temp[i] = ar[i]; | |
} | |
return perform_merge_sort(ar, temp, 0, n-1); | |
} | |
int main() { | |
long n; | |
cin >> n; | |
long ar[n]; | |
for(long i=0; i<n; i++) | |
cin >> ar[i]; | |
cout << get_inversions_count(ar, n) << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
vector<string> split_string(string); | |
long merge_vectors(vector<int> &ar, vector<int> &temp, long left, long mid, long right) { | |
long inversion_count = 0; | |
long left_pos = left; | |
long right_pos = mid + 1; | |
long current_pos = left; | |
while (left_pos <= mid && right_pos <= right) { | |
if (ar[left_pos] <= ar[right_pos]) { | |
temp[current_pos++] = ar[left_pos++]; | |
} | |
else { | |
temp[current_pos++] = ar[right_pos++]; | |
inversion_count += (mid - left_pos + 1); | |
} | |
} | |
while (left_pos <= mid) { | |
temp[current_pos++] = ar[left_pos++]; | |
} | |
while (right_pos <= right) { | |
temp[current_pos++] = ar[right_pos++]; | |
} | |
for(long i=left; i<=right; i++) { | |
ar[i] = temp[i]; | |
} | |
return inversion_count; | |
} | |
long split_and_merge(vector<int> &ar, vector<int> &temp, long left, long right) { | |
long inversion_count = 0; | |
if(left < right) { | |
long mid = (left + right) / 2; | |
inversion_count += split_and_merge(ar, temp, left, mid); | |
inversion_count += split_and_merge(ar, temp, mid+1, right); | |
inversion_count += merge_vectors(ar, temp, left, mid, right); | |
} | |
return inversion_count; | |
} | |
long countInversions(vector<int> ar) { | |
vector<int> temp = ar; | |
return split_and_merge(ar, temp, 0, ar.size()-1); | |
} | |
int main() | |
{ | |
ofstream fout(getenv("OUTPUT_PATH")); | |
int t; | |
cin >> t; | |
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
for (int t_itr = 0; t_itr < t; t_itr++) { | |
int n; | |
cin >> n; | |
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | |
string arr_temp_temp; | |
getline(cin, arr_temp_temp); | |
vector<string> arr_temp = split_string(arr_temp_temp); | |
vector<int> arr(n); | |
for (int i = 0; i < n; i++) { | |
int arr_item = stoi(arr_temp[i]); | |
arr[i] = arr_item; | |
} | |
long result = countInversions(arr); | |
fout << result << "\n"; | |
} | |
fout.close(); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment