-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from swapnanildutta/master
update
- Loading branch information
Showing
69 changed files
with
2,053 additions
and
53 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Comparison Sorting | ||
Quicksort usually has a running time of , but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison sorts, i.e. they sort a list just by comparing the elements to one another. A comparison sort algorithm cannot beat (worst-case) running time, since represents the minimum number of comparisons needed to know where to place each element. For more details, you can see these notes (PDF). | ||
Alternative Sorting | ||
Another sorting method, the counting sort, does not require comparison. Instead, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times. | ||
For example, consider an array . All of the values are in the range , so create an array of zeroes, . The results of each iteration follow: | ||
i arr[i] result | ||
0 1 [0, 1, 0, 0] | ||
1 1 [0, 2, 0, 0] | ||
2 3 [0, 2, 0, 1] | ||
3 2 [0, 2, 1, 1] | ||
4 1 [0, 3, 1, 1] | ||
Now we can print the list of occurrences, or determine the sorted array: . | ||
Challenge | ||
Given a list of integers, count and output the number of times each value appears as a list of space-separated integers. | ||
Function Description | ||
Complete the countingSort function in the editor below. It should return an array of integers where each value is the number of occurrences of the element's index value in the original array. | ||
countingSort has the following parameter(s): | ||
arr: an array of integers | ||
Input Format | ||
The first line contains an integer , the number of items in . | ||
Each of the next lines contains an integer where . | ||
Constraints | ||
Output Format | ||
Output the number of times every number from through appears in as a list of space-separated integers on one line. | ||
Sample Input | ||
100 | ||
63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33 | ||
Sample Output | ||
0 2 0 2 0 0 1 0 1 2 1 0 1 1 0 0 2 0 1 0 1 2 1 1 1 3 0 2 0 0 2 0 3 3 1 0 0 0 0 2 2 1 1 1 2 0 2 0 1 0 1 0 0 1 0 0 2 1 0 1 1 1 0 1 0 1 0 2 1 3 2 0 0 2 1 2 1 0 2 2 1 2 1 2 1 1 2 2 0 3 2 1 1 0 1 1 1 0 2 2 | ||
Explanation | ||
Each of the resulting values represents the number of times appeared in . | ||
*/ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
vector<string> split_string(string); | ||
|
||
// Complete the countingSort function below. | ||
vector<int> countingSort(vector<int> arr) { | ||
|
||
vector<int>count(100,0); | ||
|
||
for(int i:arr) | ||
count[i]++; | ||
|
||
return count; | ||
} | ||
|
||
int main() | ||
{ | ||
ofstream fout(getenv("OUTPUT_PATH")); | ||
|
||
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; | ||
} | ||
|
||
vector<int> result = countingSort(arr); | ||
|
||
for (int i = 0; i < result.size(); i++) { | ||
fout << result[i]; | ||
|
||
if (i != result.size() - 1) { | ||
fout << " "; | ||
} | ||
} | ||
|
||
fout << "\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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'''# Problem: https://www.hackerrank.com/challenges/tutorial-intro/problem | ||
# Score: 30 | ||
About Tutorial Challenges | ||
Many of the challenges on HackerRank are difficult and assume that you already know the relevant algorithms. These tutorial challenges are different. They break down algorithmic concepts into smaller challenges so that you can learn the algorithm by solving them. They are intended for those who already know some programming, however. You could be a student majoring in computer science, a self-taught programmer, or an experienced developer who wants an active algorithms review. Here's a great place to learn by doing! | ||
The first series of challenges covers sorting. They are listed below: | ||
Tutorial Challenges - Sorting | ||
Insertion Sort challenges | ||
Insertion Sort 1 - Inserting | ||
Insertion Sort 2 - Sorting | ||
Correctness and loop invariant | ||
Running Time of Algorithms | ||
Quicksort challenges | ||
Quicksort 1 - Partition | ||
Quicksort 2 - Sorting | ||
Quicksort In-place (advanced) | ||
Running time of Quicksort | ||
Counting sort challenges | ||
Counting Sort 1 - Counting | ||
Counting Sort 2 - Simple sort | ||
Counting Sort 3 - Preparing | ||
Full Counting Sort (advanced) | ||
There will also be some challenges where you'll get to apply what you've learned using the completed algorithms. | ||
About the Challenges | ||
Each challenge will describe a scenario and you will code a solution. As you progress through the challenges, you will learn some important concepts in algorithms. In each challenge, you will receive input on STDIN and you will need to print the correct output to STDOUT. | ||
There may be time limits that will force you to make your code efficient. If you receive a "Terminated due to time out" message when you submit your solution, you'll need to reconsider your method. If you want to test your code locally, each test case can be downloaded, inputs and expected results, using hackos. You earn hackos as you solve challenges, and you can spend them on these tests. | ||
For many challenges, helper methods (like an array) will be provided for you to process the input into a useful format. You can use these methods to get started with your program, or you can write your own input methods if you want. Your code just needs to print the right output to each test case. | ||
Sample Challenge | ||
This is a simple challenge to get things started. Given a sorted array () and a number (), can you print the index location of in the array? | ||
For example, if and , you would print for a zero-based index array. | ||
If you are going to use the provided code for I/O, this next section is for you. | ||
Function Description | ||
Complete the introTutorial function in the editor below. It must return an integer representing the zero-based index of . | ||
introTutorial has the following parameter(s): | ||
arr: a sorted array of integers | ||
V: an integer to search for | ||
The next section describes the input format. You can often skip it, if you are using included methods or code stubs. | ||
Input Format | ||
The first line contains an integer, , a value to search for. | ||
The next line contains an integer, , the size of . The last line contains space-separated integers, each a value of where . | ||
Output Format | ||
Output the index of in the array. | ||
The next section describes the constraints and ranges of the input. You should check this section to know the range of the input. | ||
Constraints | ||
It is guaranteed that will occur in exactly once. | ||
This "sample" shows the first input test case. It is often useful to go through the sample to understand a challenge. | ||
Sample Input 0 | ||
4 | ||
6 | ||
1 4 5 7 9 12 | ||
Sample Output 0 | ||
1 | ||
Explanation 0 | ||
. The value is the element in the array, but its index is since in this case, array indices start from (see array definition under Input Format). | ||
''' | ||
v = int(input()) | ||
n = int(input()) | ||
arr = list(map(int, input().split())) | ||
i = 0 | ||
while arr[i] != v: | ||
i += 1 | ||
print(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'''# Problem: https://www.hackerrank.com/challenges/marcs-cakewalk/problem | ||
# Score: 15 | ||
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and Marc can walk a distance to expend those calories. If Marc has eaten cupcakes so far, after eating a cupcake with calories he must walk at least miles to maintain his weight. | ||
For example, if he eats cupcakes with calorie counts in the following order: , the miles he will need to walk are . This is not the minimum, though, so we need to test other orders of consumption. In this case, our minimum miles is calculated as . | ||
Given the individual calorie counts for each of the cupcakes, determine the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order. | ||
Function Description | ||
Complete the marcsCakewalk function in the editor below. It should return a long integer that represents the minimum miles necessary. | ||
marcsCakewalk has the following parameter(s): | ||
calorie: an integer array that represents calorie count for each cupcake | ||
Input Format | ||
The first line contains an integer , the number of cupcakes in . | ||
The second line contains space-separated integers . | ||
Constraints | ||
Output Format | ||
Print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. | ||
Sample Input 0 | ||
3 | ||
1 3 2 | ||
Sample Output 0 | ||
11 | ||
Explanation 0 | ||
Let's say the number of miles Marc must walk to maintain his weight is . He can minimize by eating the cupcakes in the following order: | ||
Eat the cupcake with calories, so . | ||
Eat the cupcake with calories, so . | ||
Eat the cupcake with calories, so . | ||
We then print the final value of , which is , as our answer. | ||
Sample Input 1 | ||
4 | ||
7 4 9 6 | ||
Sample Output 1 | ||
79 | ||
''' | ||
|
||
_ = input() | ||
calories = list(map(int, input().split())) | ||
calories = sorted(calories, reverse=True) | ||
ans = 0 | ||
for index, cupcake in enumerate(calories): | ||
ans += 2**index*cupcake | ||
print(ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'''# Problem: https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem | ||
# Score: 15 | ||
Consider an array of integers, . We define the absolute difference between two elements, and (where ), to be the absolute value of . | ||
Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array we can create pairs of numbers: and . The absolute differences for these pairs are , and . The minimum absolute difference is . | ||
Function Description | ||
Complete the minimumAbsoluteDifference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements. | ||
minimumAbsoluteDifference has the following parameter(s): | ||
n: an integer that represents the length of arr | ||
arr: an array of integers | ||
Input Format | ||
The first line contains a single integer , the size of . | ||
The second line contains space-separated integers . | ||
Constraints | ||
Output Format | ||
Print the minimum absolute difference between any two elements in the array. | ||
Sample Input 0 | ||
3 | ||
3 -7 0 | ||
Sample Output 0 | ||
3 | ||
Explanation 0 | ||
With integers in our array, we have three possible pairs: , , and . The absolute values of the differences between these pairs are as follows: | ||
Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible absolute differences is . | ||
Sample Input 1 | ||
10 | ||
-59 -36 -13 1 -53 -92 -2 -96 -54 75 | ||
Sample Output 1 | ||
1 | ||
Explanation 1 | ||
The smallest absolute difference is . | ||
Sample Input 2 | ||
5 | ||
1 -3 71 68 17 | ||
Sample Output 2 | ||
3 | ||
Explanation 2 | ||
The minimum absolute difference is ''' | ||
|
||
_ = input() | ||
arr = sorted(map(int, input().split())) | ||
diff = 2*10**9 | ||
for i in range(1, len(arr)): | ||
diff = min(diff, arr[i] - arr[i-1]) | ||
print(diff) |
Oops, something went wrong.