diff --git a/Interview Preparation Kit - CPP/01. Warm-up Challenges/001. Sock Merchant.cpp b/Interview Preparation Kit - CPP/01. Warm-up Challenges/001. Sock Merchant.cpp new file mode 100644 index 00000000..09f14c1f --- /dev/null +++ b/Interview Preparation Kit - CPP/01. Warm-up Challenges/001. Sock Merchant.cpp @@ -0,0 +1,25 @@ +// Problem: https://www.hackerrank.com/challenges/sock-merchant/problem +// Score: 10 + + +#include + + +int main() { + int n, element; + int arr[101] = {0}; + int ans = 0; + + std::cin >> n; + + for (int i = 0; i < n; i++) { + std::cin >> element; + arr[element]++; + + ans += arr[element] / 2; + arr[element] = arr[element] % 2; + } + + std::cout << ans << std::endl; + return 0; +} diff --git a/Interview Preparation Kit - CPP/01. Warm-up Challenges/002. Counting Valleys.cpp b/Interview Preparation Kit - CPP/01. Warm-up Challenges/002. Counting Valleys.cpp new file mode 100644 index 00000000..758a31c0 --- /dev/null +++ b/Interview Preparation Kit - CPP/01. Warm-up Challenges/002. Counting Valleys.cpp @@ -0,0 +1,34 @@ +// Problem: https://www.hackerrank.com/challenges/counting-valleys/problem +// Score: 15 + +#include +#include +using namespace std; + +int main() { + int n; + cin >> n; + + string str; + cin >> str; + + int ans = 0; + + int current_level = 0; + + for (char c: str){ + if (c == 'D') { + if (current_level == 0){ + ans++; + } + + current_level--; + } + + else if (c == 'U') current_level++; + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/01. Warm-up Challenges/003. Jumping on the Clouds.cpp b/Interview Preparation Kit - CPP/01. Warm-up Challenges/003. Jumping on the Clouds.cpp new file mode 100644 index 00000000..bfaf7a9c --- /dev/null +++ b/Interview Preparation Kit - CPP/01. Warm-up Challenges/003. Jumping on the Clouds.cpp @@ -0,0 +1,34 @@ +// Problem: https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem +// Score: 20 + + +#include +#include +using namespace std; + +int main(){ + int n; + cin >> n; + + int element; + vector arr; + + while (cin >> element){ + arr.push_back(element); + } + + int ans = 0; + int position = 0; + + while (position < n - 1){ + position ++; + if (position + 1 <= n - 1 && arr[position + 1] != 1){ + position ++; + } + ans ++; + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/01. Warm-up Challenges/004. Repeated String.cpp b/Interview Preparation Kit - CPP/01. Warm-up Challenges/004. Repeated String.cpp new file mode 100644 index 00000000..5a64d611 --- /dev/null +++ b/Interview Preparation Kit - CPP/01. Warm-up Challenges/004. Repeated String.cpp @@ -0,0 +1,32 @@ +// Problem: https://www.hackerrank.com/challenges/repeated-string/problem +// Score: 20 + + +#include +#include + +using namespace std; + + +long long count(string const & str){ + long long ans = 0; + for (const char & c: str){ + ans += c == 'a'; + } + return ans; +} + + +int main(){ + string str; + cin >> str; + + long long n; + cin >> n; + + long long ans = 0; + ans = n / str.size() * count(str) + count(str.substr(0, n % str.size())); + + cout << ans; + return 0; +} diff --git a/Interview Preparation Kit - CPP/01. Warm-up Challenges/CMakeLists.txt b/Interview Preparation Kit - CPP/01. Warm-up Challenges/CMakeLists.txt new file mode 100644 index 00000000..8fcc02e5 --- /dev/null +++ b/Interview Preparation Kit - CPP/01. Warm-up Challenges/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 3.8) + +project(warm-up) + +add_executable(task-1 "001. Sock Merchant.cpp") +add_executable(task-2 "002. Counting Valleys.cpp") +add_executable(task-3 "003. Jumping on the Clouds.cpp") +add_executable(task-4 "004. Repeated String.cpp") diff --git a/Interview Preparation Kit - CPP/02. Arrays/001. 2D Array - DS.cpp b/Interview Preparation Kit - CPP/02. Arrays/001. 2D Array - DS.cpp new file mode 100644 index 00000000..2e4ef6cc --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/001. 2D Array - DS.cpp @@ -0,0 +1,38 @@ +// Problem: https://www.hackerrank.com/challenges/2d-array/problem +// Score: 15 + + +#include +using namespace std; + +int hourglass_sum(int arr[6][6], int i, int j){ + int ans = 0; + ans = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + + arr[i + 1][j + 1] + + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]; + return ans; +} + + +int main(){ + + int arr[6][6]; + + for (auto & i : arr){ + for (int & j : i){ + cin >> j; + } + } + + int ans = -1000; + for (int i = 0; i < 4; i++){ + for (int j = 0; j < 4; j++){ + if (hourglass_sum(arr, i, j) > ans){ + ans = hourglass_sum(arr, i, j); + } + } + } + + cout << ans; + return 0; +} diff --git a/Interview Preparation Kit - CPP/02. Arrays/002. Arrays - Left Rotation.cpp b/Interview Preparation Kit - CPP/02. Arrays/002. Arrays - Left Rotation.cpp new file mode 100644 index 00000000..1595386c --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/002. Arrays - Left Rotation.cpp @@ -0,0 +1,27 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem +// Score: 20 + + +#include +#include +using namespace std; + +int main(){ + int n, d; + + cin >> n; + cin >> d; + + vector arr; + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + + for (int i = 0; i < n; i++){ + cout << arr[(i + d) % n] << " "; + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/02. Arrays/003. New Year Chaos.cpp b/Interview Preparation Kit - CPP/02. Arrays/003. New Year Chaos.cpp new file mode 100644 index 00000000..6c073993 --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/003. New Year Chaos.cpp @@ -0,0 +1,56 @@ +// Problem: https://www.hackerrank.com/challenges/new-year-chaos/problem +// Score: 40 + +#include +#include +using namespace std; + + +bool check_sorted(vector & arr){ + bool ans = true; + for (int i = 0; i < arr.size() - 1; i++){ + if (arr[i] > arr[i + 1]){ + ans = false; + } + } + return ans; +} + + +int main(){ + int t; + cin >> t; + + for (int i = 0; i < t; i++){ + int n; + cin >> n; + vector arr; + + for (int j = 0; j < n; j++){ + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + + int ans = 0; + + for (int j = 0; j < 2; j++){ + for (int k = arr.size() - 1; k > 0; k--){ + if (arr[k] < arr[k - 1]){ + swap(arr[k], arr[k - 1]); + ans++; + } + } + } + + if (check_sorted(arr)){ + cout << ans << endl; + } + else { + cout << "Too chaotic" << endl; + } + + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/02. Arrays/004. Minimum Swaps 2.cpp b/Interview Preparation Kit - CPP/02. Arrays/004. Minimum Swaps 2.cpp new file mode 100644 index 00000000..dbe45816 --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/004. Minimum Swaps 2.cpp @@ -0,0 +1,36 @@ +// Problem: https://www.hackerrank.com/challenges/minimum-swaps-2/problem +// Score: 40 + + +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + vector arr; + + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + arr.push_back(tmp - 1); + } + + int i = 0; + int ans = 0; + while (i < n){ + if (arr[i] != i){ + swap(arr[i], arr[arr[i]]); + ans++; + } + else{ + i++; + } + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/02. Arrays/005. Array Manipulation.cpp b/Interview Preparation Kit - CPP/02. Arrays/005. Array Manipulation.cpp new file mode 100644 index 00000000..99ca243b --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/005. Array Manipulation.cpp @@ -0,0 +1,36 @@ +// Problem: https://www.hackerrank.com/challenges/crush/problem +// Score: 60 + + +#include +#include +using namespace std; + + +int main(){ + int n, m; + cin >> n >> m; + + vector arr(n, 0); + + for (int i = 0; i < m; i++){ + int start, finish, value; + cin >> start >> finish >> value; + arr[start - 1] += value; + arr[finish] -= value; + } + + long long int ans = 0; + long long int current = 0; + + for (int value: arr){ + current += value; + if (current > ans){ + ans = current; + } + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/02. Arrays/CMakeLists.txt b/Interview Preparation Kit - CPP/02. Arrays/CMakeLists.txt new file mode 100644 index 00000000..61060a89 --- /dev/null +++ b/Interview Preparation Kit - CPP/02. Arrays/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 3.8) + +project(arrays) + +add_executable(task-1 001.\ 2D\ Array\ -\ DS.cpp) +add_executable(task-2 002.\ Arrays\ -\ Left\ Rotation.cpp) +add_executable(task-3 003.\ New\ Year\ Chaos.cpp) +add_executable(task-4 004.\ Minimum\ Swaps\ 2.cpp) +add_executable(task-5 005.\ Array\ Manipulation.cpp) diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.cpp b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.cpp new file mode 100644 index 00000000..e777fc35 --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.cpp @@ -0,0 +1,48 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-ransom-note/problem +// Score: 25 + +#include +#include +#include +using namespace std; + + +int main(){ + int m, n; + cin >> m >> n; + + map magazine; + + for (int i = 0; i < m; i++) { + string word; + cin >> word; + + if (magazine.find(word) == magazine.end()){ + magazine[word] = 1; + } + else { + magazine[word]++; + } + } + + + bool ans = true; + for (int i = 0; i < n; i++){ + string word; + cin >> word; + + if (magazine.find(word) == magazine.end() || magazine[word] == 0){ + ans = false; + break; + } + else { + magazine[word]--; + } + } + + + if (ans) cout << "Yes" << endl; + else cout << "No" << endl; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/002. Two Strings.cpp b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/002. Two Strings.cpp new file mode 100644 index 00000000..720b0630 --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/002. Two Strings.cpp @@ -0,0 +1,38 @@ +// Problem: https://www.hackerrank.com/challenges/two-strings/problem +// Score: 25 + + +#include +#include +#include +using namespace std; + + +int main(){ + int p; + cin >> p; + + for (int i = 0; i < p; i++){ + string s1, s2; + cin >> s1 >> s2; + + set letters; + + for (char c: s1){ + letters.insert(c); + } + + bool ans = false; + for (char c: s2){ + if (letters.find(c) != letters.end()){ + ans = true; + break; + } + } + + if (ans) cout << "YES" << endl; + else cout << "NO" << endl; + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/003. Sherlock and Anagrams.cpp b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/003. Sherlock and Anagrams.cpp new file mode 100644 index 00000000..f8c664b1 --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/003. Sherlock and Anagrams.cpp @@ -0,0 +1,45 @@ +// Problem: https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem +// Score: 50 + + +#include +#include +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + for (int t = 0; t < n; t++){ + string str; + cin >> str; + + map lib; + + for (int i = 1; i < str.size(); i++){ + for (int j = 0; j < str.size() - i + 1; j++){ + string tmp = str.substr(j, i); + sort(tmp.begin(), tmp.end()); + + if (lib.find(tmp) != lib.end()){ + lib[tmp]++; + } + else { + lib[tmp] = 1; + } + + } + } + + int ans = 0; + for (auto const & el: lib){ + ans += el.second * (el.second - 1) / 2; + } + + cout << ans << endl; + } + + return 0; +} \ No newline at end of file diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/004. Count Triplets.cpp b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/004. Count Triplets.cpp new file mode 100644 index 00000000..218ed8ed --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/004. Count Triplets.cpp @@ -0,0 +1,35 @@ +// Problem: https://www.hackerrank.com/challenges/count-triplets-1/problem +// Score: 25 + + +#include +#include +using namespace std; + + +struct custom_int{ + long long int value = 0; +}; + + +int main(){ + int n, r; + cin >> n >> r; + map arr2; + map arr3; + + long long int ans = 0; + + for (int i = 0; i < n; i++) { + int tmp; + cin >> tmp; + + ans += arr3[tmp].value; + arr3[tmp * r].value += arr2[tmp].value; + arr2[tmp * r].value++; + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/005. Frequency Queries.cpp b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/005. Frequency Queries.cpp new file mode 100644 index 00000000..6da94f4f --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/005. Frequency Queries.cpp @@ -0,0 +1,66 @@ +// Problem: https://www.hackerrank.com/challenges/frequency-queries/problem +// Score: 40 + + +#include +using namespace std; + + +struct default_long_long_int{ + int value = 0; +}; + + +struct custom_struct{ +public: + void insert(int num){ + arr[num].value++; + frequencies[arr[num].value].value++; + frequencies[arr[num].value - 1].value--; + } + + void remove(int num){ + if (arr[num].value != 0){ + arr[num].value--; + frequencies[arr[num].value].value++; + frequencies[arr[num].value + 1].value--; + } + + if (arr[num].value == 0){ + arr.erase(num); + } + }; + + int check(int value){ + if (frequencies[value].value != 0) return 1; + else return 0; + } + +private: + map arr; + map frequencies; +}; + + +int main(){ + int q; + scanf("%d",&q); + custom_struct arr; + + for (int i = 0; i < q; i++){ + int command, value; + scanf("%d",&command); + scanf("%d",&value); + + if (command == 1){ + arr.insert(value); + } + if (command == 2){ + arr.remove(value); + } + if (command == 3){ + printf("%d \n", arr.check(value)); + } + } + return 0; +} diff --git a/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/CMakeLists.txt b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/CMakeLists.txt new file mode 100644 index 00000000..131ca8f7 --- /dev/null +++ b/Interview Preparation Kit - CPP/03. Dictionaries and Hashmaps/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 3.8) + +project(dictionaries-and-hashmaps) + +add_executable(task-1 001.\ Hash\ Tables\ -\ Ransom\ Note.cpp) +add_executable(task-2 002.\ Two\ Strings.cpp) +add_executable(task-3 003.\ Sherlock\ and\ Anagrams.cpp) +add_executable(task-4 004.\ Count\ Triplets.cpp) +add_executable(task-5 005.\ Frequency\ Queries.cpp) \ No newline at end of file diff --git a/Interview Preparation Kit - CPP/04. Sorting/003. Sorting - Comparator.cpp b/Interview Preparation Kit - CPP/04. Sorting/003. Sorting - Comparator.cpp new file mode 100644 index 00000000..91a96769 --- /dev/null +++ b/Interview Preparation Kit - CPP/04. Sorting/003. Sorting - Comparator.cpp @@ -0,0 +1,61 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-comparator-sorting +// Score: 35 + + +#include +#include +#include +#include +using namespace std; + +class player { +public: + player(string name, int score): name_(name), score_(score){} + + string get_name() const{ + return name_; + } + + int get_score() const{ + return score_; + } + + void print() const{ + cout << name_ << " " << score_ << '\n'; + } + +private: + string name_; + int score_; +}; + + +bool compare_players(player const & p1, player const & p2){ + if (p1.get_score() > p2.get_score()) return true; + else if (p1.get_score() == p2.get_score()) return p1.get_name() < p2.get_name(); + else return false; +} + + +int main(){ + int n; + cin >> n; + + vector arr; + + for (int i = 0; i < n; i++){ + string name; + int score; + cin >> name >> score; + player tmp(name, score); + arr.push_back(tmp); + } + + sort(arr.begin(), arr.end(), compare_players); + + for (player const & el: arr){ + el.print(); + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/04. Sorting/004. Fraudulent Activity Notifications.cpp b/Interview Preparation Kit - CPP/04. Sorting/004. Fraudulent Activity Notifications.cpp new file mode 100644 index 00000000..070b15e2 --- /dev/null +++ b/Interview Preparation Kit - CPP/04. Sorting/004. Fraudulent Activity Notifications.cpp @@ -0,0 +1,77 @@ +// Problem: https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem +// Score: 40 + + +#include +#include +using namespace std; + + +class moving_median{ +public: + + explicit moving_median(int target_size): target_size(target_size){} + + void add(int value){ + if (current_size < target_size){ + current_size++; + } + else { + check_operation(value); + arr[q.front()]--; + q.pop(); + } + + arr[value]++; + q.push(value); + } + + int get_n_notifications(){ + return n_notifications; + } + + +private: + void check_operation(int value){ + if (value >= two_medians()) n_notifications++; + }; + + + int find_i_element(int num){ + int count = 0; + for (int i = 0; i < 201; i++){ + count += arr[i]; + if (count >= num) return i; + } + return 200; + }; + + + int two_medians(){ + return (find_i_element(target_size / 2 + 1) + find_i_element(target_size / 2 + target_size % 2)); + }; + + + int n_notifications = 0; + int target_size; + int current_size = 0; + int arr[201] = {0}; + queue q; +}; + + +int main(){ + int n, d; + cin >> n >> d; + + moving_median data(d); + + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + data.add(tmp); + } + + cout << data.get_n_notifications(); + return 0; +} diff --git a/Interview Preparation Kit - CPP/04. Sorting/CMakeLists.txt b/Interview Preparation Kit - CPP/04. Sorting/CMakeLists.txt new file mode 100644 index 00000000..a2a6ee18 --- /dev/null +++ b/Interview Preparation Kit - CPP/04. Sorting/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required (VERSION 3.8) + +project(sorting) + +add_executable(task-3 003.\ Sorting\ -\ Comparator.cpp) +add_executable(task-4 004.\ Fraudulent\ Activity\ Notifications.cpp) \ No newline at end of file diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/001. Strings - Making Anagrams.cpp b/Interview Preparation Kit - CPP/05. String Manipulation/001. Strings - Making Anagrams.cpp new file mode 100644 index 00000000..21f0b7d6 --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/001. Strings - Making Anagrams.cpp @@ -0,0 +1,47 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-making-anagrams/problem +// Score: 25 + + +#include +#include +#include +using namespace std; + + +int main(){ + string s1, s2; + cin >> s1 >> s2; + + sort(s1.begin(), s1.end()); + sort(s2.begin(), s2.end()); + + int i = 0; + int j = 0; + int ans = 0; + while (i < s1.size() || j < s2.size()){ + if (i >= s1.size()){ + j++; + ans++; + } + else if (j >= s2.size()){ + i++; + ans++; + } + else if (s1[i] == s2[j]){ + i++; + j++; + } + else if (s1[i] < s2[j]){ + ans++; + i++; + } + else if (s2[j] < s1[i]){ + ans++; + j++; + } + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/002. Alternating Characters.cpp b/Interview Preparation Kit - CPP/05. String Manipulation/002. Alternating Characters.cpp new file mode 100644 index 00000000..e248381f --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/002. Alternating Characters.cpp @@ -0,0 +1,33 @@ +// Problem: https://www.hackerrank.com/challenges/alternating-characters/problem +// Score: 20 + + +#include +#include +using namespace std; + + +int main(){ + int t; + cin >> t; + + for (int i = 0; i < t; i++) { + string str; + cin >> str; + + + char prev = '0'; + int ans = 0; + + for (char c: str){ + if (c == prev) { + ans++; + } + prev = c; + } + + cout << ans << '\n'; + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/003. Sherlock and the Valid String.cpp b/Interview Preparation Kit - CPP/05. String Manipulation/003. Sherlock and the Valid String.cpp new file mode 100644 index 00000000..2757d175 --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/003. Sherlock and the Valid String.cpp @@ -0,0 +1,50 @@ +// Problem: https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem +// Score: 35 + + +#include +#include +#include +using namespace std; + + +struct default_zero_int{ + int val = 0; +}; + + +int main(){ + string str; + cin >> str; + + map arr; + + for (char c: str){ + arr[c].val++; + } + + map counts; + + for (auto el: arr){ + counts[el.second.val].val++; + } + + if (counts.size() <= 1){ + cout << "YES"; + } + else if (counts.size() > 2){ + cout << "NO"; + } + else { + auto el1 = *counts.begin(); + auto el2 = *(next(counts.begin(), 1)); + + if (((el1.first == 1 && el1.second.val == 1) || (el2.first == 1 && el2.second.val == 1)) || + ((el1.second.val == 1 || el2.second.val == 1) && (abs(el1.first - el2.first) == 1))){ + cout << "YES"; + } + else cout << "NO"; + } + + return 0; +} diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/004. Special String Again.cpp b/Interview Preparation Kit - CPP/05. String Manipulation/004. Special String Again.cpp new file mode 100644 index 00000000..15ca577f --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/004. Special String Again.cpp @@ -0,0 +1,39 @@ +// Problem: https://www.hackerrank.com/challenges/special-palindrome-again/problem +// Score: 40 + + +#include +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + string str; + cin >> str; + + vector> arr; + arr.push_back(make_pair(str[0], 0)); + + for (char c: str){ + if (c == arr.back().first){ + arr.back().second++; + } + else { + arr.push_back(make_pair(c, 1)); + } + } + + int ans = 0; + for (int i = 0; i < arr.size(); i++){ + ans += (arr[i].second + 1) * arr[i].second / 2; + if ((i != 0) && (i != arr.size() - 1) && (arr[i].second == 1) && (arr[i - 1].first == arr[i + 1].first)){ + ans += min(arr[i - 1].second, arr[i + 1].second); + } + } + + cout << ans; + return 0; +} diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/005. Common Child.cpp b/Interview Preparation Kit - CPP/05. String Manipulation/005. Common Child.cpp new file mode 100644 index 00000000..732cf1ec --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/005. Common Child.cpp @@ -0,0 +1,31 @@ +// Problem: https://www.hackerrank.com/challenges/common-child/problem +// Score: 60 + + +#include +#include +#include +using namespace std; + + +int main(){ + string s1, s2; + cin >> s1 >> s2; + + vector> dp(s1.size() + 1, vector(s2.size() + 1, 0)); + + for (int i = 1; i < s1.size() + 1; i++){ + for (int j = 1; j < s2.size() + 1; j++){ + if (s1[i - 1] == s2[j - 1]){ + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else { + dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); + } + } + } + + cout << dp[s1.size()][s2.size()]; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/05. String Manipulation/CMakeLists.txt b/Interview Preparation Kit - CPP/05. String Manipulation/CMakeLists.txt new file mode 100644 index 00000000..e7326e12 --- /dev/null +++ b/Interview Preparation Kit - CPP/05. String Manipulation/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 3.8) + +project(string-manipulation) + +add_executable(task-1 001.\ Strings\ -\ Making\ Anagrams.cpp) +add_executable(task-2 002.\ Alternating\ Characters.cpp) +add_executable(task-3 003.\ Sherlock\ and\ the\ Valid\ String) +add_executable(task-4 004.\ Special\ String\ Again.cpp) +add_executable(task-5 005.\ Common\ Child.cpp) diff --git a/Interview Preparation Kit - CPP/06. Greedy Algorithms/001. Minimum Absolute Difference in an Array.cpp b/Interview Preparation Kit - CPP/06. Greedy Algorithms/001. Minimum Absolute Difference in an Array.cpp new file mode 100644 index 00000000..6513b09b --- /dev/null +++ b/Interview Preparation Kit - CPP/06. Greedy Algorithms/001. Minimum Absolute Difference in an Array.cpp @@ -0,0 +1,34 @@ +// Problem: https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem +// Score: 15 + + +#include +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + + vector arr; + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + + sort(arr.begin(), arr.end()); + + int ans = abs(arr[0] - arr[1]); + for (int i = 1; i < arr.size(); i++){ + if (abs(arr[i] - arr[i-1]) < ans){ + ans = abs(arr[i] - arr[i-1]); + } + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/06. Greedy Algorithms/002. Luck Balance.cpp b/Interview Preparation Kit - CPP/06. Greedy Algorithms/002. Luck Balance.cpp new file mode 100644 index 00000000..00b38f65 --- /dev/null +++ b/Interview Preparation Kit - CPP/06. Greedy Algorithms/002. Luck Balance.cpp @@ -0,0 +1,37 @@ +// Problem: https://www.hackerrank.com/challenges/luck-balance/problem +// Score: 20 + + +#include +#include +#include +using namespace std; + + +int main(){ + + int n, k; + cin >> n >> k; + + int ans = 0; + vector arr; + + for (int i = 0; i < n; i++){ + int l, t; + cin >> l >> t; + + if (t == 0) ans += l; + else arr.push_back(l); + } + + sort(arr.rbegin(), arr.rend()); + + for (int i = 0; i < arr.size(); i++){ + if (i < k) ans += arr[i]; + else ans -= arr[i]; + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/06. Greedy Algorithms/003. Greedy Florist.cpp b/Interview Preparation Kit - CPP/06. Greedy Algorithms/003. Greedy Florist.cpp new file mode 100644 index 00000000..2c25f470 --- /dev/null +++ b/Interview Preparation Kit - CPP/06. Greedy Algorithms/003. Greedy Florist.cpp @@ -0,0 +1,34 @@ +// Problem: https://www.hackerrank.com/challenges/greedy-florist/problem +// Score: 35 + + +#include +#include +#include +using namespace std; + + +int main(){ + int n, k; + cin >> n >> k; + vector arr; + + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + + sort(arr.rbegin(), arr.rend()); + + int ans = 0; + int mult = 0; + for (int i = 0; i < n; i++){ + if (i % k == 0) mult++; + ans += arr[i] * mult; + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/06. Greedy Algorithms/004. Max Min.cpp b/Interview Preparation Kit - CPP/06. Greedy Algorithms/004. Max Min.cpp new file mode 100644 index 00000000..0c82e7a2 --- /dev/null +++ b/Interview Preparation Kit - CPP/06. Greedy Algorithms/004. Max Min.cpp @@ -0,0 +1,32 @@ +// Problem: https://www.hackerrank.com/challenges/angry-children/problem +// Score: 35 + + +#include +#include +#include +using namespace std; + + +int main(){ + int n, k; + cin >> n >> k; + vector arr; + + for (int i = 0; i < n; i++){ + int tmp; + cin >> tmp; + arr.push_back(tmp); + } + + sort(arr.begin(), arr.end()); + + int ans = arr[k - 1] - arr[0]; + for (int i = 0; i < arr.size() - k + 1; i++){ + int tmp = arr[k - 1 + i] - arr[i]; + if (tmp < ans) ans = tmp; + } + + cout << ans; + return 0; +} diff --git a/Interview Preparation Kit - CPP/06. Greedy Algorithms/CMakeLists.txt b/Interview Preparation Kit - CPP/06. Greedy Algorithms/CMakeLists.txt new file mode 100644 index 00000000..543dabf0 --- /dev/null +++ b/Interview Preparation Kit - CPP/06. Greedy Algorithms/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 3.8) + +project(greedy-algorithms) + +add_executable(task-1 001.\ Minimum\ Absolute\ Difference\ in\ an\ Array.cpp) +add_executable(task-2 002.\ Luck\ Balance.cpp) +add_executable(task-3 003.\ Greedy\ Florist.cpp) +add_executable(task-4 004.\ Max\ Min.cpp) + diff --git a/Interview Preparation Kit - CPP/07. Search/001. Hash Tables Ice Cream Parlor.cpp b/Interview Preparation Kit - CPP/07. Search/001. Hash Tables Ice Cream Parlor.cpp new file mode 100644 index 00000000..2428e792 --- /dev/null +++ b/Interview Preparation Kit - CPP/07. Search/001. Hash Tables Ice Cream Parlor.cpp @@ -0,0 +1,32 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem +// Score: 35 + + +#include +#include +using namespace std; + + +int main(){ + int t = 0; + cin >> t; + + for (int i = 0; i < t; i++){ + int money, n; + cin >> money >> n; + map arr; + + for (int j = 0; j < n; j++){ + int c; + cin >> c; + + if (arr.find(c) == arr.end()){ + arr[money - c] = j + 1; + } + else{ + cout << arr[c] << " " << j + 1 << '\n'; + } + } + } + return 0; +} diff --git a/Interview Preparation Kit - CPP/07. Search/CMakeLists.txt b/Interview Preparation Kit - CPP/07. Search/CMakeLists.txt new file mode 100644 index 00000000..6eb4d3f1 --- /dev/null +++ b/Interview Preparation Kit - CPP/07. Search/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required (VERSION 3.8) + +project(greedy-algorithms) + +add_executable(task-1 001.\ Hash\ Tables\ Ice\ Cream\ Parlor.cpp) + diff --git a/Interview Preparation Kit - CPP/08. Dynamic Programming/001. Max Array Sum.cpp b/Interview Preparation Kit - CPP/08. Dynamic Programming/001. Max Array Sum.cpp new file mode 100644 index 00000000..78995425 --- /dev/null +++ b/Interview Preparation Kit - CPP/08. Dynamic Programming/001. Max Array Sum.cpp @@ -0,0 +1,29 @@ +// Problem: https://www.hackerrank.com/challenges/max-array-sum/problem +// Score: 20 + + +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + vector arr(n); + + for (int i = 0; i < n; i++){ + cin >> arr[i]; + } + + vector dp(n); + dp[0] = arr[0]; + dp[1] = max(arr[0], arr[1]); + for (int i = 2; i < n; i++){ + dp[i] = max(max(dp[i-2] + arr[i], dp[i - 1]), arr[i]); + } + + cout << dp[n - 1]; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/08. Dynamic Programming/002. Abbreviation.cpp b/Interview Preparation Kit - CPP/08. Dynamic Programming/002. Abbreviation.cpp new file mode 100644 index 00000000..958febb2 --- /dev/null +++ b/Interview Preparation Kit - CPP/08. Dynamic Programming/002. Abbreviation.cpp @@ -0,0 +1,49 @@ +// Problem: https://www.hackerrank.com/challenges/abbr/problem +// Score: 40 + + +#include +#include +#include +using namespace std; + + +int main(){ + int q; + cin >> q; + + for (int k = 0; k < q; k++){ + string s1, s2; + cin >> s1 >> s2; + + vector> dp (s2.size() + 1, vector (s1.size() + 1, false)); + + dp[0][0] = true; + + for (int i = 1; i < s2.size() + 1; i++){ + dp[i][0] = false; + } + + for (int i = 1; i < s1.size() + 1; i++){ + dp[0][i] = (islower(s1[i - 1])) && (dp[0][i - 1] == true); + } + + for (int i = 1; i < s2.size() + 1; i++){ + for (int j = 1; j < s1.size() + 1; j++){ + if (islower(s1[j-1])){ + dp[i][j] = (dp[i - 1][j - 1] && s2[i - 1] == toupper(s1[j - 1])) || dp[i][j - 1]; + } + else { + dp[i][j] = dp[i - 1][j - 1] && s2[i - 1] == s1[j - 1]; + } + + } + } + + if (dp[s2.size()][s1.size()]) cout << "YES" << endl; + else cout << "NO" << endl; + } + return 0; +} + + diff --git a/Interview Preparation Kit - CPP/08. Dynamic Programming/003. Candies.cpp b/Interview Preparation Kit - CPP/08. Dynamic Programming/003. Candies.cpp new file mode 100644 index 00000000..7b565558 --- /dev/null +++ b/Interview Preparation Kit - CPP/08. Dynamic Programming/003. Candies.cpp @@ -0,0 +1,39 @@ +// Problem: https://www.hackerrank.com/challenges/candies/problem +// Score: 50 + + +#include +#include +using namespace std; + + +int main(){ + int n; + cin >> n; + vector arr(n); + vector up (n, 1); + vector down (n, 1); + + for (int i = 0; i < n; i++){ + cin >> arr[i]; + } + + for (int i = 1; i < n; i++){ + if (arr[i] > arr[i - 1]){ + up[i] = up[i - 1] + 1; + } + + if (arr[n - i - 1] > arr[n - i]){ + down[n - i - 1] = down[n - i] + 1; + } + } + + long long int ans = 0; + for (int i = 0; i < n; i++){ + ans += max(up[i], down[i]); + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/08. Dynamic Programming/CMakeLists.txt b/Interview Preparation Kit - CPP/08. Dynamic Programming/CMakeLists.txt new file mode 100644 index 00000000..d58cc3cd --- /dev/null +++ b/Interview Preparation Kit - CPP/08. Dynamic Programming/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 3.8) + +project(dynamic-programming) + +add_executable(task-1 001.\ Max\ Array\ Sum.cpp) +add_executable(task-2 "002. Abbreviation.cpp") +add_executable(task-3 003.\ Candies.cpp) + diff --git a/Interview Preparation Kit - CPP/09. Stacks and Queues/001. Balanced Brackets.cpp b/Interview Preparation Kit - CPP/09. Stacks and Queues/001. Balanced Brackets.cpp new file mode 100644 index 00000000..2c2734d9 --- /dev/null +++ b/Interview Preparation Kit - CPP/09. Stacks and Queues/001. Balanced Brackets.cpp @@ -0,0 +1,44 @@ +// Problem: https://www.hackerrank.com/challenges/balanced-brackets/problem +// Score: 25 + + +#include +#include +using namespace std; + + +int main(){ + int t; + cin >> t; + + for (int i = 0; i < t; i++){ + string str; + cin >> str; + vector brackets; + + bool ans = true; + + for (char c: str){ + if (c == ')' || c == ']' || c == '}'){ + if ((!brackets.empty()) && ( + (c == ')' && brackets.back() == '(') || + (c == ']' && brackets.back() == '[') || + (c == '}' && brackets.back() == '{'))){ + brackets.pop_back(); + } + else { + ans = false; + break; + } + } + else brackets.push_back(c); + } + + if (ans && brackets.empty()) cout << "YES" << endl; + else cout << "NO" << endl; + } + + + return 0; +} + diff --git a/Interview Preparation Kit - CPP/09. Stacks and Queues/003. Largest Rectangle.cpp b/Interview Preparation Kit - CPP/09. Stacks and Queues/003. Largest Rectangle.cpp new file mode 100644 index 00000000..230aa6f1 --- /dev/null +++ b/Interview Preparation Kit - CPP/09. Stacks and Queues/003. Largest Rectangle.cpp @@ -0,0 +1,58 @@ +// Problem: https://www.hackerrank.com/challenges/largest-rectangle/problem +// Score: 50 + + +#include +#include +using namespace std; + + +void update_ans(long long int & ans, int i, vector & positions, vector & heights){ + long long int square; + square = heights.back() * (i - positions.back()); + ans = max(ans, square); + heights.pop_back(); + positions.pop_back(); +} + + +int main (){ + int n; + cin >> n; + + vector arr(n); + for (int i = 0; i < n; i++){ + cin >> arr[i]; + } + + vector positions; + vector heights; + + long long int ans = 0; + + for (int i = 0; i < n; i++){ + if (heights.empty() || arr[i] > heights.back()){ + heights.push_back(arr[i]); + positions.push_back(i); + } + + else if (arr[i] < heights.back()){ + int last_position = positions.back(); + + while (!heights.empty() && heights.back() > arr[i]){ + last_position = positions.back(); + update_ans(ans, i, positions, heights); + } + positions.push_back(last_position); + heights.push_back(arr[i]); + } + } + + while (!heights.empty()){ + update_ans(ans, n, positions, heights); + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/09. Stacks and Queues/CMakeLists.txt b/Interview Preparation Kit - CPP/09. Stacks and Queues/CMakeLists.txt new file mode 100644 index 00000000..e0427d7e --- /dev/null +++ b/Interview Preparation Kit - CPP/09. Stacks and Queues/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required (VERSION 3.8) + +project(stacks-and-queues) + + +add_executable(task-1 "001. Balanced Brackets.cpp") +add_executable(task-3 "003. Largest Rectangle.cpp") diff --git a/Interview Preparation Kit - CPP/10. Graphs/003. BFS - Shortest Reach in a Graph.cpp b/Interview Preparation Kit - CPP/10. Graphs/003. BFS - Shortest Reach in a Graph.cpp new file mode 100644 index 00000000..13b08f28 --- /dev/null +++ b/Interview Preparation Kit - CPP/10. Graphs/003. BFS - Shortest Reach in a Graph.cpp @@ -0,0 +1,66 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-bfs-shortest-reach/problem +// Score: 45 + + +#include +#include +#include +using namespace std; + + +struct node{ + bool visited = false; + int path = -1; + vector kids; +}; + + +int main(){ + int t; + cin >> t; + for (int k = 0; k < t; k++){ + int n, m; + cin >> n >> m; + + vector nodes(n); + for (int i = 0; i < n; i++){ + nodes[i] = new node(); + } + + for (int i = 0; i < m; i++){ + int a, b; + cin >> a >> b; + nodes[a-1]->kids.push_back(nodes[b-1]); + nodes[b-1]->kids.push_back(nodes[a-1]); + } + + int start; + cin >> start; + + deque q; + q.push_back(nodes[start - 1]); + + int path = 0; + q.front()->path = 0; + q.front()->visited = true; + + while (!q.empty()){ + path = q.front()->path + 6; + + for (node * el: q.front()->kids){ + if (!el->visited){ + el->path = path; + el->visited = true; + q.push_back(el); + } + } + q.pop_front(); + } + + for (node* el: nodes){ + if (el->path != 0) cout << el->path << " "; + } + cout << '\n'; + } + return 0; +} \ No newline at end of file diff --git a/Interview Preparation Kit - CPP/10. Graphs/004. DFS - Connected Cell in a Grid.cpp b/Interview Preparation Kit - CPP/10. Graphs/004. DFS - Connected Cell in a Grid.cpp new file mode 100644 index 00000000..c3b5ad3b --- /dev/null +++ b/Interview Preparation Kit - CPP/10. Graphs/004. DFS - Connected Cell in a Grid.cpp @@ -0,0 +1,57 @@ +// Problem: https://www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid/problem +// Score: 45 + + +#include +#include +using namespace std; + + +void count_nodes(vector> const & arr, vector> & visited, int i, int j, int & count){ + if (visited[i][j]) return; + + visited[i][j] = true; + if (arr[i][j] == 0) return; + if (i != 0) count_nodes(arr, visited, i - 1, j, count); + if (i != 0 && j != 0) count_nodes(arr, visited, i - 1, j - 1, count); + if (i != 0 && j != arr[0].size() - 1) count_nodes(arr, visited, i - 1, j + 1, count); + if (j != 0) count_nodes(arr, visited, i, j - 1, count); + if (j != arr[0].size() - 1) count_nodes(arr, visited, i, j + 1, count); + if (i != arr.size() - 1) count_nodes(arr, visited, i + 1, j, count); + if (i != arr.size() - 1 && j != 0) count_nodes(arr, visited, i + 1, j - 1, count); + if (i != arr.size() - 1 && j != arr[0].size() - 1) count_nodes(arr, visited, i + 1, j + 1, count); + count++; +} + + +int main(){ + int n, m; + cin >> n >> m; + + vector> arr(n, vector(m)); + + for (int i = 0; i < n; i++){ + for (int j = 0; j < m; j++){ + cin >> arr[i][j]; + } + } + + vector> visited(n, vector(m, false)); + + + int ans = 0; + + for (int i = 0; i < n; i++){ + for (int j = 0; j < m; j++){ + int count = 0; + count_nodes(arr, visited, i, j, count); + if (count > ans){ + ans = count; + } + } + } + + cout << ans; + + return 0; +} diff --git a/Interview Preparation Kit - CPP/10. Graphs/CMakeLists.txt b/Interview Preparation Kit - CPP/10. Graphs/CMakeLists.txt new file mode 100644 index 00000000..d9213a22 --- /dev/null +++ b/Interview Preparation Kit - CPP/10. Graphs/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required (VERSION 3.8) + +project(graphs) + + +add_executable(task-3 "003. BFS - Shortest Reach in a Graph.cpp") +add_executable(task-4 "004. DFS - Connected Cell in a Grid.cpp") diff --git a/Interview Preparation Kit - CPP/11. Trees/001. Tree - Height of a Binary Tree.cpp b/Interview Preparation Kit - CPP/11. Trees/001. Tree - Height of a Binary Tree.cpp new file mode 100644 index 00000000..2e64ff50 --- /dev/null +++ b/Interview Preparation Kit - CPP/11. Trees/001. Tree - Height of a Binary Tree.cpp @@ -0,0 +1,11 @@ +// Problem: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem +// Score: 10 + + +int height(Node *root) { + if ((root == nullptr) || (root->left == nullptr && root->right == nullptr)) { + return 0; + } else { + return max(height(root->left), height(root->right)) + 1; + } +} diff --git a/Interview Preparation Kit - CPP/11. Trees/CMakeLists.txt b/Interview Preparation Kit - CPP/11. Trees/CMakeLists.txt new file mode 100644 index 00000000..962ec206 --- /dev/null +++ b/Interview Preparation Kit - CPP/11. Trees/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required (VERSION 3.8) + +project(trees) + + +add_executable(task-1 "001. Tree - Height of a Binary Tree.cpp") diff --git a/Interview Preparation Kit - Python/01. Warm-up Challenges/001. Sock Merchant.py b/Interview Preparation Kit - Python/01. Warm-up Challenges/001. Sock Merchant.py new file mode 100644 index 00000000..d6dfb923 --- /dev/null +++ b/Interview Preparation Kit - Python/01. Warm-up Challenges/001. Sock Merchant.py @@ -0,0 +1,18 @@ +# Problem: https://www.hackerrank.com/challenges/sock-merchant/problem +# Score: 10 + + +def sock_merchant(ar): + list_of_socks = [0 for i in range(101)] + for element in ar: + list_of_socks[element] = list_of_socks[element] + 1 + ans = 0 + for sock in list_of_socks: + ans += sock // 2 + return ans + + +n = int(input().strip()) +ar = list(map(int, input().strip().split(' '))) +result = sock_merchant(ar) +print(result) diff --git a/Interview Preparation Kit - Python/01. Warm-up Challenges/002. Counting Valleys.py b/Interview Preparation Kit - Python/01. Warm-up Challenges/002. Counting Valleys.py new file mode 100644 index 00000000..4e7749a9 --- /dev/null +++ b/Interview Preparation Kit - Python/01. Warm-up Challenges/002. Counting Valleys.py @@ -0,0 +1,21 @@ +# Problem: https://www.hackerrank.com/challenges/counting-valleys/problem +# Score: 15 + + +def counting_valleys(n, s): + current_level = 0 + count = 0 + for i in range(n): + if s[i] == 'U': + current_level += 1 + elif s[i] == 'D': + current_level -= 1 + if current_level == -1: + count += 1 + return count + + +n = int(input().strip()) +s = input().strip() +result = counting_valleys(n, s) +print(result) diff --git a/Interview Preparation Kit - Python/01. Warm-up Challenges/003. Jumping on the Clouds.py b/Interview Preparation Kit - Python/01. Warm-up Challenges/003. Jumping on the Clouds.py new file mode 100644 index 00000000..bc967cda --- /dev/null +++ b/Interview Preparation Kit - Python/01. Warm-up Challenges/003. Jumping on the Clouds.py @@ -0,0 +1,17 @@ +# Problem: https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem +# Score: 20 + + +def jumping_on_clouds(n, c): + c.append(0) + ans = 0 + position = 0 + while position < n-1: + position += (c[position+2] == 0) + 1 + ans += 1 + return ans + + +n = int(input()) +c = list(map(int, input().rstrip().split())) +print(jumping_on_clouds(n, c)) diff --git a/Interview Preparation Kit - Python/01. Warm-up Challenges/004. Repeated String.py b/Interview Preparation Kit - Python/01. Warm-up Challenges/004. Repeated String.py new file mode 100644 index 00000000..18a0f6c6 --- /dev/null +++ b/Interview Preparation Kit - Python/01. Warm-up Challenges/004. Repeated String.py @@ -0,0 +1,11 @@ +# Problem: https://www.hackerrank.com/challenges/repeated-string/problem +# Score: 20 + + +def repeated_string(s, n): + return n // len(s) * s.count('a') + s[0: n % len(s)].count('a') + + +s = input() +n = int(input()) +print(repeated_string(s, n)) diff --git a/Interview Preparation Kit - Python/02. Arrays/001. Arrays - Left Rotation.py b/Interview Preparation Kit - Python/02. Arrays/001. Arrays - Left Rotation.py new file mode 100644 index 00000000..88e69778 --- /dev/null +++ b/Interview Preparation Kit - Python/02. Arrays/001. Arrays - Left Rotation.py @@ -0,0 +1,12 @@ +# Problem: https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem +# Score: 20 + + +def array_left_rotation(a, k): + result = a[k:] + a[:k] + return result + + +n, k = map(int, input().strip().split(' ')) +a = list(map(int, input().strip().split(' '))) +print(*array_left_rotation(a, k)) diff --git a/Interview Preparation Kit - Python/02. Arrays/002. 2D Array - DS.py b/Interview Preparation Kit - Python/02. Arrays/002. 2D Array - DS.py new file mode 100644 index 00000000..1304e93b --- /dev/null +++ b/Interview Preparation Kit - Python/02. Arrays/002. 2D Array - DS.py @@ -0,0 +1,17 @@ +# Problem: https://www.hackerrank.com/challenges/2d-array/problem +# Score: 15 + + +def hourglasses(arr, i, j): + return sum(arr[i][j:j+3]) + arr[i+1][j+1] + sum(arr[i+2][j:j+3]) + + +arr = [] +for i in range(6): + arr.append(list(map(int, input().split()))) + +ans = hourglasses(arr, 0, 0) +for i in range(4): + for j in range(4): + ans = max(ans, hourglasses(arr, i, j)) +print(ans) diff --git a/Interview Preparation Kit - Python/02. Arrays/003. New Year Chaos.py b/Interview Preparation Kit - Python/02. Arrays/003. New Year Chaos.py new file mode 100644 index 00000000..25d8cbdb --- /dev/null +++ b/Interview Preparation Kit - Python/02. Arrays/003. New Year Chaos.py @@ -0,0 +1,20 @@ +# Problem: https://www.hackerrank.com/challenges/new-year-chaos/problem +# Score: 40 + + +t = int(input()) +for test in range(t): + n = int(input()) + arr = list(map(int, input().split())) + count = 0 + + for i in range(2): + for j in range(len(arr) - 1, 0, -1): + if arr[j] < arr[j-1]: + arr[j], arr[j-1] = arr[j-1], arr[j] + count += 1 + + if arr == sorted(arr): + print(count) + else: + print('Too chaotic') diff --git a/Interview Preparation Kit - Python/02. Arrays/004. Minimum Swaps 2.py b/Interview Preparation Kit - Python/02. Arrays/004. Minimum Swaps 2.py new file mode 100644 index 00000000..39f7049f --- /dev/null +++ b/Interview Preparation Kit - Python/02. Arrays/004. Minimum Swaps 2.py @@ -0,0 +1,17 @@ +# Problem: https://www.hackerrank.com/challenges/minimum-swaps-2/problem +# Score: 40 + + +n = int(input()) +arr = list(map(int, input().split())) +count = 0 + +i = 0 +while i < len(arr): + if arr[i] != i + 1: + arr[arr[i] - 1], arr[i] = arr[i], arr[arr[i] - 1] + count += 1 + else: + i += 1 + +print(count) diff --git a/Interview Preparation Kit - Python/02. Arrays/005. Array Manipulation.py b/Interview Preparation Kit - Python/02. Arrays/005. Array Manipulation.py new file mode 100644 index 00000000..24ebe546 --- /dev/null +++ b/Interview Preparation Kit - Python/02. Arrays/005. Array Manipulation.py @@ -0,0 +1,21 @@ +# Problem: https://www.hackerrank.com/challenges/crush/problem +# Score: 60 + + +n, queries = map(int, input().split()) + +arr = [0 for i in range(n+2)] + +for i in range(queries): + start, finish, k = map(int, input().split()) + arr[start - 1] += k + arr[finish] -= k + +ans = 0 +current = 0 +for i in arr: + current += i + if current > ans: + ans = current + +print(ans) diff --git a/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.py b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.py new file mode 100644 index 00000000..4ce33742 --- /dev/null +++ b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/001. Hash Tables - Ransom Note.py @@ -0,0 +1,18 @@ +# Problem: https://www.hackerrank.com/challenges/ctci-ransom-note/problem +# Score: 25 + + +from collections import Counter + + +def checkMagazine(magazine, note): + if Counter(note) - Counter(magazine) == {}: + return 'Yes' + else: + return 'No' + + +m, n = map(int, input().split()) +magazine = list(input().split()) +note = list(input().split()) +print(checkMagazine(magazine, note)) diff --git a/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/002. Two Strings.py b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/002. Two Strings.py new file mode 100644 index 00000000..d295a539 --- /dev/null +++ b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/002. Two Strings.py @@ -0,0 +1,11 @@ +# Problem: https://www.hackerrank.com/challenges/two-strings/problem +# Score: 25 + + +for i in range(int(input())): + str1 = set(input()) + str2 = set(input()) + if str1 & str2 != set(): + print('YES') + else: + print('NO') diff --git a/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/003. Count Triplets.py b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/003. Count Triplets.py new file mode 100644 index 00000000..f3c30d24 --- /dev/null +++ b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/003. Count Triplets.py @@ -0,0 +1,21 @@ +# Problem: https://www.hackerrank.com/challenges/count-triplets-1/problem +# Score: 25 + + +from collections import defaultdict + + +def count_triplets(arr, r): + arr2 = defaultdict(int) + arr3 = defaultdict(int) + count = 0 + for i in arr: + count += arr3[i] + arr3[i*r] += arr2[i] + arr2[i*r] += 1 + return count + + +n, r = map(int, input().split()) +arr = list(map(int, input().split())) +print(count_triplets(arr, r)) diff --git a/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/004. Frequency Queries.py b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/004. Frequency Queries.py new file mode 100644 index 00000000..b2621d83 --- /dev/null +++ b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/004. Frequency Queries.py @@ -0,0 +1,25 @@ +# Problem: https://www.hackerrank.com/challenges/frequency-queries/problem +# Score: 40 + + +from collections import defaultdict + + +arr = defaultdict(int) +frequencies = defaultdict(int) +result = [] +for i in range(int(input())): + command, value = map(int, input().split()) + if command == 1: + arr[value] += 1 + frequencies[arr[value]] += 1 + frequencies[arr[value] - 1] -= 1 + if command == 2 and arr[value] != 0: + arr[value] -= 1 + frequencies[arr[value]] += 1 + frequencies[arr[value] + 1] -= 1 + if command == 3: + result.append(1 if frequencies[value] > 0 else 0) + +for i in result: + print(i) diff --git a/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/005. Sherlock and Anagrams.py b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/005. Sherlock and Anagrams.py new file mode 100644 index 00000000..0ee173c4 --- /dev/null +++ b/Interview Preparation Kit - Python/03. Dictionaries and Hashmaps/005. Sherlock and Anagrams.py @@ -0,0 +1,20 @@ +# Problem: https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem +# Score: 50 + + +from collections import defaultdict + + +for t in range(int(input())): + s = input() + substrings = defaultdict(int) + + for i in range(1, len(s)): + for j in range(len(s) - i + 1): + substrings[''.join(sorted(s[j:j+i]))] += 1 + + ans = 0 + for key, value in substrings.items(): + ans += value*(value-1) // 2 + + print(ans) diff --git a/Interview Preparation Kit - Python/04. Sorting/001. Sorting - Bubble Sort.py b/Interview Preparation Kit - Python/04. Sorting/001. Sorting - Bubble Sort.py new file mode 100644 index 00000000..375d4e9d --- /dev/null +++ b/Interview Preparation Kit - Python/04. Sorting/001. Sorting - Bubble Sort.py @@ -0,0 +1,18 @@ +# Problem: https://www.hackerrank.com/challenges/ctci-bubble-sort/problem +# Score: 30 + + +n = int(input()) +a = list(map(int, input().split())) + +count = 0 + +for i in range(n): + for j in range(n-1): + if a[j] > a[j+1]: + a[j], a[j+1] = a[j+1], a[j] + count += 1 + +print('Array is sorted in', count, 'swaps.') +print('First Element:', a[0]) +print('Last Element:', a[-1]) diff --git a/Interview Preparation Kit - Python/04. Sorting/002. Mark and Toys.py b/Interview Preparation Kit - Python/04. Sorting/002. Mark and Toys.py new file mode 100644 index 00000000..996397da --- /dev/null +++ b/Interview Preparation Kit - Python/04. Sorting/002. Mark and Toys.py @@ -0,0 +1,16 @@ +# Problem: https://www.hackerrank.com/challenges/mark-and-toys/problem +# Score: 35 + + +n, k = map(int, input().split()) +prices = sorted(list(map(int, input().split()))) + +count = 0 +total_sum = 0 +for i in prices: + total_sum += i + if total_sum > k: + break + else: + count += 1 +print(count) diff --git a/Interview Preparation Kit - Python/07. Search/001. Hash Tables - Ice Cream Parlor.py b/Interview Preparation Kit - Python/07. Search/001. Hash Tables - Ice Cream Parlor.py new file mode 100644 index 00000000..73e541a9 --- /dev/null +++ b/Interview Preparation Kit - Python/07. Search/001. Hash Tables - Ice Cream Parlor.py @@ -0,0 +1,16 @@ +# Problem: https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem +# Score: 35 + + +t = int(input()) +for i in range(t): + money = int(input()) + n = int(input()) + arr = list(map(int, input().split())) + + saved_values = {} + for counter, value in enumerate(arr): + if money-value in saved_values: + print(saved_values[money-value] + 1, counter + 1) + elif value not in saved_values: + saved_values[value] = counter diff --git a/Interview Preparation Kit - Python/07. Search/002. Minimum Time Required.py b/Interview Preparation Kit - Python/07. Search/002. Minimum Time Required.py new file mode 100644 index 00000000..c97992a0 --- /dev/null +++ b/Interview Preparation Kit - Python/07. Search/002. Minimum Time Required.py @@ -0,0 +1,22 @@ +# Problem: https://www.hackerrank.com/challenges/minimum-time-required/problem +# Score: 35 + + +import math + + +def minimum_time(goal, machines): + min_days = math.ceil(goal / (len(machines) / min(machines))) + max_days = math.ceil(goal / (len(machines) / max(machines))) + while min_days < max_days: + day = (min_days + max_days) // 2 + if sum(day // i for i in machines) >= goal: + max_days = day + else: + min_days = day + 1 + return min_days + + +n, goal = map(int, input().split()) +machines = list(map(int, input().split())) +print(minimum_time(goal, machines)) diff --git a/Interview Preparation Kit - Python/07. Search/003. Triple sum.py b/Interview Preparation Kit - Python/07. Search/003. Triple sum.py new file mode 100644 index 00000000..e222a5b2 --- /dev/null +++ b/Interview Preparation Kit - Python/07. Search/003. Triple sum.py @@ -0,0 +1,26 @@ +# Problem: https://www.hackerrank.com/challenges/triple-sum/problem +# Score: 40 + + +def binary_search_last(arr, el, low, high): + if low + 1 >= high: + if arr[low] > el: + return 0 + else: + return low + 1 + middle = (low + high) // 2 + if arr[middle] > el: + return binary_search_last(arr, el, low, middle) + else: + return binary_search_last(arr, el, middle, high) + + +len_a, len_b, len_c = map(int, input().split()) +a = sorted(set(map(int, input().split()))) +b = sorted(list(set(map(int, input().split())))) +c = sorted(list(set(map(int, input().split())))) + +ans = 0 +for i in set(b): + ans += binary_search_last(a, i, 0, len(a)) * binary_search_last(c, i, 0, len(c)) +print(ans) diff --git a/Interview Preparation Kit - Python/08. Dynamic Programming/001. Max Array Sum.py b/Interview Preparation Kit - Python/08. Dynamic Programming/001. Max Array Sum.py new file mode 100644 index 00000000..1912a746 --- /dev/null +++ b/Interview Preparation Kit - Python/08. Dynamic Programming/001. Max Array Sum.py @@ -0,0 +1,18 @@ +# Problem: https://www.hackerrank.com/challenges/max-array-sum/problem +# Score: 20 + + +def max_subset_sum(arr): + dp = list() + dp.append(arr[0]) + dp.append(max(arr[:2])) + ans = dp[-1] + for i in arr[2:]: + dp.append(max(i, dp[-2] + i, ans)) + ans = max(ans, dp[-1]) + return ans + + +n = int(input()) +arr = list(map(int, input().split())) +print(max_subset_sum(arr))