I've googled and asked people about this question, but can't get the meaning. I know my solution is incorrect, but I hope it can help (it's on JS):
let C = [1, 3, 1, 4, 2, 3, 5, 4],
Y = 4;
function solution(X, A) {
var leaves = [];
var i = 0;
var result = -1;
for (i = 0; i < A.length; i++) {
if (typeof leaves[A[i]] == 'undefined') {
leaves[A[i]] = i;
}
}
if (leaves.length <= X) {
return -1;
}
for (i = 1; i <= X; i++) {
if (typeof leaves[i] == 'undefined') {
return -1;
} else {
result = Math.max(result, leaves[i]);
}
}
return result;
}
console.log(solution(Y, C));
I've googled and asked people about this question, but can't get the meaning. I know my solution is incorrect, but I hope it can help (it's on JS):
let C = [1, 3, 1, 4, 2, 3, 5, 4],
Y = 4;
function solution(X, A) {
var leaves = [];
var i = 0;
var result = -1;
for (i = 0; i < A.length; i++) {
if (typeof leaves[A[i]] == 'undefined') {
leaves[A[i]] = i;
}
}
if (leaves.length <= X) {
return -1;
}
for (i = 1; i <= X; i++) {
if (typeof leaves[i] == 'undefined') {
return -1;
} else {
result = Math.max(result, leaves[i]);
}
}
return result;
}
console.log(solution(Y, C));
I don't need a solution to the task, I just need its clearer explanation, i.e what I have to do to solve it.
Share Improve this question edited Apr 23, 2018 at 9:50 Nina Scholz 387k26 gold badges363 silver badges413 bronze badges asked Apr 23, 2018 at 9:40 smussmus 1431 gold badge3 silver badges14 bronze badges 7- What is this code supposed to acplish? What would be the expected result for this input? What happens instead? Your variable names are pletely unhelpful. – Máté Safranka Commented Apr 23, 2018 at 9:42
- what is FrogRiverOne, and what should your code do? – Nina Scholz Commented Apr 23, 2018 at 9:43
- "I've googled and asked people about this question" what question? – Yury Tarabanko Commented Apr 23, 2018 at 9:43
- i dont understand the problem, but here is the description: app.codility./programmers/lessons/4-counting_elements/… – Nina Scholz Commented Apr 23, 2018 at 9:48
- answer1 and answer2, you can get answer. – Durga Commented Apr 23, 2018 at 9:50
12 Answers
Reset to default 14Simple 100% solution using Set(). Being a Set can only contain unique values, you can simply iterate through array A, adding the values into a Set until the size of Set equals the integer X, at which point that array key is the solution.
function solution(X, A) {
let holdValues = new Set();
for(i=0;i<A.length;i++) {
holdValues.add(A[i]);
if(holdValues.size == X)return i;
}
return -1;
}
Here is my solution using javascript:
function solution(X, A) {
let set = new Set();
for(let i = 0; i < A.length; i++){
if(A[i] <= X){
set.add(A[i]);
}
if(set.size === X){
return i;
}
}
return -1;
}
Okay, I figured out your problem for you. Your solution is actually almost correct, but you overplicated the evaluation. All you have to do is initialize a counter variable to 0, and as you iterate over A in the first loop, whenever leaves[A[i]]
is undefined
, increment this counter. This indicates that a leaf has fallen into a position where there is no leaf yet. If after incrementing the counter is equal to X, it means all positions now have leaves on them, so just return i
. If your loop makes it all the way to the end of A
, it means there are uncovered positions, so return -1.
My java Solution (100% Correctness, 100% Performance) it has:
Time plexity => O(N)
Space plexity => O(X)
public int solution(int X, int[] A) {
int[] flags = new int[X + 1];
int sum = 0;
//Equivalent to all the leafs have fallen.
int desiredSum = (X * (X + 1)) / 2; // 1 + 2 + ....n = (n * (n + 1)) / 2
for (int i = 0; i < A.length; i++) {
//Verify if the leaf [i] has fallen.
if (A[i] <= X && flags[A[i]] == 0) {
flags[A[i]] = 1;
sum += A[i];
}
//Verify if all the leafs have fallen.
if (sum == desiredSum) {
return i;
}
}
return -1;
}
Dictionary is better than Set, the plexity of updating or getting count of dic is O(1).
public func solution(_ x : Int, _ array: [Int]) -> Int {
let count = array.count
if x > count { return -1 }
var map = [Int: Bool]()
for i in 0..<count {
map[array[i]] = true
if map.count == x {
return i
}
}
return -1
}
public int solution(int X, int[] A) {
int sec = -1;
Set<Integer> checkList = new HashSet<>();
for(int i = 0; i < A.length; i++) {
if(A[i] >= 1 && A[i] <= X && checkList.size() < X) {
checkList.add(A[i]);
if (sec < i) {
sec = i;
}
}
if(checkList.size() == X) {
break;
}
}
System.out.println(checkList.toString());
if(checkList.size() < X) {
sec = -1;
}
return sec;
}
C#:
using System.Collections.Generic;
...
public static int solution(int X, int[] A)
{
HashSet<int> numbers = new HashSet<int>();
for (int i = 0; i < A.Length; i++)
{
numbers.Add(A[i]);
if (numbers.Count==X)
{
return i;
}
}
return -1;
}
Solution in Javascript, 100%, O(N), its a bit quicker than using sets on the performance tests. Longest part of this exercise was figuring out the question! Anyway, here's my solution:
function solution(X, A) {
let newArray = new Array(X + 1);
for (let i = 0; i < A.length; i++) {
if (!newArray[A[i]]) {
newArray[A[i]] = true;
X--;
if (!X) {
return i;
}
}
}
return -1;
}
this is my solution, java score 100:
HashSet<Integer> set = new HashSet<Integer>();
int response = -1;
for (int i = 0; i < A.length; i++){
set.add(A[i]);
if (set.size() == X){
response = i;
break;
}
}
return response;
function solution(A) {
let count = Math.floor(A.length / 2);
let map = new Map();
for (let i = 0; i < A.length; i++)
map.set(A[i], map.has(A[i]) ? map.get(A[i]) + 1 : 1);
for (const k of map.keys())
if (map.get(k) > count)
return A.indexOf(k);
return -1;
}
Plain java solution
class Solution {
public int solution(int X, int[] A) {
// write your code in Java SE 8
int leaves = A.length;
int counter=0;
int [] path= new int [X+1];
for (int t=0; t<leaves; t++){
if(path[A[t]]!=A[t]){
path[A[t]]=A[t];
counter++;
}
if (counter==X)
return t;
}
return -1;
}
}
Swift 100% solution:
public func solution(_ X : Int, _ A : inout [Int]) -> Int {
var occurs: [Int] = Array(repeating: -1, count: X + 1)
for i in 0..<A.count {
if occurs[A[i]] == -1 {
occurs[A[i]] = i
}
}
occurs.removeFirst()
if (occurs.filter { $0 == -1 }).count == 0 {
return occurs.max(by: <) ?? 0
}
return -1
}