Question
An array A of size 2n that contains each integer 1, 2, . . . , n twice is balanced if, for each integer k,
An array A of size 2n that contains each integer 1, 2, . . . , n twice is balanced if, for each integer k, the two occurrences of k in A are separated by k other integers. For example, the array A = [3, 1, 2, 1, 3, 2] is balanced because there are three integers between the two 3s, two integers between the two 2s and one integer between the two 1s. This question requires you to create a python function balanced that implements an algorithm (that you must devise) that counts, for an input n, the number of balanced arrays of size 2n.
finish the code
You must create a function balanced whose input is a positive integer n that returns the number of different ways of forming a balanced array of size 2n. #edit this cell with your answer def balanced(n): "return the number of balanced arrays of size 2n". Note that if you use backtracking (or recursion in some other way) and so the function calls itself, you might want it to accept more inputs. For example, if you want to call the function and say how many balanced arrays of size 2n you have found so far, you might write def balanced(n, count): You must, however, provide default values so that the function works when called with only one argument (in particular, your function should pass the tests below). So you might write, for example, def balanced(n, count=0) def balancedtest(): assert balanced(1) == 0 assert balanced(2) == 0 assert balanced(3) == 2 assert balanced(4) == 2 assert balanced(5) == 0 assert balanced(6) == 0 assert balanced (7) == 52 assert balanced(8) == 300 print ("all tests passed")Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started