Tuesday, August 30, 2016

Hackerrank - Combination Lock

Consider a -slot combination lock where each slot contains a dial numbered with the ten sequential decimal integers in the inclusive range from  to . In one operation, you can choose a slot and rotate the dial by one click, either in the positive direction (to increase the displayed number by ) or the negative direction (to decrease the displayed number by ). Note that, due to the cyclical nature of the dial, the next number after  is  and the number before  is ). For example, if the number  is currently displayed on the dial, you can rotate the dial to either  (positive direction) or  (negative direction) in a single operation.
combination lock
Given the initial configuration of numbers in each slot and some desired configuration of numbers, determine the minimum number of operations you must perform to change the lock's slots to the desired configuration.
Input Format
The first line contains  space-separated integers denoting the current slot configuration. 
The second line contains  space-separated integers denoting the desired slot configuration.
Constraints
  • Each number in a slot is .
Output Format
Print a single integer denoting the minimum number of moves to change this configuration to the correct one.
Sample Input
1 2 9 5 7
1 3 2 0 7  
Sample Output
9
Explanation
We perform the following operations on each slot:
  • Slot : We rotate the dial  times because this slot is already displaying the desired number (i.e., ).
  • Slot : We rotate the dial  time, changing it from .
  • Slot : We rotate the dial  times, changing it from .
  • Slot : We rotate the dial  times, changing it from .
  • Slot : We rotate the dial  times because this slot is already displaying the desired number (i.e., ).
Finally, we sum the number of operations performed at each slot: . Thus, we print  as our answer.
Solution:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int a[6];
  5. int b[6];
  6.  
  7. int main(){
  8.     int ans = 0;
  9.     for(int i = 0; i < 5; i++)scanf("%d"&a[i]);
  10.     for(int i = 0; i < 5; i++)scanf("%d"&b[i]);
  11.     for(int i = 0; i < 5; i++){
  12.         ans += min(abs(a[i]-b[i])10-abs(a[i]-b[i]));
  13.     }
  14.     printf("%d\n", ans);
  15.     return 0;
  16. }

Hackerrank - Bon Appétit

Anna and Brian order  items at a restaurant, but Anna declines to eat any of the  item (where ) due to an allergy. When the check comes, they decide to split the cost of all the items they shared; however, Brian may have forgotten that they didn't split the  item and accidentally charged Anna for it.
You are given , the cost of each of the  items, and the total amount of money that Brian charged Anna for her portion of the bill. If the bill is fairly split, print Bon Appetit; otherwise, print the amount of money that Brian must refund to Anna.
Input Format
The first line contains two space-separated integers denoting the respective values of  (the number of items ordered) and  (the -based index of the item that Anna did not eat). 
The second line contains  space-separated integers where each integer  denotes the cost, , of item (where ). 
The third line contains an integer, , denoting the amount of money that Brian charged Anna for her share of the bill.
Constraints
Output Format
If Brian did not overcharge Anna, print Bon Appetit on a new line; otherwise, print the difference (i.e., ) that Brian must refund to Anna (it is guaranteed that this will always be an integer).
Sample Input 0
4 1
3 10 2 9
12
Sample Output 0
5
Explanation 0 
Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is  and, split in half, the cost per person is . Brian charged her  for her portion of the bill, which is more than the  dollars worth of food that she actually shared with him. Thus, we print the amount Anna was overcharged, , on a new line.
Sample Input 1
4 1
3 10 2 9
7
Sample Output 1
Bon Appetit
Explanation 1 
Anna didn't eat item , but she shared the rest of the items with Brian. The total cost of the shared items is  and, split in half, the cost per person is . Because this matches the amount, , that Brian charged Anna for her portion of the bill, we print Bon Appetit on a new line.
Solution:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.     int n, k, x, anna, sum = 0;
  6.     scanf("%d"&n);
  7.     scanf("%d"&k);
  8.     for(int i = 0; i < n; i++){
  9.         scanf("%d"&x);
  10.         if(== i)continue;
  11.         sum += x;
  12.     }
  13.     sum /= 2;
  14.     scanf("%d"&anna);
  15.     if(sum == anna)puts("Bon Appetit");
  16.     else printf("%d\n"(anna-sum));
  17.     return 0;
  18. }