Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interview Preparation kit added #291

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Problem: https://www.hackerrank.com/challenges/sock-merchant/problem
// Score: 10


#include <iostream>


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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Problem: https://www.hackerrank.com/challenges/counting-valleys/problem
// Score: 15

#include <iostream>
#include <string>
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Problem: https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem
// Score: 20


#include <iostream>
#include <vector>
using namespace std;

int main(){
int n;
cin >> n;

int element;
vector<int> 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Problem: https://www.hackerrank.com/challenges/repeated-string/problem
// Score: 20


#include <iostream>
#include <string>

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;
}
Original file line number Diff line number Diff line change
@@ -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")
38 changes: 38 additions & 0 deletions Interview Preparation Kit - CPP/02. Arrays/001. 2D Array - DS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Problem: https://www.hackerrank.com/challenges/2d-array/problem
// Score: 15


#include <iostream>
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Problem: https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem
// Score: 20


#include <iostream>
#include <vector>
using namespace std;

int main(){
int n, d;

cin >> n;
cin >> d;

vector<int> 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;
}
56 changes: 56 additions & 0 deletions Interview Preparation Kit - CPP/02. Arrays/003. New Year Chaos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Problem: https://www.hackerrank.com/challenges/new-year-chaos/problem
// Score: 40

#include <iostream>
#include <vector>
using namespace std;


bool check_sorted(vector<int> & 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<int> 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Problem: https://www.hackerrank.com/challenges/minimum-swaps-2/problem
// Score: 40


#include <iostream>
#include <vector>
using namespace std;


int main(){
int n;
cin >> n;
vector<int> 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Problem: https://www.hackerrank.com/challenges/crush/problem
// Score: 60


#include <iostream>
#include <vector>
using namespace std;


int main(){
int n, m;
cin >> n >> m;

vector<long long int> 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;
}
9 changes: 9 additions & 0 deletions Interview Preparation Kit - CPP/02. Arrays/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Problem: https://www.hackerrank.com/challenges/ctci-ransom-note/problem
// Score: 25

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main(){
int m, n;
cin >> m >> n;

map<string, int> 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;
}
Loading