Sunteți pe pagina 1din 5

Programming

Exercises
Min / Max
Given five positive integers, find the minimum and maximum values that can be calculated by
summing exactly four of the five integers. Then print the respective minimum and maximum
values as a single line of two space-separated long integers.

Input Format

A single array of five space-separated integers.

Constraints

Each integer is in the inclusive range [1 … 10^9].

Output Format

Print two space-separated integers denoting the respective minimum and maximum values that
can be calculated by summing exactly four of the five integers. (The output can be greater than a
32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

University grading policy.


Every student receives a grade in the inclusive range from 0 to 100 .
Any less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student’s grade according to these
rules:

If the difference between the grade and the next multiple of 5 is less than 3, round up to the
next multiple of 5.

If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

For example, grade=84 will be rounded to 85 but grade=29 will not be rounded because the
rounding would result in a number that is less than 40 .

Given the initial value of grade for each of Sam’s n students, write code to automate the
rounding process. Complete the function solve that takes an integer array of all grades, and return
an integer array consisting of the rounded grades. For each grade , round it according to the
rules above and print the result on a new line.

Input Format

Array of integers denoting grades.


Constraints

1 <= n <= 60
0 <= grade(i) <= 100

Output Format

For each of the grades, print the rounded grade on a new line.

Sample Input

[73 67 38 33]

Sample Output

[75 67 40 33]

Breaking Records
Maria plays college basketball and wants to go pro. Each season she maintains a record of her
play. She tabulates the number of times she breaks her season record for most points and
least points in a game. Points scored in the first game establish her record for the season, and
she begins counting from there.

For example, assume her scores for the season are represented in the array score=[12,24,10,24].
Scores are in the same order as the games played. She would tabulate her results as follows:

```
Count
Game Score Minimum Maximum Min Max
0 --------12--------12----------12---------0----0
1 --------24--------12----------24---------0----1
2 --------10--------10----------24---------1----1
3 --------24--------10----------24---------1----1

```

Given Maria’s array of scores for a season of n games, find and print the number of times she
breaks her records for most and least points scored during the season.

Input Format

Array of integers.

Constraints

1 <= n <=1000
0 <= score < 200

Output Format

Print two space-seperated integers describing the respective numbers of times her best (highest)
score increased and her worst (lowest) score decreased.

Sample Input

10 5 20 20 4 5 2 25 1
Sample Output

2 4

Divisible Sum Pairs


You are given an array of n integers, a0, a1,…,a(n-1) and a positive integer, k . Find and print
the number of (i,j) pairs where i<j and a(i) + a(j) is divisible by k .

Input Format

The variable contains an integer k.


The variable is an array that contains n space-separated integers describing the values of
ar[a(0),a(1),...a(n-1)] .

Constraints

2<= n <=100
1<= k <=100
1<= a(i) <=100

Output Format

Print the number of (i,j) pairs where i<j and a(i) + a(j) is evenly divisible by k .

Sample Input

3
1 3 2 6 1 2

Sample Output

Explanation

(Keep in mind in MATLAB index starts at 1 , not 0 )

(0,2) -> a(0) + a(2) = 1+2 = 3


(0,5) -> a(0) + a(5) = 1+2 = 3
(1,3) -> a(1) + a(3) = 3+6 = 9
… And so on …

Programmers Day
Marie invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of
the Programmer (the 256th day of the year) during a year in the inclusive range from 1700 to
2700 .

From 1700 to 1917 , Russia’s official calendar was the Julian calendar; since 1919 they used
the Gregorian calendar system. The transition from the Julian to Gregorian calendar system
occurred in 1918 , when the next day after January 31st was February 14th. This means that in
1918 , February 14th was the 32nd day of the year in Russia.

In both calendar systems, February is the only month with a variable amount of days; it has 29
days during a leap year, and 28 days during all other years. In the Julian calendar, leap years
are divisible by 4 ; in the Gregorian calendar, leap years are either of the following:

Divisible by 400 .
Divisible by 4 and not divisible by 100 .

Given a year, y , find the date of the 256th day of that year according to the official Russian
calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day,
mm is the two-digit month, and yyyy is y .

Input Format

Single integer denoting the year, y .

Constraints

1700<= y <=2700

Output Format

Print the full date of Day of the Programmer during year y in the format dd.mm.yyyy, where dd
is the two-digit day, mm is the two-digit month, and yyyy is y .

Sample Input

2017

Sample Output

13.09.2017

Equalize Array
Karl has an array of integers. He wants to reduce the array until all remaining elements are equal.
Determine the fewest number of elements to delete to reach his goal.

For example, if his array is arr=[1,2,2,3] , we see that he can delete the 2 elements 1 and 3
leaving arr=[2,2] . He could also delete both twos and either the 1 or the 3, but that would take
3 deletions. The minimum number of deletions is 2.

Input Format

Array of n integers.

Constraints

1 <= n <= 100


1 <= arr(i) <= 100

Output Format

Print a single integer denoting the minimum number of elements Karl must delete for all elements
in the array to be equal.

Sample Input

3 3 2 1 3
Sample Output

Challenges…
Largest Palindrome
Write a program that finds the longest palindromic subset of integers in a given array. Try to be as
efficient as possible!

Input Format

Array of n positive integers.

Output Format

Array of m positive integers forming the largest palindrome.

Sample Input

[1 20 25 20 36 20 98 5 98 20 3]

Sample Output

[20 98 5 98 20]

S-ar putea să vă placă și