Tasks Details
The submission is being evaluated.
The submission is being evaluated.
medium
Calculate the number of elements of an array that are not divisors of each element.
Task Score
33%
Correctness
40%
Performance
25%
You are given an array A consisting of N integers.
For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.
For example, consider integer N = 5 and array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6For the following elements:
- A[0] = 3, the non-divisors are: 2, 6,
- A[1] = 1, the non-divisors are: 3, 2, 3, 6,
- A[2] = 2, the non-divisors are: 3, 3, 6,
- A[3] = 3, the non-divisors are: 2, 6,
- A[4] = 6, there aren't any non-divisors.
Write a function:
class Solution { public int[] solution(int[] A); }
that, given an array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors.
Result array should be returned as an array of integers.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 3 A[4] = 6the function should return [2, 4, 3, 2, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- each element of array A is an integer within the range [1..2 * N].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 21
Time spent on task 14 minutes
Notes
not defined yet
Code: 13:45:11 UTC,
java,
autosave
Code: 13:46:15 UTC,
java,
autosave
Code: 13:46:26 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
// write your code in Java SE 8
}
}
Code: 13:47:05 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
for
// write your code in Java SE 8
}
}
Code: 13:47:20 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
for(int i=0; i<A.length; i++){
}
// write your code in Java SE 8
}
}
Code: 13:47:30 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
for(int i=0; i<A.length; i++){
p[A[i]]++;
}
// write your code in Java SE 8
}
}
Code: 13:47:45 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
for(int i=0; i<A.length; i++){
p[A[i]]++;
}
for(int i=0; i<)
// write your code in Java SE 8
}
}
Code: 13:48:03 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] p = new int[A.length*2 + 1];
for(int i=0; i<A.length; i++){
p[A[i]]++;
}
for(int i=0; i<A.length; i++){
}
// write your code in Java SE 8
}
}
Code: 13:48:23 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[]
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
}
// write your code in Java SE 8
}
}
Code: 13:48:35 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
}
// write your code in Java SE 8
}
}
Code: 13:48:45 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=0; j<)
}
// write your code in Java SE 8
}
}
Code: 13:48:55 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=0; j<sq)
}
// write your code in Java SE 8
}
}
Code: 13:49:26 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=0; j<Math.sqrt(A[i])){
}
}
// write your code in Java SE 8
}
}
Code: 13:49:38 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
}
}
// write your code in Java SE 8
}
}
Code: 13:49:52 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(i)
}
}
// write your code in Java SE 8
}
}
Code: 13:50:09 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A)
}
}
// write your code in Java SE 8
}
}
Code: 13:50:42 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A/)
}
}
// write your code in Java SE 8
}
}
Code: 13:51:03 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A/j == j){
p[i]++;
}
}
}
// write your code in Java SE 8
}
}
Code: 13:51:15 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A/j == j){
p[i] += A;
}
}
}
// write your code in Java SE 8
}
}
Code: 13:51:25 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A/j == j){
p[i] += A[j];
}
}
}
// write your code in Java SE 8
}
}
Code: 13:51:53 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A/j == j){
p[i] += A[j];
}else{
p[i] += (A[j] + A[]
}
}
}
// write your code in Java SE 8
}
}
Code: 13:52:03 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += A[j];
}else{
p[i] += (A[j] + A[]
}
}
}
// write your code in Java SE 8
}
}
Code: 13:53:06 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += A[j];
}else{
p[i] += (A[j] + A[A[i]/j]
}
}
}
// write your code in Java SE 8
}
}
Code: 13:53:16 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += A[j];
}else{
p[i] += (A[j] + A[A[i]/j]);
}
}
}
// write your code in Java SE 8
}
}
Code: 13:53:37 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (A[j] + A[A[i]/j]);
}
}
}
// write your code in Java SE 8
}
}
Code: 13:54:01 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
// write your code in Java SE 8
}
}
Code: 13:54:14 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
// write your code in Java SE 8
}
}
Code: 13:54:33 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
}
// write your code in Java SE 8
}
}
Code: 13:54:55 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i])){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
The submission is being evaluated.
Code: 13:55:03 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Code: 13:55:03 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Analysis
Code: 13:56:56 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Code: 13:57:07 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i] != j)
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Code: 13:57:17 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i] == j){}
else if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Analysis
Code: 13:57:59 UTC,
java,
autosave
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Code: 13:58:28 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<=Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got [2, 5, 3, 2, 0] expected [2, 4, 3, 2, 0]
Code: 13:58:35 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<=Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got [2, 5, 3, 2, 0] expected [2, 4, 3, 2, 0]
Code: 13:58:39 UTC,
java,
final,
score: 
33
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A) {
int[] cnt = new int[A.length*2 + 1];
int[] p = new int[A.length];
for(int i=0; i<A.length; i++){
cnt[A[i]]++;
}
for(int i=0; i<A.length; i++){
for(int j=1; j<=Math.sqrt(A[i]); j++){
if(A[i]/j == j){
p[i] += cnt[j];
}else{
p[i] += (cnt[j] + cnt[A[i]/j]);
}
}
}
int[] ans = new int[A.length];
for(int i=0; i<ans.length; i++){
ans[i] = A.length - p[i];
}
return ans;
// write your code in Java SE 8
}
}
The submission is being evaluated.