diff --git a/03. CompareTheTriplet/solution.cpp b/03. CompareTheTriplet/solution.cpp index 0686870..86bd1e9 100644 --- a/03. CompareTheTriplet/solution.cpp +++ b/03. CompareTheTriplet/solution.cpp @@ -35,7 +35,6 @@ vector compareTriplets(vector a, vector b) { result.push_back(aliceScore); result.push_back(bobScore); return result; - } int main() diff --git a/04. AVeryBigSum/solution.py b/04. AVeryBigSum/solution.py index 203f405..f6f291c 100644 --- a/04. AVeryBigSum/solution.py +++ b/04. AVeryBigSum/solution.py @@ -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') diff --git a/05. DiagonalDifference/solution.cpp b/05. DiagonalDifference/solution.cpp index dceae21..1883e6f 100644 --- a/05. DiagonalDifference/solution.cpp +++ b/05. DiagonalDifference/solution.cpp @@ -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 + +using namespace std; + +string ltrim(const string &); +string rtrim(const string &); +vector 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> arr) { + + int s1 = 0; + int s2 = 0; + int n = arr.size(); + + for(int i=0;i> 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 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(isspace))) + ); + + return s; +} + +string rtrim(const string &str) { + string s(str); + + s.erase( + find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), + s.end() + ); + + return s; +} + +vector split(const string &str) { + vector 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; +} diff --git a/06. PlusMinus/solution.cpp b/06. PlusMinus/solution.cpp new file mode 100644 index 0000000..3c42833 --- /dev/null +++ b/06. PlusMinus/solution.cpp @@ -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 + +using namespace std; + +vector split_string(string); + +// Complete the plusMinus function below. +void plusMinus(vector arr) { + + float countPositive = 0; + float countNegative = 0; + float n = arr.size(); + + for(int i;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::max(), '\n'); + + string arr_temp_temp; + getline(cin, arr_temp_temp); + + vector arr_temp = split_string(arr_temp_temp); + + vector 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 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 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; +} diff --git a/06. PlusMinus/PlusMinus.py b/06. PlusMinus/solution.py similarity index 97% rename from 06. PlusMinus/PlusMinus.py rename to 06. PlusMinus/solution.py index 899945b..7979b16 100644 --- a/06. PlusMinus/PlusMinus.py +++ b/06. PlusMinus/solution.py @@ -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. diff --git a/07. Staircase/solution.cpp b/07. Staircase/solution.cpp new file mode 100644 index 0000000..9e735ab --- /dev/null +++ b/07. Staircase/solution.cpp @@ -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 + +using namespace std; + +// Complete the staircase function below. +void staircase(int n) { + + for(int i=0;i> n; + cin.ignore(numeric_limits::max(), '\n'); + + staircase(n); + + return 0; +} + diff --git a/07. Staircase/Staircase.py b/07. Staircase/solution.py similarity index 95% rename from 07. Staircase/Staircase.py rename to 07. Staircase/solution.py index aea1253..d76f332 100644 --- a/07. Staircase/Staircase.py +++ b/07. Staircase/solution.py @@ -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 ''' @@ -16,6 +17,7 @@ def staircase(n): for i in range(1, n + 1): print(f'{"#"*i:>{n}}') + if __name__ == '__main__': n = int(input()) diff --git a/08. Mini-MaxSum/solution.cpp b/08. Mini-MaxSum/solution.cpp new file mode 100644 index 0000000..2f2a9b4 --- /dev/null +++ b/08. Mini-MaxSum/solution.cpp @@ -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 + +using namespace std; + +vector split_string(string); + +// Complete the miniMaxSum function below. +void miniMaxSum(vector 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 arr_temp = split_string(arr_temp_temp); + + vector 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 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 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; +} + diff --git a/08. Mini-MaxSum/Mini-MaxSum.py b/08. Mini-MaxSum/solution.py similarity index 96% rename from 08. Mini-MaxSum/Mini-MaxSum.py rename to 08. Mini-MaxSum/solution.py index 090cf90..3fd0179 100644 --- a/08. Mini-MaxSum/Mini-MaxSum.py +++ b/08. Mini-MaxSum/solution.py @@ -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. @@ -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())) diff --git a/09. BirthdayCakeCandles/solution.cpp b/09. BirthdayCakeCandles/solution.cpp new file mode 100644 index 0000000..0120aae --- /dev/null +++ b/09. BirthdayCakeCandles/solution.cpp @@ -0,0 +1,94 @@ +/* + Topic : Algorithms + Subtopic : Birthday Cake Candles + Language : C++ + Problem Statement : + You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out. + + Url : https://www.hackerrank.com/challenges/birthday-cake-candles/problem +*/ + +#include + +using namespace std; + +vector split_string(string); + +// Complete the birthdayCakeCandles function below. +int birthdayCakeCandles(vector ar) { + + int max = ar[0]; + int count = 0; + int n = ar.size(); + + for(int i=0;i max) { + max = ar[i]; + } + } + for (int i=0;i> ar_count; + cin.ignore(numeric_limits::max(), '\n'); + + string ar_temp_temp; + getline(cin, ar_temp_temp); + + vector ar_temp = split_string(ar_temp_temp); + + vector ar(ar_count); + + for (int i = 0; i < ar_count; i++) { + int ar_item = stoi(ar_temp[i]); + + ar[i] = ar_item; + } + + int result = birthdayCakeCandles(ar); + + fout << result << "\n"; + + fout.close(); + + return 0; +} + +vector 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 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; +} diff --git a/09. BirthdayCakeCandles/BirthdayCakeCandles.py b/09. BirthdayCakeCandles/solution.py similarity index 97% rename from 09. BirthdayCakeCandles/BirthdayCakeCandles.py rename to 09. BirthdayCakeCandles/solution.py index 26d7876..e0afd14 100644 --- a/09. BirthdayCakeCandles/BirthdayCakeCandles.py +++ b/09. BirthdayCakeCandles/solution.py @@ -1,6 +1,7 @@ ''' Topic : Algorithms Subtopic : Birthday Cake Candles + Language ; Python Problem Statement : You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out. diff --git a/10. TimeConversion/solution.cpp b/10. TimeConversion/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/10. TimeConversion/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/10. TimeConversion/TimeConversion.py b/10. TimeConversion/solution.py similarity index 100% rename from 10. TimeConversion/TimeConversion.py rename to 10. TimeConversion/solution.py diff --git a/11. GradingStudents/solution.cpp b/11. GradingStudents/solution.cpp new file mode 100644 index 0000000..e69de29 diff --git a/11. GradingStudents/GradingStudents.py b/11. GradingStudents/solution.py similarity index 100% rename from 11. GradingStudents/GradingStudents.py rename to 11. GradingStudents/solution.py diff --git a/12. Kangaroo/solution.cpp b/12. Kangaroo/solution.cpp new file mode 100644 index 0000000..e69de29 diff --git a/12. Kangaroo/Kangaroo.py b/12. Kangaroo/solution.py similarity index 100% rename from 12. Kangaroo/Kangaroo.py rename to 12. Kangaroo/solution.py diff --git a/13. AppleAndOrange/solution.cpp b/13. AppleAndOrange/solution.cpp new file mode 100644 index 0000000..e69de29 diff --git a/13. AppleAndOrange/AppleAndOrange.py b/13. AppleAndOrange/solution.py similarity index 100% rename from 13. AppleAndOrange/AppleAndOrange.py rename to 13. AppleAndOrange/solution.py diff --git a/14. BetweenTwoSets/solution.cpp b/14. BetweenTwoSets/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/14. BetweenTwoSets/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/14. BetweenTwoSets/solution.py b/14. BetweenTwoSets/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/14. BetweenTwoSets/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/15. BreakingTheRecords/solution.cpp b/15. BreakingTheRecords/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/15. BreakingTheRecords/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/15. BreakingTheRecords/solution.py b/15. BreakingTheRecords/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/15. BreakingTheRecords/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/16. BirthdayChocolate/solution.cpp b/16. BirthdayChocolate/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/16. BirthdayChocolate/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/16. BirthdayChocolate/solution.py b/16. BirthdayChocolate/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/16. BirthdayChocolate/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/17.DivisibleSumPairs/solution.cpp b/17.DivisibleSumPairs/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/17.DivisibleSumPairs/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/17.DivisibleSumPairs/solution.py b/17.DivisibleSumPairs/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/17.DivisibleSumPairs/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/18. MigratoryBirds/solution.cpp b/18. MigratoryBirds/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/18. MigratoryBirds/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/18. MigratoryBirds/solution.py b/18. MigratoryBirds/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/18. MigratoryBirds/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/19. DayOfTheProgrammer/solution.cpp b/19. DayOfTheProgrammer/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/19. DayOfTheProgrammer/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/19. DayOfTheProgrammer/solution.py b/19. DayOfTheProgrammer/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/19. DayOfTheProgrammer/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file diff --git a/20. BonAppetit/solution.cpp b/20. BonAppetit/solution.cpp new file mode 100644 index 0000000..91b0e7e --- /dev/null +++ b/20. BonAppetit/solution.cpp @@ -0,0 +1,7 @@ +/* + Topic : Algorithms + Subtopic : + Language : C++ + Problem Statement : + Url : +*/ \ No newline at end of file diff --git a/20. BonAppetit/solution.py b/20. BonAppetit/solution.py new file mode 100644 index 0000000..acc5308 --- /dev/null +++ b/20. BonAppetit/solution.py @@ -0,0 +1,9 @@ +''' + Topic : Algorithms + Subtopic : + Language : Python + Problem Statement : + + + Url : +''' \ No newline at end of file