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:

Fast & Curious

PAST CHALLENGES

The Fellowship of the Code

May the 4th Challenge

The Great Code Off 2021

The Doge 2021

The Matrix 2021

The OLX Group challenge

Silver 2020

Palladium 2020

Rhodium 2019

Ruthenium 2019

Technetium 2019

Molybdenum 2019

Niobium 2019

Zirconium 2019

Yttrium 2019

Strontium 2019

Rubidium 2018

Arsenicum 2018

Krypton 2018

Bromum 2018

Future Mobility

Grand Challenge

Digital Gold

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

Check whether a given sequence is a valid traversal of some ternary undirected tree.

Spoken language:

An island has M towns: M ≥ 4 and M is an even integer. The towns are labelled with unique integers within the range [0..(M−1)]. The towns are connected through a network of (M−1) roads. Each road is bidirectional and connects exactly two distinct towns. Some towns, called *cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

Last year, the queen of the island went on a trip to visit all the towns (eventually arriving back at the starting point). She took a route that passed through each of (M−1) roads exactly twice. The sequence of towns visited during this trip was logged. Each cul-de-sac town was visited exactly once. Every other town had to be visited exactly three times (the final arrival at the starting point was not counted as a visit).

After last year's trip, the queen ordered the construction of a new circular highway connecting all cul-de-sac towns. The highway consists of a number of new roads, connecting the cul-de-sacs in the order of the queen's last visit.

The idea was raised that, with the new roads in place, it may be possible to devise a *Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

int solution(int A[], int N);

that, given an array A consisting of N = 2*(M−1) integers representing the sequence in which M towns were visited last year, returns the number of different Hamiltonian routes possible this year.

The function should return 0 if no Hamiltonian route exists.

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

Two Hamiltonian routes that contain exactly the same roads are considered the same, and are counted as one. For example, a route that goes through 0, 3, 4, 1, 5, 6, 7, 2 (and back to 0) is the same as the route 3, 4, 1, 5, 6, 7, 2, 0 (and back to 3), and is the same as the route 0, 2, 7, 6, 5, 1, 4, 3 (and back to 0).

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

The left picture below shows the road network inferred from the sequence A, and the right picture shows the new circular highway that runs through the cul-de-sacs:

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

the function should return −2, because the first road in the route does not connect distinct towns (it connects town 4 with itself).

Taking another example, for the following array A:

the function should return −2, because the given route violates the condition regarding towns (town 3 is visited neither exactly once, nor exactly thrice).

Taking another example, for the following sequence A:

the function should return −2, because the given route violates the condition regarding roads: the road connecting towns 0 and 3 occurs twice as it should (it is first taken in the direction from 0 to 3, then from 3 to 0), but the road connecting towns 0 and 1 (and few other roads) incorrectly occurs only once.

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

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

An island has M towns: M ≥ 4 and M is an even integer. The towns are labelled with unique integers within the range [0..(M−1)]. The towns are connected through a network of (M−1) roads. Each road is bidirectional and connects exactly two distinct towns. Some towns, called *cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

Last year, the queen of the island went on a trip to visit all the towns (eventually arriving back at the starting point). She took a route that passed through each of (M−1) roads exactly twice. The sequence of towns visited during this trip was logged. Each cul-de-sac town was visited exactly once. Every other town had to be visited exactly three times (the final arrival at the starting point was not counted as a visit).

After last year's trip, the queen ordered the construction of a new circular highway connecting all cul-de-sac towns. The highway consists of a number of new roads, connecting the cul-de-sacs in the order of the queen's last visit.

The idea was raised that, with the new roads in place, it may be possible to devise a *Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

int solution(vector<int> &A);

that, given an array A consisting of N = 2*(M−1) integers representing the sequence in which M towns were visited last year, returns the number of different Hamiltonian routes possible this year.

The function should return 0 if no Hamiltonian route exists.

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

Two Hamiltonian routes that contain exactly the same roads are considered the same, and are counted as one. For example, a route that goes through 0, 3, 4, 1, 5, 6, 7, 2 (and back to 0) is the same as the route 3, 4, 1, 5, 6, 7, 2, 0 (and back to 3), and is the same as the route 0, 2, 7, 6, 5, 1, 4, 3 (and back to 0).

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

The left picture below shows the road network inferred from the sequence A, and the right picture shows the new circular highway that runs through the cul-de-sacs:

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

the function should return −2, because the first road in the route does not connect distinct towns (it connects town 4 with itself).

Taking another example, for the following array A:

the function should return −2, because the given route violates the condition regarding towns (town 3 is visited neither exactly once, nor exactly thrice).

Taking another example, for the following sequence A:

the function should return −2, because the given route violates the condition regarding roads: the road connecting towns 0 and 3 occurs twice as it should (it is first taken in the direction from 0 to 3, then from 3 to 0), but the road connecting towns 0 and 1 (and few other roads) incorrectly occurs only once.

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

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

An island has M towns: M ≥ 4 and M is an even integer. The towns are labelled with unique integers within the range [0..(M−1)]. The towns are connected through a network of (M−1) roads. Each road is bidirectional and connects exactly two distinct towns. Some towns, called *cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

Last year, the queen of the island went on a trip to visit all the towns (eventually arriving back at the starting point). She took a route that passed through each of (M−1) roads exactly twice. The sequence of towns visited during this trip was logged. Each cul-de-sac town was visited exactly once. Every other town had to be visited exactly three times (the final arrival at the starting point was not counted as a visit).

After last year's trip, the queen ordered the construction of a new circular highway connecting all cul-de-sac towns. The highway consists of a number of new roads, connecting the cul-de-sacs in the order of the queen's last visit.

The idea was raised that, with the new roads in place, it may be possible to devise a *Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

class Solution { public int solution(int[] A); }

that, given an array A consisting of N = 2*(M−1) integers representing the sequence in which M towns were visited last year, returns the number of different Hamiltonian routes possible this year.

The function should return 0 if no Hamiltonian route exists.

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

Two Hamiltonian routes that contain exactly the same roads are considered the same, and are counted as one. For example, a route that goes through 0, 3, 4, 1, 5, 6, 7, 2 (and back to 0) is the same as the route 3, 4, 1, 5, 6, 7, 2, 0 (and back to 3), and is the same as the route 0, 2, 7, 6, 5, 1, 4, 3 (and back to 0).

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

The left picture below shows the road network inferred from the sequence A, and the right picture shows the new circular highway that runs through the cul-de-sacs:

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

the function should return −2, because the first road in the route does not connect distinct towns (it connects town 4 with itself).

Taking another example, for the following array A:

the function should return −2, because the given route violates the condition regarding towns (town 3 is visited neither exactly once, nor exactly thrice).

Taking another example, for the following sequence A:

the function should return −2, because the given route violates the condition regarding roads: the road connecting towns 0 and 3 occurs twice as it should (it is first taken in the direction from 0 to 3, then from 3 to 0), but the road connecting towns 0 and 1 (and few other roads) incorrectly occurs only once.

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

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

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

func Solution(A []int) int

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

class Solution { public int solution(int[] A); }

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

class Solution { public int solution(int[] A); }

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

function solution(A);

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

fun solution(A: IntArray): Int

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

function solution(A)

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

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.

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

int solution(NSMutableArray *A);

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

function solution(A: array of longint; N: longint): longint;

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

function solution($A);

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

sub solution { my (@A)=@_; ... }

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

def solution(A)

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

def solution(a)

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

object Solution { def solution(a: Array[Int]): Int }

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

public func solution(_ A : inout [Int]) -> Int

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].

*cul-de-sacs*, are connected to just one other town. Each of the remaining towns is connected to exactly three other towns. Each town can be reached from any other town.

*Hamiltonian* route this year; i.e. one that passes through each town exactly once (and arrives back at the starting point).

Write a function

Private Function solution(A As Integer()) As Integer

The function should return −1 if the number of possible Hamiltonian routes exceeds 100,000,000.

The function should return −2 if the route described by the array A violates any of the following conditions:

- each road connects distinct towns;
- each town is visited either exactly once or exactly thrice;
- each road is taken exactly twice.

For example, consider array A consisting of fourteen elements such that:

Given this array, the function should return 3, as explained below.

Exactly three distinct Hamiltonian routes are possible:

Taking another example, for the following array A:

Taking another example, for the following array A:

Taking another example, for the following sequence A:

Write an ** efficient** algorithm for the following assumptions:

- N = 2*(M−1), where M is an even integer within the range [4..200,000];
- each element of the array A is an integer within the range [0..(M−1)].