Tag Archives: hackerank soruları

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

HackerRank Çözümleri – Find the Median

HackerRank içerisinde bulunan “Find the Median” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir seri içerisindeki medyan değerini, yani seriyi ortadan ikiye ayıran değeri bulmanız isteniyor.

► HackerRank – Find the Median: https://www.hackerrank.com/challenges/find-the-median/problem

► Problem açıklaması:

The median of a list of numbers is essentially its middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, find the median?

https://en.wikipedia.org/wiki/Median

Example

arr = [5, 3, 1, 2, 4]

The sorted array arr = [1, 2, 3, 4, 5]. The middle element and the median is 3.

Sample Input 0

7

0 1 2 4 6 5 3

Sample Output 0

3

The sorted arr = [0, 1, 2, 3, 4, 5, 6]. It’s middle element is at arr[3] = 3.

HackerRank Çözümleri – Time Complexity: Primality

HackerRank içerisinde bulunan “Time Complexity: Primality” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir sayının asal sayı olup olmadığını O(sqrt(n)) zamanında bulmanız isteniyor.

► HackerRank – Time Complexity: Primality: https://www.hackerrank.com/challenges/ctci-big-o/problem

► Problem açıklaması:

A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given p integers, determine the primality of each integer and return Prime or Not prime on a new line.

Note: If possible, try to come up with an O(sqrt(n)) primality algorithm, or see what sort of optimizations you can come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code.

Sample Input

STDIN Function

—– ——–

3 p = 3 (number of values to follow)

12 n = 12 (first number to check)

5 n = 5

7 n = 7

Sample Output

Not prime

Prime

Prime

Explanation

We check the following integers for primality:

n = 12 is divisible by numbers other than 1 and itself (i.e.: 2, 3, 4, 6).

n = 5 is only divisible 1 and itself.

n = 7 is only divisible 1 and itself.

HackerRank Çözümleri – Mark and Toys

HackerRank içerisinde bulunan “Mark and Toys” sorusunun açıklaması ve çözümü. Burada verilen oyuncakların fiyat listesine ve elinizdeki paraya göre, en fazla kaç adet oyuncak satın alabileceğinizin bulunması isteniyor.

► HackerRank – Mark and Toys: https://www.hackerrank.com/challenges/mark-and-toys/problem

► Problem açıklaması:

Mark and Jane are very happy after having their first child. Their son loves toys, so Mark wants to buy some. There are a number of different toys lying in front of him, tagged with their prices. Mark has only a certain amount to spend, and he wants to maximize the number of toys he buys with this money. Given a list of toy prices and an amount to spend, determine the maximum number of gifts he can buy.

Note Each toy can be purchased only once.

Example;

prices = [1, 2, 3, 4]

k = 7

The budget is 7 units of currency. He can buy items that cost [1, 2, 3] for 6, or [3, 4] for 7 units. The maximum is 3 items.

Function Description

Complete the function maximumToys in the editor below.

maximumToys has the following parameter(s):

int prices[n]: the toy prices

int k: Mark’s budget

Returns

int: the maximum number of toys

Sample Input

7 50

1 12 5 111 200 1000 10

Sample Output

4

Explanation

He can buy only 4 toys at most. These toys have the following prices: 1, 12, 5, 10.

HackerRank Çözümleri – Alternating Characters

HackerRank içerisinde bulunan “Alternating Characters” sorusunun açıklaması ve çözümü. Soruda size verilen string içerisinde, yan yana hiçbir karakterin aynı olmaması için kaç defa karakter silme işlemi yapmanız gerektiği soruluyor.

► HackerRank – Alternating Characters: https://www.hackerrank.com/challenges/alternating-characters/problem

► Problem açıklaması:

You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.

Your task is to find the minimum number of required deletions.

Example

s = AABAAB

Remove an A at positions 0 and 3 to make s = ABAB in 2 deletions.

Function Description

Complete the alternatingCharacters function in the editor below.

alternatingCharacters has the following parameter(s):

string s: a string

Returns

int: the minimum number of deletions required

Sample Input

5

AAAA

BBBBB

ABABABAB

BABABA

AAABBB

Sample Output

3

4

0

0

4