Tasks Details
easy
1.
PermCheck
Check whether array A is a permutation.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given.
A permutation is a sequence containing each element from 1 to N once, and only once.
For example, array A such that:
A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2is a permutation, but array A such that:
A[0] = 4 A[1] = 1 A[2] = 3is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns 1 if array A is a permutation and 0 if it is not.
For example, given array A such that:
A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2the function should return 1.
Given array A such that:
A[0] = 4 A[1] = 1 A[2] = 3the function should return 0.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [1..1,000,000,000].
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 6 minutes
Notes
not defined yet
Task timeline
Code: 12:26:29 UTC,
java,
autosave
Code: 12:27:05 UTC,
java,
autosave
Code: 12:28:48 UTC,
java,
autosave
Code: 12:29:11 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) {
TreeMap map = new TreeMap();
for(int i=0; i<A.length; i++){
map.put(A[i]);
}
// write your code in Java SE 8
}
}
Analysis
Compile error
Solution.java:11: error: no suitable method found for put(int) map.put(A[i]); ^ method Map.put(Object,Object) is not applicable (actual and formal argument lists differ in length) method AbstractMap.put(Object,Object) is not applicable (actual and formal argument lists differ in length) method TreeMap.put(Object,Object) is not applicable (actual and formal argument lists differ in length) 1 error
Code: 12:29:27 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) {
TreeMap map = new TreeMap();
for(int i=0; i<A.length; i++){
map.put(A[i].);
}
// write your code in Java SE 8
}
}
Code: 12:29:30 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) {
TreeMap map = new TreeMap();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Analysis
Compile error
Solution.java:14: error: missing return statement } ^ Note: Solution.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error
Code: 12:30: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) {
Map<String, String> testMap = new HashMap<String, String>();
출처: https://bvc12.tistory.com/163 [국브스토리]
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Code: 12:30:36 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) {
Map<Integer, Integer> testMap = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Analysis
Compile error
Solution.java:12: error: cannot find symbol map.put(A[i], 1); ^ symbol: variable map location: class Solution 1 error
Code: 12:30:44 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Analysis
Compile error
Solution.java:15: error: missing return statement } ^ 1 error
Code: 12:30: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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Code: 12:31:04 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
// write your code in Java SE 8
}
}
Code: 12:31: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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(map.contains)
}
// write your code in Java SE 8
}
}
Code: 12:31:43 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(!map.containsKey(i)){
return 0;
}
}
return 1;
// write your code in Java SE 8
}
}
Analysis
Code: 12:31: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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(!map.containsKey(i+1)){
return 0;
}
}
return 1;
// write your code in Java SE 8
}
}
Code: 12:31:58 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(!map.containsKey(i+1)){
return 0;
}
}
return 1;
// write your code in Java SE 8
}
}
Analysis
Code: 12:32:05 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(!map.containsKey(i+1)){
return 0;
}
}
return 1;
// write your code in Java SE 8
}
}
Analysis
Code: 12:32:09 UTC,
java,
final,
score: 
100
// 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) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++){
map.put(A[i], 1);
}
for(int i=0; i<A.length; i++){
if(!map.containsKey(i+1)){
return 0;
}
}
return 1;
// write your code in Java SE 8
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N * log(N))
expand all
Correctness tests
1.
0.008 s
OK
2.
0.004 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
3.
0.004 s
OK
4.
0.008 s
OK
1.
0.004 s
OK
2.
0.008 s
OK
3.
0.004 s
OK
4.
0.008 s
OK
1.
0.008 s
OK
2.
0.008 s
OK
permutations_of_ranges
permutations of sets like [2..100] for which the anwsers should be false
permutations of sets like [2..100] for which the anwsers should be false
✔
OK
1.
0.004 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.048 s
OK
1.
0.380 s
OK
2.
0.376 s
OK
1.
0.484 s
OK
2.
0.484 s
OK
1.
0.476 s
OK
2.
0.480 s
OK
1.
0.008 s
OK
2.
0.228 s
OK
3.
0.008 s
OK
1.
0.004 s
OK
2.
0.008 s
OK
3.
0.048 s
OK
4.
0.472 s
OK
5.
0.484 s
OK