MATLAB - Exercises
MATLAB - Exercises
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
Constraints
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
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
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
Input Format
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
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
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
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
Output Format
Sample Input
[1 20 25 20 36 20 98 5 98 20 3]
Sample Output
[20 98 5 98 20]