CodeFights
Solutions for challenges proposed on CodeFights.com/arcade
Install / Use
/learn @DataSutures/CodeFightsREADME
CodeFights
Solutions for challenges proposed on CodeFights.com
Arcade
Intro
- add
- centuryFromYear
- checkPalindrome
- adjacentElementsProduct
- shapeArea
- makeArrayConsecutive2
- almostIncreasingSequence
- matrixElementsSum
- allLongestStrings
- commonCharacterCount
- isLucky
- sortByHeight
- reverseParentheses
- alternatingSums
- addBorder
- areSimilar
- arrayChange
- palindromeRearranging
- areEquallyStrong
- arrayMaximalAdjacentDifference
- isIPv4Address
- avoidObstacles
- boxBlur
- minesweeper
- arrayReplace
- evenDigitsOnly
- variableName
- alphabeticShift
- chessBoardCellColor
- circleOfNumbers
- depositProfit
- absoluteValuesSumMinimization
- stringsRearrangement
- extractEachKth
- firstDigit
- differentSymbolsNaive
- arrayMaxConsecutiveSum
- growingPlant
- knapsackLight
- longestDigitsPrefix
- digitDegree
- bishopAndPawn
- isBeautifulString
- findEmailDomain
- buildPalindrome
- electionsWinners
- isMAC48Address
- lineEncoding
- chessKnight
- deleteDigit
- longestWord
- validTime
- sumUpNumbers
- differentSquares
- digitsProduct
- fileNaming
Databases
- projectList
- countriesSelection
- monthlyScholarships
- projectsTeam
- automaticNotifications
- volleyballResults
- mostExpensive
- contestLeaderboard
- gradeDistribution
- mischievousNephews
The Core
- addTwoDigits
- largestNumber
- candies
- seatsInTheater
- maxMultiple
- circleOfNumbers
- lateRide
- phoneCall
- reachNextLevel
- knapsackLight
- extraNumber
- isInfiniteProcess
- arithmeticExpression
- tennisSet
- willYou
- metroCard
Python
Graphs
add
Description
Write a function that returns the sum of two numbers.
Example:
param1 = 1
param2 = 2
add(param1, param2) = 3.
Input/Output
-
[time limit] 3000ms (java)
-
[input] integer param1
Guaranteed constraints:
-100 ≤ param1 ≤ 1000
-
[input] integer param2
Guaranteed constraints:
-100 ≤ param2 ≤ 1000
- [output] integer
The sum of the two inputs.
centuryFromYear
Description
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
//Example 1
year = 1905
centuryFromYear(year) = 20
//Example 2
year = 1700
centuryFromYear(year) = 17
Input/Output
-
[time limit] 3000ms (java)
-
[input] integer year
A positive integer, designating the year.
Guaranteed constraints:
1 ≤ year ≤ 2005.
-
[output] integer
The number of the century the year is in.
checkPalindrome
Description
Given a string, check if it is a palindrome.
Example
// Example 1
inputString = "aabaa"
checkPalindrome(inputString) = true;
// Example 2
inputString = "abac"
checkPalindrome(inputString) = false;
// Example 3
inputString = "a"
checkPalindrome(inputString) = true;
Input/Output
-
[time limit] 3000ms (java)
-
[input] string inputString
A non-empty string consisting of lowercase characters.
Guaranteed constraints:
1 ≤ inputString.length ≤ 10^5
-
[output] boolean
true if inputString is a palindrome, false otherwise.
adjacentElementsProduct
Description
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.ding the year 100, the second - from the year 101 up to and including the year 200, etc.
Example
inputArray = [3, 6, -2, -5, 7, 3]
adjacentElementsProduct(inputArray) = 21
// 7 and 3 produce the largest product.
Input/Output
-
[time limit] 3000ms (java)
-
[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.
shapeArea
Description
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
// Example 1
n = 2
shapeArea(n) = 5
// Example 2
n = 3
shapeArea(n) = 13
Input/Output
-
[time limit] 3000ms (java)
-
[input] integer n
Guaranteed constraints:
1 ≤ n < 10^4
- [output] integer
The area of the n-interesting polygon.
makeArrayConsecutive2
Description
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
statues = [6, 2, 3, 8]
makeArrayConsecutive2(statues) = 3
Input/Output
-
[time limit] 3000ms (java)
-
[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.
almostIncreasingSequence
Description
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Example
// Example 1
sequence = [1, 3, 2, 1]
almostIncreasingSequence(sequence) = false
// There is no one element in this array that can be removed in order to get a strictly increasing
// sequence.
// Example 2
sequence = [1, 3, 2]
almostIncreasingSequence(sequence) = true
// You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you
// can remove 2 to get the strictly increasing sequence [1, 3].
Input/Output
-
[time limit] 3000ms (java)
-
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 10^5
-105 ≤ sequence[i] ≤ 10^5
-
[output] boolean
Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.
matrixElementsSum
Description
After becoming famous, CodeBots decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms, each cell containing an integer - the price of the room. Some rooms are free (their cost is 0), but that's probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots.
Help the bots calculate the total price of all the rooms that are suitable for them.
Example
matrix = [[0, 1, 1, 2],
[0, 5, 0, 0],
[2, 0, 3, 3]]
matrixElementsSum(matrix) = 9
Here's the rooms matrix with unsuitable rooms marked with 'x':
[[x, 1, 1, 2],
[x, 5, x, x],
[x, x, x, x]]
Thus, the answer is: 1 + 5 + 1 + 2 = 9.
Input/Output
-
[time limit] 3000ms (java)
-
[input] array.array.integer matrix
2-dimensional array of integers representing a rectangular matrix of the building.Guaranteed constraints:
1 ≤ matrix.length ≤ 5
1 ≤ matrix[i].length ≤ 5
0 ≤ matrix[i][j] ≤ 10
