Saturday, 15 July 2017

Marc's Cakewalk Hacker Rank Problem Solution.


Problem:

Marc loves cupcakes, but he also likes to stay fit. He eats  cupcakes in one sitting, and each cupcake  has a calorie count, . After eating a cupcake with  calories, he must walk at least  (where  is the number cupcakes he has already eaten) miles to maintain his weight.
Given the individual calorie counts for each of the  cupcakes, find and print a long integer denoting the minimum number of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any order.
Input Format
The first line contains an integer, , denoting the number of cupcakes. 
The second line contains  space-separated integers describing the respective calorie counts of each cupcake, .
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:
  1. Eat the cupcake with  calories, so .
  2. Eat the cupcake with  calories, so .
  3. Eat the cupcake with  calories, so .
We then print the final value of , which is , as our answer.

Solution:

#include <bits/stdc++.h>
using namespace std;

int main() {
    long  long n,a,sum = 0;
    cin >> n;
    vector<long long> ar;
    for(long long i = 0; i < n; i++){
        cin >> a;
        ar.push_back(a);
    }
    sort(ar.begin(),ar.end());
    for(int i = ar.size() - 1,j = 0; i >= 0; i--,j++){
        sum += (ar[i] * pow(2,j));
    }
    cout << sum << endl;
    return 0;
}

Minimum Absolute Difference in an Array Hacker Rank Problem Solution.

Problem:

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.
Input Format
The first line contains a single integer denoting  (the number of integers). 
The second line contains  space-separated integers describing the respective values of .
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 , so we print  as our answer.
Solution:

#include <bits/stdc++.h>
using namespace std;


int main() {
    int a,n,mx = INT_MAX;
    vector<int> ar;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a;
        ar.push_back(a);
    }
    sort(ar.begin(),ar.end());
    for(int i = 0; i < ar.size() - 1; i++){
        if(ar[i+1]-ar[i] < mx)mx = ar[i+1]-ar[i];
    }
    cout << mx << endl;
    return 0;
}