Skip to content

Commit

Permalink
some solution added
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Jul 31, 2020
1 parent b7d5bf5 commit 7a92ee7
Show file tree
Hide file tree
Showing 33 changed files with 531 additions and 4 deletions.
1 change: 0 additions & 1 deletion 03. CompareTheTriplet/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ vector<int> compareTriplets(vector<int> a, vector<int> b) {
result.push_back(aliceScore);
result.push_back(bobScore);
return result;

}

int main()
Expand Down
1 change: 1 addition & 0 deletions 04. AVeryBigSum/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Complete the aVeryBigSum function below.
def aVeryBigSum(ar):
return sum(ar)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

Expand Down
104 changes: 103 additions & 1 deletion 05. DiagonalDifference/solution.cpp
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;
}
87 changes: 87 additions & 0 deletions 06. PlusMinus/solution.cpp
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;
}
2 changes: 1 addition & 1 deletion 06. PlusMinus/PlusMinus.py → 06. PlusMinus/solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
Topic : Algorithms
Subtopic : Plus Minus
Language : Python
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.
Expand Down
32 changes: 32 additions & 0 deletions 07. Staircase/solution.cpp
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;
}

2 changes: 2 additions & 0 deletions 07. Staircase/Staircase.py → 07. Staircase/solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
Topic : Algorithms
Subtopic : StairCase
Language : Python
Problem Statement : Write a program that prints a staircase of size 'n'.
Url : https://www.hackerrank.com/challenges/staircase/problem
'''
Expand All @@ -16,6 +17,7 @@
def staircase(n):
for i in range(1, n + 1):
print(f'{"#"*i:>{n}}')

if __name__ == '__main__':
n = int(input())

Expand Down
89 changes: 89 additions & 0 deletions 08. Mini-MaxSum/solution.cpp
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;
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''
Topic : Algorithms
Subtopic :MiniMax Sum
Language : Python
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.
Expand All @@ -21,6 +21,7 @@
def miniMaxSum(arr):
arr = sorted(arr)
print(sum(arr[:-1]),sum(arr[1:]))

if __name__ == '__main__':
arr = list(map(int, input().rstrip().split()))

Expand Down
Loading

0 comments on commit 7a92ee7

Please sign in to comment.