codility | My solutions for exercises from codility.com
kandi X-RAY | codility Summary
kandi X-RAY | codility Summary
My solutions for exercises from codility.com.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of codility
codility Key Features
codility Examples and Code Snippets
Community Discussions
Trending Discussions on codility
QUESTION
I've been doing a lot of the practise tests on codility and though all my solutions work, they always have hidden stress tests which include massive arrays, the following code for example needs to take array A and find the lowest positive number missing from the sequence.
e.g: given A = [1, 3, 6, 4, 1, 2], the function should return 5. 5 is the value of i
which increments in the loop. Function returns whenever index i
is not found in array.
It works correctly but codility tested it with a 1-100,000 array and it timed out at the 6 second limit. most of my codes on the practise tests tend to fail for the same reason. Any help greatly appreciated.
...ANSWER
Answered 2021-May-27 at 11:06Your code loops through every item in the array for every item in the array. This gives a worst-case runtime of O(n^2)
. You can get a much better result if you sort the array, and then iterate through it looking for any missing numbers.
QUESTION
the following code generates a kind of 'skyline' profile where the initial values are 0's and the profile is made with 1's. I implemented using a for loop and I wonder if there is a more efficient way to obtain the same result.
...ANSWER
Answered 2021-May-26 at 23:27Unless you want to use matrix A
for some computation which result in numbers other than 1 or 0, you can simply treat 1 and 0 like True and False:
QUESTION
recently I have been exercising on Codility and I have managed to accomplish the exercise Passing Cars.
...ANSWER
Answered 2021-Apr-21 at 14:08To execute a C program its source code has to be compiled/translated by the compiler into machine code.
Machine code is just a bunch of instruction that the CPU can execute one by one, to causing the value of registers or memory locations to change. When a part of these execution should handover a value to another section, that gets executed subsequent, it has to pass these value via a register. But when the "return value register" is not set, i may contain some random value of a prior instruction result. Your CPU does not care about missing hand-overs between two code sections, because it does only care about executing instruction one by one, without knowing what the larger logic of these instructions was ment to be.
It seams that in your case that the old value in such a register is coincidentally the same value as what which should be set/returned in/from PassingCars
as a result. Probably because you have code that calculated the length of your input array and that value is used during the execution often, which is actually the correct result of the function to return.
To check this you could compile your source code into assembly code and reverse engineer it:
QUESTION
I am trying to solve the question https://medium.com/@sichangpark/codility-2-1-arrays-oddoccurrencesinarray-cf4c1f7d7caf
...ANSWER
Answered 2021-Mar-29 at 17:37Writing an "imperative" loop is (almost) never the "right" solution in scala. Having to write a return
is usually another indication that something is wrong.
Also, sorting the list in the beginning is inefficient. Something like this would solve this in linear time:
QUESTION
Currently working on problems from codility for practice, and for some reason I'm unable to get more than 83% correctness overall, originally I solved it with 100% correctness but with N^2 time complexity (it needs to be N or lower)
I've adjusted my code to be able to solve in O(N) but now my correctness has dropped to 77%, I'm currently trying to solve for cases of 2 elements ie) [1000,-1000] should return 2000, but I return a 0;
Link to Question on Codility:https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/
The question:
A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2..100,000]; each element of array A is an integer within the range [−1,000..1,000]
...ANSWER
Answered 2021-Feb-28 at 22:57Any integer P, such that 0 < P < N, splits this tape into two non-empty parts
The "non-empty" is crucial here. If you would try printing both parts in the second loop you would see that in the last iteration the second part is empty.
All you need to do is skip the last iteration in you loop:
QUESTION
I'm new to Java, and I'm wondering what can be done in the implementation below so that I wouldn't face with the aforementioned error in the question.
The coding question can be found here ==> Triangle: Determine if an array includes a triangular triplet (Codility)
My solution is as follows:
...ANSWER
Answered 2021-Feb-07 at 09:37If, for example, the 3 numbers you are testing are all Integer.MAX_VALUE
, they represent a valid triangle. However, your myTriangleList.get(i)+ myTriangleList.get(i+1)> myTriangleList.get(i+2)
test would return false
, because the addition of the first two numbers will give a negative sum, due to numeric overflow.
You can check if the sum is negative, and in that case accept the 3 numbers as a triangle:
QUESTION
Based on online solutions, I almost figured out how to solve Codility's MinMaxDivision, but there's one detail in the solution that I'm struggling to confirm.
The question is as follows:
Task description
You are given integers K, M and a non-empty array A consisting of N integers. Every element of the array is not greater than M.
You should divide this array into K blocks of consecutive elements. The size of the block is any integer between 0 and N. Every element of the array should belong to some block.
The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0.
The large sum is the maximal sum of any block.
For example, you are given integers K = 3, M = 5 and array A such that:
...
ANSWER
Answered 2020-Dec-30 at 20:50There are situations, where the number of blocks fits the criteria but none of the blocks have their sum equaling the mid value
This doesn't matter, because check
will return true
and a lower mid
will be checked: some lower mid
will eventually be one that will equal some block's sum.
One good example is A = [2, 3, 3, 5, 4, 2, 3], K = 3: the mid value eventually get's the value 10, we can have 3 blocks [2,3,3],[5,4],[2,3] but none of them are equal 10.
After mid = 10
and check
returning true
, this will execute:
QUESTION
I've been reading conflicting answers about modern javascript engines' time complexity when it comes to sets vs arrays in javascript.
I completed the demo task of codility, which is a simple assignment to find a solution for the following: given an array A of N integers, return the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
My first solution was:
...ANSWER
Answered 2020-Dec-10 at 18:27The comparison like that is not entirely fair because in the function where you use the Set, the Array needs to be converted to a Set first, which takes some time.
Have a look at the results below if this is ignored. I have updated the solution2
function to receive a Set
and changed the while
loop to a for
loop - for the sake of direct comparison.
You may notice that for a small array, Set might be slower. This is trivial because the time complexity only really comes into affect for a large (significant) n
.
Also note, Array.includes
is indeed O(n) but because it is in a for
loop which in the worst case could go up to n
the solution has a time complexity of O(n^2).
QUESTION
I try to solve codility CyclicRotation problem with Python. I got a runtime error for empty array: "tested program terminated with exit code 1" so my total score is %87. How can i fix this and take %100 score? https://i.stack.imgur.com/uiRcT.png
...ANSWER
Answered 2020-Nov-29 at 14:49Try this:
QUESTION
I was passing through codility lessons. I tried my best and scored 40% , then tried search for the answer.
Could someone who gets the following code kindly explain to me what's happening from maxFlags line(why maxFlags like that, I thought from what I understand the maximum flags we can have equals the number of peaks before filtering according the distance)
This is the 100% code I found
...ANSWER
Answered 2020-Nov-16 at 17:24According to the task, "if you take K flags, then the distance between any two flags should be greater than or equal to K." So for the covering distance, D, the minimal distance between any two flags is K. To maximise the number of flags, we use the minimal distance between them:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install codility
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page