Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

AVAILABLE LESSONS:

Lesson 1

Iterations

Lesson 2

Arrays

Lesson 3

Time Complexity

Lesson 4

Counting Elements

Lesson 5

Prefix Sums

Lesson 6

Sorting

Lesson 7

Stacks and Queues

Lesson 8

Leader

Lesson 9

Maximum slice problem

Lesson 10

Prime and composite numbers

Lesson 11

Sieve of Eratosthenes

Lesson 12

Euclidean algorithm

Lesson 13

Fibonacci numbers

Lesson 14

Binary search algorithm

Lesson 15

Caterpillar method

Lesson 16

Greedy algorithms

Lesson 17

Dynamic programming

Lesson 90

Tasks from Indeed Prime 2015 challenge

Lesson 91

Tasks from Indeed Prime 2016 challenge

Lesson 92

Tasks from Indeed Prime 2016 College Coders challenge

Lesson 99

Future training

respectable

From drawers containing both clean and dirty socks, choose socks to launder in order to obtain the maximum number of clean pairs of socks.

Programming language:
Spoken language:

Bob is about to go on a trip. But first he needs to take care of his supply of socks. Each sock has its own color. Bob wants to take as many pairs of clean socks as possible (both socks in the pair should be of the same color).

Socks are divided into two drawers: clean and dirty socks. Bob has time for only one laundry and his washing machine can clean at most K socks. He wants to pick socks for laundering in such a way that after washing he will have a maximal number of clean, same-colored pairs of socks. It is possible that some socks cannot be paired with any other sock, because Bob may have lost some socks over the years.

Bob has exactly N clean and M dirty socks, which are described in arrays C and D, respectively. The colors of the socks are represented as integers (equal numbers representing identical colors).

For example, given four clean socks and five dirty socks:

If Bob's washing machine can clean at most K = 2 socks, then he can take a maximum of three pairs of clean socks. He can wash one red sock and one green sock, numbered 1 and 2 respectively. Then he will have two pairs of red socks and one pair of green socks.

Write a function:

int solution(int K, int C[], int N, int D[], int M);

that, given an integer K (the number of socks that the washing machine can clean), two arrays C and D (containing the color representations of N clean and M dirty socks respectively), returns the maximum number of pairs of socks that Bob can take on the trip.

For example, given K = 2, C = [1, 2, 1, 1] and D = [1, 4, 3, 2, 4], the function should return 3, as explained above.

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

Bob is about to go on a trip. But first he needs to take care of his supply of socks. Each sock has its own color. Bob wants to take as many pairs of clean socks as possible (both socks in the pair should be of the same color).

Socks are divided into two drawers: clean and dirty socks. Bob has time for only one laundry and his washing machine can clean at most K socks. He wants to pick socks for laundering in such a way that after washing he will have a maximal number of clean, same-colored pairs of socks. It is possible that some socks cannot be paired with any other sock, because Bob may have lost some socks over the years.

Bob has exactly N clean and M dirty socks, which are described in arrays C and D, respectively. The colors of the socks are represented as integers (equal numbers representing identical colors).

For example, given four clean socks and five dirty socks:

If Bob's washing machine can clean at most K = 2 socks, then he can take a maximum of three pairs of clean socks. He can wash one red sock and one green sock, numbered 1 and 2 respectively. Then he will have two pairs of red socks and one pair of green socks.

Write a function:

int solution(int K, vector<int> &C, vector<int> &D);

that, given an integer K (the number of socks that the washing machine can clean), two arrays C and D (containing the color representations of N clean and M dirty socks respectively), returns the maximum number of pairs of socks that Bob can take on the trip.

For example, given K = 2, C = [1, 2, 1, 1] and D = [1, 4, 3, 2, 4], the function should return 3, as explained above.

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

Bob is about to go on a trip. But first he needs to take care of his supply of socks. Each sock has its own color. Bob wants to take as many pairs of clean socks as possible (both socks in the pair should be of the same color).

Socks are divided into two drawers: clean and dirty socks. Bob has time for only one laundry and his washing machine can clean at most K socks. He wants to pick socks for laundering in such a way that after washing he will have a maximal number of clean, same-colored pairs of socks. It is possible that some socks cannot be paired with any other sock, because Bob may have lost some socks over the years.

Bob has exactly N clean and M dirty socks, which are described in arrays C and D, respectively. The colors of the socks are represented as integers (equal numbers representing identical colors).

For example, given four clean socks and five dirty socks:

If Bob's washing machine can clean at most K = 2 socks, then he can take a maximum of three pairs of clean socks. He can wash one red sock and one green sock, numbered 1 and 2 respectively. Then he will have two pairs of red socks and one pair of green socks.

Write a function:

class Solution { public int solution(int K, int[] C, int[] D); }

that, given an integer K (the number of socks that the washing machine can clean), two arrays C and D (containing the color representations of N clean and M dirty socks respectively), returns the maximum number of pairs of socks that Bob can take on the trip.

For example, given K = 2, C = [1, 2, 1, 1] and D = [1, 4, 3, 2, 4], the function should return 3, as explained above.

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

For example, given four clean socks and five dirty socks:

Write a function:

func Solution(K int, C []int, D []int) int

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

class Solution { public int solution(int K, int[] C, int[] D); }

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

function solution(K, C, D);

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

fun solution(K: Int, C: IntArray, D: IntArray): Int

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

function solution(K, C, D)

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

int solution(int K, NSMutableArray *C, NSMutableArray *D);

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

function solution(K: longint; C: array of longint; N: longint; D: array of longint; M: longint): longint;

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

function solution($K, $C, $D);

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

sub solution { my ($K, $C, $D)=@_; my @C=@$C; my @D=@$D; ... }

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

def solution(K, C, D)

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

def solution(k, c, d)

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

object Solution { def solution(k: Int, c: Array[Int], d: Array[Int]): Int }

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

public func solution(_ K : Int, _ C : inout [Int], _ D : inout [Int]) -> Int

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

For example, given four clean socks and five dirty socks:

Write a function:

Private Function solution(K As Integer, C As Integer(), D As Integer()) As Integer

Assume that:

- K is an integer within the range [0..50];
- each element of arrays C, D is an integer within the range [1..50];
- C and D are not empty and each of them contains at most 50 elements.

** correctness**. The performance of your solution will not be the focus of the assessment.

Information about upcoming challenges, solutions and lessons directly in your inbox.

© 2009–2020 Codility Ltd., registered in England and Wales (No. 7048726). VAT ID GB981191408. Registered office: 107 Cheapside, London EC2V 6DN