LeetCode Çözümleri – 448. Find All Numbers Disappeared in an Array

LeetCode içerisinde bulunan “Find All Numbers Disappeared in an Array” sorusunun açıklaması ve çözümü. Bu soruda size [1, n] aralığındaki sayıları içerebilen bir tam sayı dizisinde, [1, n] aralığında olup ta bu dizide olmayan sayıları geriye döndürmeniz isteniyor.

► LeetCode 228. 448. Find All Numbers Disappeared in an Array: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/

► Problem açıklaması:

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]

Output: [5,6]

Example 2:

Input: nums = [1,1]

Output: [2]

Constraints:

n == nums.length

1 <= n <= 10^5

1 <= nums[i] <= n

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

HackerRank Çözümleri – Cycle Detection

HackerRank içerisinde bulunan “Cycle Detection” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir linked list içerisinde döngüsel bir yapı olup olmadığını bulmanız isteniyor.

► HackerRank – Cycle Detection: https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem

► Problem açıklaması:

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.

Example

head refers to the list of nodes 1 — 2 — 3 — NULL

The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0.

head refers to the list of nodes 1 — 2 — 3 — 1 — NULL

There is a cycle where node 3 points back to node 1, so return 1.

Function Description

Complete the has_cycle function in the editor below.

It has the following parameter:

SinglyLinkedListNode pointer head: a reference to the head of the list

Returns

int:1 if there is a cycle or 0 if there is not

Note: If the list is empty, head will be null.

Input Format

The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases format will not be described for this question due to its complexity. Expand the section for the main function and review the code if you would like to figure out how to create a custom case.

LeetCode Çözümleri – 228. Summary Ranges

LeetCode içerisinde bulunan “Summary Ranges” sorusunun açıklaması ve çözümü. Bu soruda size verilen tam sayılarda, birbirini takip edenleri “aralık” olarak yazdırmanız isteniyor. Yani [0,1,2,4,5,7] için sizden [“0–2″,”4–5″,”7”] şeklinde bir liste isteniyor.

► LeetCode 228. Summary Ranges: https://leetcode.com/problems/summary-ranges/

► Problem açıklaması:

You are given a sorted unique integer array nums.

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

“a–b” if a != b

“a” if a == b

Example 1:

Input: nums = [0,1,2,4,5,7]

Output: [“0–2″,”4–5″,”7”]

Explanation: The ranges are:

[0,2] — “0–2”

[4,5] — “4–5”

[7,7] — “7”

Example 2:

Input: nums = [0,2,3,4,6,8,9]

Output: [“0″,”2–4″,”6″,”8–9”]

Explanation: The ranges are:

[0,0] — “0”

[2,4] — “2–4”

[6,6] — “6”

[8,9] — “8–9”

Example 3:

Input: nums = []

Output: []

Example 4:

Input: nums = [-1]

Output: [“-1”]

Example 5:

Input: nums = [0]

Output: [“0”]

Constraints:

0 lessEqual nums.length lessEqual 20

-2^31 <= nums[i] <= 2^31 – 1

All the values of nums are unique.

nums is sorted in ascending order.

LeetCode Çözümleri – 1184. Distance Between Bus Stops

LeetCode içerisinde bulunan “Distance Between Bus Stops” sorusunun açıklaması ve çözümü. Bu soruda size verilen başlangıç ve bitiş otobüs durak numaralarına göre (ki bu duraklar dairesel olarak dizilmişler), bunlar arasında gidebileceğiniz en kısa mesafeyi (saat yönünde veya tersinde) bulmanız isteniyor.

► LeetCode 1184. Distance Between Bus Stops: https://leetcode.com/problems/distance-between-bus-stops/

► Problem açıklaması:

A bus has n stops numbered from 0 to n – 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

The bus goes along both directions i.e. clockwise and counterclockwise. Return the shortest distance between the given start and destination stops.

Example 1:

Input: distance = [1,2,3,4], start = 0, destination = 1

Output: 1

Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1

Example 2:

Input: distance = [1,2,3,4], start = 0, destination = 2

Output: 3

Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.

Example 3:

Input: distance = [1,2,3,4], start = 0, destination = 3

Output: 4

Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.

Constraints:

1 <= n <= 10^4

distance.length == n

0 <= start, destination < n

0 <= distance[i] <= 10^4

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

LeetCode Çözümleri – 507. Perfect Number

LeetCode içerisinde bulunan “Perfect Number” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit tamsayının “perfect number”, yani kendisi hariç pozitif tam bölenlerinin toplamı kendine eşit bir sayı olup olmadığını bulmanız isteniyor.

► LeetCode 507. Perfect Number: https://leetcode.com/problems/perfect-number/

► Problem açıklaması:

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.

Given an integer n, return true if n is a perfect number, otherwise return false.

Example 1:

Input: num = 28

Output: true

Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28.

Example 2:

Input: num = 6

Output: true

Example 3:

Input: num = 496

Output: true

Example 4:

Input: num = 8128

Output: true

Example 5:

Input: num = 2

Output: false

Constraints:

1 <= num <= 10^8

https://youtu.be/1Awbh059xmk

LeetCode Çözümleri – 605. Can Place Flowers

LeetCode içerisinde bulunan “Can Place Flowers” sorusunun açıklaması ve çözümü. Bu soruda bir çiçekliği temsil bir tam sayı dizisinde, iki çiçeğin yan yana olmaması koşuluyla verilen çiçek sayısının hepsinin yerleştirilip yerleştirilemeyeceği soruluyor.

► LeetCode 605. Can Place Flowers: https://leetcode.com/problems/can-place-flowers/

► Problem açıklaması:

You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.

Given an integer array flowerbed containing 0’s and 1’s, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1

Output: true

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2

Output: false

Constraints:

1 <= flowerbed.length <= 2 * 10^4

flowerbed[i] is 0 or 1.

There are no two adjacent flowers in flowerbed.

0 <= n <= flowerbed.length

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