HackerRank Solution of Data Structure .Most of the question solution in c++ .I Will Try to Post very Question Solution that will help you to improve coding skill.For any query you can Ask me.
HackerRank Solution Arrays – DS in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main() { int n; scanf("%d",&n); int *arr = malloc(sizeof(int) * n); for(int arr_i = 0; arr_i < n; arr_i++) scanf("%d",&arr[arr_i]); for(int arr_i = n-1; arr_i >=0; arr_i--) printf("%d ",arr[arr_i]); return 0; }
HackerRank Solution 2D Array – DS in C
#include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> char* readline(); char** split_string(char*); // Complete the hourglassSum function below. int hourglassSum(int arr_rows, int arr_columns, int** arr) { int i,j,sum,max=-500; for(i=1;i<5;i++) { sum=0; for(j=1;j<5;j++) { sum= arr[i-1][j-1]+arr[i-1][j]+arr[i-1][j+1]+arr[i][j]+arr[i+1][j-1]+arr[i+1][j]+arr[i+1][j+1]; if(sum>max) max=sum; } } return max; } int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); int** arr = malloc(6 * sizeof(int*)); for (int i = 0; i < 6; i++) { *(arr + i) = malloc(6 * (sizeof(int))); char** arr_item_temp = split_string(readline()); for (int j = 0; j < 6; j++) { char* arr_item_endptr; char* arr_item_str = *(arr_item_temp + j); int arr_item = strtol(arr_item_str, &arr_item_endptr, 10); if (arr_item_endptr == arr_item_str || *arr_item_endptr != '\0') { exit(EXIT_FAILURE); } *(*(arr + i) + j) = arr_item; } } int arr_rows = 6; int arr_columns = 6; int result = hourglassSum(arr_rows, arr_columns, arr); fprintf(fptr, "%d\n", result); fclose(fptr); return 0; } char* readline() { size_t alloc_length = 1024; size_t data_length = 0; char* data = malloc(alloc_length); while (true) { char* cursor = data + data_length; char* line = fgets(cursor, alloc_length - data_length, stdin); if (!line) { break; } data_length += strlen(cursor); if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } size_t new_length = alloc_length << 1; data = realloc(data, new_length); if (!data) { break; } alloc_length = new_length; } if (data[data_length - 1] == '\n') { data[data_length - 1] = '\0'; } data = realloc(data, data_length); return data; } char** split_string(char* str) { char** splits = NULL; char* token = strtok(str, " "); int spaces = 0; while (token) { splits = realloc(splits, sizeof(char*) * ++spaces); if (!splits) { return splits; } splits[spaces - 1] = token; token = strtok(NULL, " "); } return splits; }
HackerRank Solution Left Rotation Array – DS in C++
#include <bits/stdc++.h> using namespace std; vector<string> split_string(string); int main() { string nd_temp; getline(cin, nd_temp); vector<string> nd = split_string(nd_temp); int n = stoi(nd[0]); int d = stoi(nd[1]); string a_temp_temp; getline(cin, a_temp_temp); vector<string> a_temp = split_string(a_temp_temp); vector<int> a(n); for (int i = 0; i < n; i++) { int a_item = stoi(a_temp[i]); a[i] = a_item; } vector <int> b(n); for(int i=0;i<n;i++) cout<<a[(i+d)%n]<<" "; 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; }
HackerRank Solution Sparse Arrays – DS
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String s[]=new String[n]; for(int i=0;i<n;i++) s[i]=sc.next(); int a=sc.nextInt(); String q[]=new String[a]; for(int i=0;i<a;i++) { int x=0; q[i]=sc.next(); for(int j=0;j<n;j++) { if(s[j].equals(q[i])) x++; } System.out.println(x); } } }
HackerRank Solution Array Manipulation – DS
#include <bits/stdc++.h> using namespace std; vector<string> split_string(string); // Complete the arrayManipulation function below. long arrayManipulation(int n, vector<vector<int>> queries) { long max=LONG_MIN,sum=0; vector<long> a(n+1, 0); for(int i=0;i<queries.size();i++) { a[queries[i][0]]+=queries[i][2]; if(queries[i][1]+1<=n) a[queries[i][1]+1]-=queries[i][2]; } for(int i=1;i<=n;i++) { sum+=a[i]; if(sum>max) max=sum; } return max; } int main() { ofstream fout(getenv("OUTPUT_PATH")); string nm_temp; getline(cin, nm_temp); vector<string> nm = split_string(nm_temp); int n = stoi(nm[0]); int m = stoi(nm[1]); vector<vector<int>> queries(m); for (int i = 0; i < m; i++) { queries[i].resize(3); for (int j = 0; j < 3; j++) { cin >> queries[i][j]; } cin.ignore(numeric_limits<streamsize>::max(), '\n'); } long result = arrayManipulation(n, queries); fout << result << "\n"; fout.close(); 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; }