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

UPCOMING CHALLENGES:

CURRENT CHALLENGES:

Digital Gold

PAST CHALLENGES

Selenium 2018

Germanium 2018

Gallium 2018

Zinc 2018

Cuprum 2018

Cutting Complexity

Nickel 2018

Cobaltum 2018

Ferrum 2018

Manganum 2017

Chromium 2017

Vanadium 2016

Titanium 2016

Scandium 2016

Calcium 2015

Kalium 2015

Argon 2015

Chlorum 2014

Sulphur 2014

Phosphorus 2014

Silicium 2014

Aluminium 2014

Magnesium 2014

Natrium 2014

Neon 2014

Fluorum 2014

Oxygenium 2014

Nitrogenium 2013

Carbo 2013

Boron 2013

Beryllium 2013

Lithium 2013

Helium 2013

Hydrogenium 2013

Omega 2013

Psi 2012

Chi 2012

Phi 2012

Upsilon 2012

Tau 2012

Sigma 2012

Rho 2012

Pi 2012

Omicron 2012

Xi 2012

Nu 2011

Mu 2011

Lambda 2011

Kappa 2011

Iota 2011

Theta 2011

Eta 2011

Zeta 2011

Epsilon 2011

Delta 2011

Gamma 2011

Beta 2010

Alpha 2010

ambitious

Count the minimum number of moves required to rearrange sprinklers so as to maximize the number of farmland fields that get watered.

Programming language:
Spoken language:

Joe the Farmer owns a square plot of farmland whose sides are of length N. The land is split into N rows and N columns of equal size, so that there are N^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

Every sprinkler can be placed in any field of the farmland, but no two sprinklers can occupy the same field. Upon activation, each sprinkler irrigates every field within the same column and row in which it appears.

After delivery, the sprinklers were placed in a batch, so the K-th sprinkler is positioned in field (X** _{K}**, Y

In one move, the farmer can move a single sprinkler to an adjacent unoccupied field. Two fields are adjacent to one another if they have a common side.

What is the minimum number of moves required to rearrange the sprinklers so that all fields will be irrigated by sprinklers, and no two sprinklers will water the same column or row? Since the answer can be very large, provide it modulo 1,000,000,007 (10^{9} + 7).

Write a function:

int solution(int X[], int Y[], int N);

that, given arrays X and Y, both consisting of N integers and representing the coordinates of consecutive sprinklers, returns a minimal number of moves modulo 1,000,000,007, required to irrigate all fields.

For example, given array X = [1, 2, 2, 3, 4] and array Y = [1, 1, 4, 5, 4] the function should return 5, as we can make following moves:

For another example, given array X = [1, 1, 1, 1] and array Y = [1, 2, 3, 4] the function should return 6:

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

Joe the Farmer owns a square plot of farmland whose sides are of length N. The land is split into N rows and N columns of equal size, so that there are N^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

Every sprinkler can be placed in any field of the farmland, but no two sprinklers can occupy the same field. Upon activation, each sprinkler irrigates every field within the same column and row in which it appears.

After delivery, the sprinklers were placed in a batch, so the K-th sprinkler is positioned in field (X** _{K}**, Y

In one move, the farmer can move a single sprinkler to an adjacent unoccupied field. Two fields are adjacent to one another if they have a common side.

What is the minimum number of moves required to rearrange the sprinklers so that all fields will be irrigated by sprinklers, and no two sprinklers will water the same column or row? Since the answer can be very large, provide it modulo 1,000,000,007 (10^{9} + 7).

Write a function:

int solution(vector<int> &X, vector<int> &Y);

that, given arrays X and Y, both consisting of N integers and representing the coordinates of consecutive sprinklers, returns a minimal number of moves modulo 1,000,000,007, required to irrigate all fields.

For example, given array X = [1, 2, 2, 3, 4] and array Y = [1, 1, 4, 5, 4] the function should return 5, as we can make following moves:

For another example, given array X = [1, 1, 1, 1] and array Y = [1, 2, 3, 4] the function should return 6:

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

Joe the Farmer owns a square plot of farmland whose sides are of length N. The land is split into N rows and N columns of equal size, so that there are N^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

Every sprinkler can be placed in any field of the farmland, but no two sprinklers can occupy the same field. Upon activation, each sprinkler irrigates every field within the same column and row in which it appears.

After delivery, the sprinklers were placed in a batch, so the K-th sprinkler is positioned in field (X** _{K}**, Y

In one move, the farmer can move a single sprinkler to an adjacent unoccupied field. Two fields are adjacent to one another if they have a common side.

What is the minimum number of moves required to rearrange the sprinklers so that all fields will be irrigated by sprinklers, and no two sprinklers will water the same column or row? Since the answer can be very large, provide it modulo 1,000,000,007 (10^{9} + 7).

Write a function:

class Solution { public int solution(int[] X, int[] Y); }

that, given arrays X and Y, both consisting of N integers and representing the coordinates of consecutive sprinklers, returns a minimal number of moves modulo 1,000,000,007, required to irrigate all fields.

For example, given array X = [1, 2, 2, 3, 4] and array Y = [1, 1, 4, 5, 4] the function should return 5, as we can make following moves:

For another example, given array X = [1, 1, 1, 1] and array Y = [1, 2, 3, 4] the function should return 6:

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

func Solution(X []int, Y []int) int

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

class Solution { public int solution(int[] X, int[] Y); }

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

function solution(X, Y);

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

function solution(X, Y)

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

Note: All arrays in this task are zero-indexed, unlike the common Lua convention. You can use `#A` to get the length of the array A.

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

int solution(NSMutableArray *X, NSMutableArray *Y);

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

function solution(X: array of longint; Y: array of longint; N: longint): longint;

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

function solution($X, $Y);

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

sub solution { my ($X, $Y)=@_; my @X=@$X; my @Y=@$Y; ... }

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

def solution(X, Y)

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

def solution(x, y)

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

object Solution { def solution(x: Array[Int], y: Array[Int]): Int }

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

public func solution(inout X : [Int], inout _ Y : [Int]) -> Int

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

public func solution(_ X : inout [Int], _ Y : inout [Int]) -> Int

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

^{2} identical square fields within the farmland. Thanks to the abundant crops that Joe harvested last year, he could afford to buy N sprinklers. They significantly reduce the amount of time that Joe spends watering his plants.

** _{K}**, Y

^{9} + 7).

Write a function:

Private Function solution(X As Integer(), Y As Integer()) As Integer

Given array X = [1, 1, 2] and array Y = [1, 2, 1] the function should return 4:

Assume that:

- N is an integer within the range [2..100,000];
- each element of arrays X, Y is an integer within the range [1..N];
- each sprinkler appears in a distinct field (no field may contain more than one sprinkler).

Complexity:

- expected worst-case time complexity is O(N);

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

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