/codeFights

Problems from codefights web page

Primary LanguageJavaScriptGNU General Public License v3.0GPL-3.0

codeFights

These are my solutions in different programing language to problems from codefights web page.


Century from year.

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Example

For year = 1905, the output should be centuryFromYear(year) = 20; For year = 1700, the output should be centuryFromYear(year) = 17.

solutions available:

  • JavaScript/centuryFromYear.js
  • Java/centuryFromYear.java
  • Swift/centuryFromYear.swift
  • Kotlin/centuryFromYear.kt

Check Palindrome

Given the string, check if it is a palindrome.

Example

For inputString = "aabaa", the output should be checkPalindrome(inputString) = true; For inputString = "abac", the output should be checkPalindrome(inputString) = false; For inputString = "a", the output should be checkPalindrome(inputString) = true.

[input] string inputString

A non-empty string consisting of lowercase characters.

Guaranteed constraints: 1 ≤ inputString.length ≤ 105.

[output] boolean

true if inputString is a palindrome, false otherwise.

solutions available:

  • JavaScript/checkPalindrome.js

Adjacent Elements Product

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

Input/Output

[execution time limit] 4 seconds (js)

[input] array.integer inputArray

An array of integers containing at least two elements.

Guaranteed constraints: 2 ≤ inputArray.length ≤ 10, -1000 ≤ inputArray[i] ≤ 1000.

[output] integer

The largest product of adjacent elements.

solutions available:

  • JavaScript/adjacentElementsProduct.js

shape Area

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.

Example

For n = 2, the output should be shapeArea(n) = 5; For n = 3, the output should be shapeArea(n) = 13. Input/Output

[execution time limit] 4 seconds (js)

[input] integer n

Guaranteed constraints: 1 ≤ n < 104.

[output] integer

The area of the n-interesting polygon.

solutions available:

  • JavaScript/shapeArea.js

Make Array Consecutive2

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

Example

For statues = [6, 2, 3, 8], the output should be makeArrayConsecutive2(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

Input/Output

[execution time limit] 4 seconds (js)

[input] array.integer statues

An array of distinct non-negative integers.

Guaranteed constraints: 1 ≤ statues.length ≤ 10, 0 ≤ statues[i] ≤ 20.

[output] integer

The minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R) and no other sizes.

solutions available:

  • JavaScript/makeArrayConsecutive2.js