Tag Archives: hackerrank

HackerRank Çözümleri – Sherlock and Squares

HackerRank içerisinde bulunan “Sherlock and Squares” sorusunun açıklaması ve çözümü. Bu soruda verilen iki sayı arasındaki tam kare olan sayıların adetini bulmanız istenir.

► HackerRank – Sherlock and Squares: https://www.hackerrank.com/challenges/sherlock-and-squares/problem

► Problem açıklaması:

Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.

Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25

Example

a = 24

b = 49

There are three square integers in the range: 25, 36 and 49. Return 3.

Function Description

Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.

squares has the following parameter(s):

int a: the lower range boundary

int b: the upper range boundary

Returns

int: the number of square integers in the range.

Input Format

The first line contains q, the number of test cases. Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.

Sample Input

2

3 9

17 24

Sample Output

2

0

HackerRank Çözümleri – Viral Advertising

HackerRank içerisinde bulunan “Viral Advertising” sorusunun açıklaması ve çözümü. HackerLand Enterprise firması yeni bir reklam stratejisi üzerinde çalışıyor. İlk günde bu reklamı 5 kişiye gösteriyorlar sosyal medyada. Ve bu kişi sayısının yarısı 3’er arkadaşı ile paylaşıyor bu reklamı. Size verilen n gün sonra bu reklamı kaç kişi gördüğünü bulmanız isteniyor.

► HackerRank – Viral Advertising: https://www.hackerrank.com/challenges/strange-advertising/problem

► Problem açıklaması:

HackerLand Enterprise is adopting a new viral advertising strategy. When they launch a new product, they advertise it to exactly 5 people on social media.

On the first day, half of those 5 people (i.e., 5 / 2 = 2) like the advertisement and each shares it with 3 of their friends. At the beginning of the second day, (5 / 2 * 3 = 2 *3 = 6) people receive the advertisement.

Each day, floor(recipients / 2) of the recipients like the advertisement and will share it with 3 friends on the following day. Assuming nobody receives the advertisement twice, determine how many people have liked the ad by the end of a given day, beginning with launch day as day 1.

Example

n = 5.

Day Shared Liked Cumulative

1 5 2 2

2 6 3 5

3 9 4 9

4 12 6 15

5 18 9 24

The progression is shown above. The cumulative number of likes on the 5th day is 24.

Function Description

Complete the viralAdvertising function in the editor below.

viralAdvertising has the following parameter(s):

int n: the day number to report

Returns

int: the cumulative likes at that day

Input Format

A single integer, , the day number.

Constraints

Sample Input

3

Sample Output

9

HackerRank Çözümleri – Mars Exploration

HackerRank içerisinde bulunan “Mars Exploration” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “SOS” string’inin orijinalinden kaç adet farklı karakter barındırdığını bulmanız isteniyor.

► HackerRank – Mars Exploration: https://www.hackerrank.com/challenges/mars-exploration/problem

► Problem açıklaması:

A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help.

Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of the SOS message have been changed by radiation.

Example

s = “SOSTOT”

The original message was SOSSOS. Two of the message’s characters were changed in transit.

Function Description

Complete the marsExploration function in the editor below.

marsExploration has the following parameter(s):

string s: the string as received on Earth

Returns

int: the number of letters changed during transmission

Sample Input 0

SOSSPSSQSSOR

Sample Output 0

3

Explanation 0

= SOSSPSSQSSOR, and signal length s = |12|. They sent 4 SOS messages (i.e.: ).

Expected signal: SOSSOSSOSSOS

Recieved signal: SOSSPSSQSSOR

Difference: X X X

Sample Input 1

SOSSOT

Sample Output 1

1

Explanation 1

s = SOSSOT, and signal length 6. They sent 2 SOS messages (i.e.: ).

Expected Signal: SOSSOS

Received Signal: SOSSOT

Difference: X

Sample Input 2

SOSSOSSOS

Sample Output 2

0

Explanation 2

Since no character is altered, return 0

HackerRank Çözümleri – Super Reduced String

HackerRank içerisinde bulunan “Super Reduced String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string‘in içerisinde, yan yana olan aynı karakterlerin silinerek elde edilebilecek son string’i bulmanız isteniyor.

► HackerRank – Super Reduced String: https://www.hackerrank.com/challenges/reduced-string/problem

► Problem açıklaması:

Reduce a string of lowercase characters in range ascii[‘a’..’z’]by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them.

Delete as many characters as possible using this method and return the resulting string. If the final string is empty, return Empty String

Example.

s = “aab”

aab shortens to b in one operation: remove the adjacent a characters.

s = “abba”

Remove the two ‘b’ characters leaving ‘aa’. Remove the two ‘a’ characters to leave ”. Return ‘Empty String’.

Function Description

Complete the superReducedString function in the editor below.

superReducedString has the following parameter(s):

string s: a string to reduce

Returns

string: the reduced string or Empty String

Sample Input 0

aaabccddd

Sample Output 0

abd

Explanation 0

Perform the following sequence of operations to get the final string:

aaabccddd → abccddd → abddd → abd

Sample Input 1

aa

Sample Output 1

Empty String

Explanation 1 aa → Empty String

Sample Input 2

baab

Sample Output 2

Empty String

Explanation 2

baab → bb → Empty String

HackerRank Çözümleri – Grading Students

HackerRank içerisinde bulunan “Grading Students” sorusunun açıklaması ve çözümü. Bu soruda size verilen sınav sonuçlarını, aşağıda yazılan kurallara göre yuvarlamanız isteniyor.

► HackerRank – Grading Students: https://www.hackerrank.com/challenges/grading/problem

► Problem açıklaması:

HackerLand University has the following grading policy:

– Every student receives a in the inclusive range from 0 to 100.

– Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student’s according to these rules:

– If the difference between the grade and the next multiple of 5 is less than 3, round grade 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.

Examples

grade = 84 round to (85 – 84 is less than 3)

grade = 29 do not round (result is less than 40)

grade = 57 do not round (60 – 57 is 3 or higher)

Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below.

gradingStudents has the following parameter(s):

int grades[n]: the grades before rounding

Returns

int[n]: the grades after rounding as appropriate

Sample Input 0

4

73

67

38

33

Sample Output 0

75

67

40

33

HackerRank Çözümleri – CamelCase

HackerRank içerisinde bulunan “CamelCase” sorusunun açıklaması ve çözümü. Bu soruda size camelCase olarak yazılmış bir string’in içerisinde kaç adet kelime olduğunu bulmanız isteniyor.

► HackerRank – CamelCase: https://www.hackerrank.com/challenges/camelcase/problem

► Problem açıklaması:

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

– It is a concatenation of one or more words consisting of English letters.

– All letters in the first word are lowercase.

– For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

Example

s = oneTwoThree

There are 3 words in the string: ‘one’, ‘Two’, ‘Three’.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

string s: the string to analyze

Returns

int: the number of words in s.

Sample Input

saveChangesInTheEditor

Sample Output

5

Explanation

String s contains five words:

1. save

2. Changes

3. In

4. The

5. Editor

HackerRank Çözümleri – Fibonacci Modified

HackerRank içerisinde bulunan “Fibonacci Modified” sorusunun açıklaması ve çözümü. Bu soruda fibonacci serisinin değiştirilmiş hali olarak, bir önceki sayının kendisi değil de karesinin toplamı şeklinde çözüm bulmanız isteniyor.

► HackerRank – Fibonacci Modified: https://www.hackerrank.com/challenges/fibonacci-modified/problem

► Problem açıklaması:

Implement a modified Fibonacci sequence using the following definition:

t(i + 2) = t(i) + t(i + 2)^2

Given three integers, t1, t2, and n, compute and print the nth term of a modified Fibonacci sequence.

Function Description

Complete the fibonacciModified function in the editor below. It must return the nth number in the sequence.

fibonacciModified has the following parameter(s):

int t1: an integer

int t2: an integer

int n: the iteration to report

Returns

int: the nth number in the sequence

Note: The value of t[n] may far exceed the range of a 64-bit integer. Many submission languages have libraries that can handle such large results but, for those that don’t (e.g., C++), you will need to compensate for the size of the result.

Sample Input

0 1 5

Sample Output

5

HackerRank Çözümleri – Extra Long Factorials

HackerRank içerisinde bulunan “Extra Long Factorials” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayının faktöriyelini bulmanız isteniyor. Bazı inputlar için faktöriyel 64-bit tam sayıyı aşabilir, bu yüzden daha büyük bir tam sayı tipi kullanmanız gerekiyor.

► HackerRank – Extra Long Factorials: https://www.hackerrank.com/challenges/extra-long-factorials/problem

► Problem açıklaması:

The factorial of the integer n, written n!, is defined as:

n! = n * (n – 1) * (n – 2) * … * 3 * 2 * 1

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

n: an integer

Note: Factorials of n biggerThan 20 can’t be stored even in a 64-bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

HackerRank Çözümleri – Fair Rations

HackerRank içerisinde bulunan “Fair Rations” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde, aşağıdaki kurala istinaden kaç adet sayı eklemesi yapılarak tüm sayıların çift sayı olabileceği soruluyor.

► HackerRank – Fair Rations: https://www.hackerrank.com/challenges/fair-rations/problem

► Problem açıklaması:

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread. Your subjects are in a line, and some of them already have some loaves. Times are hard and your castle’s food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules:

1. Every time you give a loaf of bread to some person i, you must also give a loaf of bread to the person immediately in front of or behind them in the line (i.e., persons i + 1 or i – 1).

2. After all the bread is distributed, each person must have an even number of loaves.

Given the number of loaves already held by each citizen, find and print the minimum number of loaves you must distribute to satisfy the two rules above. If this is not possible, print NO.

Sample Input 0

5 2 3 4 5 6

Sample Output 0

4

Explanation 0

The initial distribution is (2, 3, 4, 5, 6). The requirements can be met as follows:

1. Give loaf of bread each to the second and third people so that the distribution becomes (2, 4, 5, 5, 6).

2. Give loaf of bread each to the third and fourth people so that the distribution becomes (2, 4, 6, 6, 6).

Each of the N subjects has an even number of loaves after 4 loaves were distributed.

HackerRank Çözümleri – Minimum Loss

HackerRank içerisinde bulunan “Minimum Loss” sorusunun açıklaması ve çözümü. Bu soruda size ev fiyatları olarak verilen bir integer dizisi içerisinde, önce satın almak koşulu ile, satın alınıp sonrasında satış yapıldığında elde edebileceğiniz minimum zarar soruluyor.

► HackerRank – Minimum Loss: https://www.hackerrank.com/challenges/minimum-loss/problem

► Problem açıklaması:

Lauren has a chart of distinct projected prices for a house over the next several years. She must buy the house in one year and sell it in another, and she must do so at a loss. She wants to minimize her financial loss.

Example:

price = [20, 15, 8, 2, 12]

Her minimum loss is incurred by purchasing in year 2 at price[1] = 15 and reselling in year 5 at price[4] = 12. Return 15 – 12 = 3.

Function Description

Complete the minimumLoss function in the editor below.

minimumLoss has the following parameter(s):

int price[n]: home prices at each year

Returns int: the minimum loss possible

Sample Input 0

3

5 10 3

Sample Output 0

2

Sample Input

1

5 20 7 8 2 5

Sample Output 1

2