Posts

Showing posts from June, 2016

Cut the sticks problem solution

You are given sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick. Suppose we have six sticks of the following lengths: 5 4 4 2 2 8 Then, in one cut operation we make a cut of length 2 from each of the six sticks. For the next cut operation four sticks are left (of non-zero length), whose lengths are the following: 3 2 2 6 The above step is repeated until no sticks are left. Given the length of sticks, print the number of sticks that are left before each subsequent cut operations . Note: For each cut operation , you have to recalcuate the length of smallest sticks (excluding zero-length sticks). Input Format The first line contains a single integer . The next line contains integers: a 0 , a 1 ,...a N-1 separated by space, where a i represents the length of i th stick. Output Format For each operation, print the number of sticks

EVEN TRAINING

Maria loves playing soccer. Because she wants to play professionally when she grows up, she tracks her training by marking her calendar with a on the days that she trained, and a on the days she did not. She also loves math, and wants your help connecting her soccer training with what she's learning about even and odd numbers in school. Given days of Maria's calendar history, print whether or not she trained for an even number of days (either or ), followed by the total number of days she trained. Note: Code demonstrating how to read input from stdin is provided in your editor. Input Format The first line contains an integer, , denoting the number of days Maria tracked in her calendar. The next line contains space-separated integers describing whether or not Maria trained each day. Recall that the number indicates that she did train, and indicates she did not. Constraints Output Format Your output should consist of space-separat

Diagonal Difference

Print the absolute difference between the two sums of the matrix's diagonals as a single integer. Sample Input 3 11 2 4 4 5 6 10 8 -12 Sample Output 15 Explanation The primary diagonal is: 11       5             -12 Sum across the primary diagonal: 11 + 5 - 12 = 4 The secondary diagonal is:             4       5 10 Sum across the secondary diagonal: 4 + 5 + 10 = 19 Difference: |4 - 19| = 15 #include<iomanip.h> int main(){     int n;     cin >> n;                  int   m[n][n],i,j,sum=0,sum2=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>m[i][j]; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j) sum+=m[i][j]; if(i+j==n-1) sum2+=m[i][j]; } } cout<<abs(sum-sum2);     return 0; }

Angry professor problem solution

This problem is taken from Hakerrank : A Discrete Mathematics professor has a class of N N students. Frustrated with their lack of discipline, he decides to cancel class if fewer than K K students are present when class starts. Given the arrival time of each student, determine if the class is canceled. Input Format The first line of input contains T T , the number of test cases. Each test case consists of two lines. The first line has two space-separated integers, N N (students in the class) and K K (the cancelation threshold). The second line contains N N space-separated integers ( a 1 , a 2 , … , a N a 1 , a 2 , … , a N ) describing the arrival times for each student. Note: Non-positive arrival times ( a i ≤ 0 a i ≤ 0 ) indicate the student arrived early or on time; positive arrival times ( a i > 0 a i > 0 ) indicate the student arrived a i a i minutes late. Output Format For each test case, print the word YES if the class is canceled or NO if it