-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7d5bf5
commit 7a92ee7
Showing
33 changed files
with
531 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,109 @@ | ||
/* | ||
Topic : Algorithms | ||
Subtopic : Diagonal Difference | ||
Language : Python | ||
Language : c++ | ||
Problem Statement : Given a square matrix, calculate the absolute difference between the sums of its diagonals. | ||
Url : https://www.hackerrank.com/challenges/diagonal-difference/problem | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
string ltrim(const string &); | ||
string rtrim(const string &); | ||
vector<string> split(const string &); | ||
|
||
/* | ||
* Complete the 'diagonalDifference' function below. | ||
* | ||
* The function is expected to return an INTEGER. | ||
* The function accepts 2D_INTEGER_ARRAY arr as parameter. | ||
*/ | ||
|
||
int diagonalDifference(vector<vector<int>> arr) { | ||
|
||
int s1 = 0; | ||
int s2 = 0; | ||
int n = arr.size(); | ||
|
||
for(int i=0;i<n;i++) { | ||
s1 += arr[i][i]; | ||
s2 += arr[i][n-i-1]; | ||
} | ||
return abs(s1 - s2); | ||
} | ||
|
||
int main() | ||
{ | ||
ofstream fout(getenv("OUTPUT_PATH")); | ||
|
||
string n_temp; | ||
getline(cin, n_temp); | ||
|
||
int n = stoi(ltrim(rtrim(n_temp))); | ||
|
||
vector<vector<int>> arr(n); | ||
|
||
for (int i = 0; i < n; i++) { | ||
arr[i].resize(n); | ||
|
||
string arr_row_temp_temp; | ||
getline(cin, arr_row_temp_temp); | ||
|
||
vector<string> arr_row_temp = split(rtrim(arr_row_temp_temp)); | ||
|
||
for (int j = 0; j < n; j++) { | ||
int arr_row_item = stoi(arr_row_temp[j]); | ||
|
||
arr[i][j] = arr_row_item; | ||
} | ||
} | ||
|
||
int result = diagonalDifference(arr); | ||
|
||
fout << result << "\n"; | ||
|
||
fout.close(); | ||
|
||
return 0; | ||
} | ||
|
||
string ltrim(const string &str) { | ||
string s(str); | ||
|
||
s.erase( | ||
s.begin(), | ||
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))) | ||
); | ||
|
||
return s; | ||
} | ||
|
||
string rtrim(const string &str) { | ||
string s(str); | ||
|
||
s.erase( | ||
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), | ||
s.end() | ||
); | ||
|
||
return s; | ||
} | ||
|
||
vector<string> split(const string &str) { | ||
vector<string> tokens; | ||
|
||
string::size_type start = 0; | ||
string::size_type end = 0; | ||
|
||
while ((end = str.find(" ", start)) != string::npos) { | ||
tokens.push_back(str.substr(start, end - start)); | ||
|
||
start = end + 1; | ||
} | ||
|
||
tokens.push_back(str.substr(start)); | ||
|
||
return tokens; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Topic : Algorithms | ||
Subtopic : Plus Minus | ||
Language : c++ | ||
Problem Statement : | ||
Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line. | ||
Url : https://www.hackerrank.com/challenges/plus-minus/problem | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
vector<string> split_string(string); | ||
|
||
// Complete the plusMinus function below. | ||
void plusMinus(vector<int> arr) { | ||
|
||
float countPositive = 0; | ||
float countNegative = 0; | ||
float n = arr.size(); | ||
|
||
for(int i;i<n;i++) { | ||
if (arr[i] > 0) | ||
countPositive++; | ||
else if (arr[i] < 0) | ||
countNegative++; | ||
} | ||
float countZero = n - countNegative - countPositive; | ||
cout << setprecision(6) << countPositive/n << endl; | ||
cout << setprecision(6) << countNegative/n << endl; | ||
cout << setprecision(6) << countZero/n << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
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; | ||
} | ||
|
||
plusMinus(arr); | ||
|
||
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; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Topic : Algorithms | ||
Subtopic : StairCase | ||
Language : C++ | ||
Problem Statement : Write a program that prints a staircase of size 'n'. | ||
Url : https://www.hackerrank.com/challenges/staircase/problem | ||
*/ | ||
|
||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
// Complete the staircase function below. | ||
void staircase(int n) { | ||
|
||
for(int i=0;i<n;i++) { | ||
cout << setfill(' ') << setw(n-(i+1)) << ""; | ||
cout << setfill('#') << setw(i+1) << '#'<< endl; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
|
||
staircase(n); | ||
|
||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Topic : Algorithms | ||
Subtopic :MiniMax Sum | ||
Language : C++ | ||
Problem Statement : | ||
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. | ||
For example,arr=[1,3,5,7,9] . Our minimum sum is 1+3+5+7=16 and our maximum sum is 3+5+7+9=24. | ||
Url : https://www.hackerrank.com/challenges/mini-max-sum/problem | ||
*/ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
vector<string> split_string(string); | ||
|
||
// Complete the miniMaxSum function below. | ||
void miniMaxSum(vector<int> arr) { | ||
|
||
long long min = LLONG_MAX , max = LLONG_MIN , sum ; | ||
for(int i = 0 ;i < arr.size() ; ++i) | ||
{ | ||
sum = 0; | ||
for(int j = 0; j < arr.size() ; ++j) | ||
{ | ||
if(i != j) | ||
sum += arr[j]; | ||
} | ||
|
||
if(sum > max) | ||
max = sum; | ||
|
||
if (sum < min) | ||
min = sum; | ||
} | ||
|
||
cout << min << " " << max << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
string arr_temp_temp; | ||
getline(cin, arr_temp_temp); | ||
|
||
vector<string> arr_temp = split_string(arr_temp_temp); | ||
|
||
vector<int> arr(5); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
int arr_item = stoi(arr_temp[i]); | ||
|
||
arr[i] = arr_item; | ||
} | ||
|
||
miniMaxSum(arr); | ||
|
||
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; | ||
} | ||
|
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
Oops, something went wrong.