# Minimum changes required to make a Catalan Sequence

Given an array arr[] of N integer elements, the task is to change the minimum number of elements of this array such that it contains first N terms of the Catalan Sequence. Thus, find the minimum changes required.

First few Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …..

Examples:

Input: arr[] = {4, 1, 2, 33, 213, 5}
Output: 3
We have to replace 4, 33, 213 with 1, 14, 42 to make first 6 terms of Catalan sequence.

Input: arr[] = {1, 1, 2, 5, 41}
Output: 1
Simply change 41 with 14

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Take an unordered multiset. Insert first N terms of Catalan sequence in this multiset.
• Traverse the array from left to right. Check if the array element if present in the multiset. If it is present, then remove that element from the multiset.
• After traversing the array, the minimum changes required will be equal to the size of the multiset.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define MAX 100000 ` `#define ll long long int ` ` `  `// To store first N Catalan numbers ` `ll catalan[MAX]; ` ` `  `// Function to find first n Catalan numbers ` `void` `catalanDP(ll n) ` `{ ` ` `  `    ``// Initialize first two values in table ` `    ``catalan = catalan = 1; ` ` `  `    ``// Fill entries in catalan[] using recursive formula ` `    ``for` `(``int` `i = 2; i <= n; i++) { ` `        ``catalan[i] = 0; ` `        ``for` `(``int` `j = 0; j < i; j++) ` `            ``catalan[i] += catalan[j] * catalan[i - j - 1]; ` `    ``} ` `} ` ` `  `// Function to return the minimum changes required ` `int` `CatalanSequence(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Find first n Catalan Numbers ` `    ``catalanDP(n); ` ` `  `    ``unordered_multiset<``int``> s; ` ` `  `    ``// a and b are first two ` `    ``// Catalan Sequence numbers ` `    ``int` `a = 1, b = 1; ` `    ``int` `c; ` ` `  `    ``// Insert first n catalan elements to set ` `    ``s.insert(a); ` `    ``if` `(n >= 2) ` `        ``s.insert(b); ` ` `  `    ``for` `(``int` `i = 2; i < n; i++) { ` `        ``s.insert(catalan[i]); ` `    ``} ` ` `  `    ``unordered_multiset<``int``>::iterator it; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If catalan element is present ` `        ``// in the array then remove it from set ` `        ``it = s.find(arr[i]); ` `        ``if` `(it != s.end()) ` `            ``s.erase(it); ` `    ``} ` ` `  `    ``// Return the remaining number of ` `    ``// elements in the set ` `    ``return` `s.size(); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 5, 41 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << CatalanSequence(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `import` `java.util.HashSet; ` ` `  `// Java implementation of the approach  ` `class` `GFG1  ` `{ ` ` `  `    ``static` `int` `MAX = ``100000``; ` ` `  `    ``// To store first N Catalan numbers  ` `    ``static` `long` `catalan[] = ``new` `long``[MAX]; ` ` `  `    ``// Function to find first n Catalan numbers  ` `    ``static` `void` `catalanDP(``long` `n)  ` `    ``{ ` ` `  `        ``// Initialize first two values in table  ` `        ``catalan[``0``] = catalan[``1``] = ``1``; ` ` `  `        ``// Filong entries in catalan[]  ` `        ``// using recursive formula  ` `        ``for` `(``int` `i = ``2``; i <= n; i++)  ` `        ``{ ` `            ``catalan[i] = ``0``; ` `            ``for` `(``int` `j = ``0``; j < i; j++)  ` `            ``{ ` `                ``catalan[i] += catalan[j] * catalan[i - j - ``1``]; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Function to return the minimum changes required  ` `    ``static` `int` `CatalanSequence(``int` `arr[], ``int` `n)  ` `    ``{ ` ` `  `        ``// Find first n Catalan Numbers  ` `        ``catalanDP(n); ` ` `  `        ``HashSet s = ``new` `HashSet(); ` ` `  `        ``// a and b are first two  ` `        ``// Catalan Sequence numbers  ` `        ``int` `a = ``1``, b = ``1``; ` `        ``int` `c; ` ` `  `        ``// Insert first n catalan elements to set  ` `        ``s.add(a); ` `        ``if` `(n >= ``2``)  ` `        ``{ ` `            ``s.add(b); ` `        ``} ` ` `  `        ``for` `(``int` `i = ``2``; i < n; i++)  ` `        ``{ ` `            ``s.add((``int``) catalan[i]); ` `        ``} ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` ` `  `            ``// If catalan element is present  ` `            ``// in the array then remove it from set  ` `            ``if` `(s.contains(arr[i]))  ` `            ``{ ` `                ``s.remove(arr[i]); ` `            ``} ` `        ``} ` ` `  `        ``// Return the remaining number of  ` `        ``// elements in the set  ` `        ``return` `s.size(); ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``1``, ``1``, ``2``, ``5``, ``41``}; ` `        ``int` `n = arr.length; ` ` `  `        ``System.out.print(CatalanSequence(arr, n)); ` `    ``} ` `}  ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of ` `# the approach  ` `MAX` `=` `100000``;  ` ` `  `# To store first N Catalan numbers  ` `catalan ``=` `[``0``] ``*` `MAX``;  ` ` `  `# Function to find first n  ` `# Catalan numbers  ` `def` `catalanDP(n) :  ` ` `  `    ``# Initialize first two values  ` `    ``# in table  ` `    ``catalan[``0``] ``=` `catalan[``1``] ``=` `1``;  ` ` `  `    ``# Fill entries in catalan[]  ` `    ``# using recursive formula  ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``) : ` `        ``catalan[i] ``=` `0``;  ` `        ``for` `j ``in` `range``(i) : ` `            ``catalan[i] ``+``=` `(catalan[j] ``*`  `                           ``catalan[i ``-` `j ``-` `1``]);  ` ` `  `# Function to return the minimum  ` `# changes required  ` `def` `CatalanSequence(arr, n) : ` `     `  `    ``# Find first n Catalan Numbers  ` `    ``catalanDP(n);  ` ` `  `    ``s ``=` `set``();  ` ` `  `    ``# a and b are first two  ` `    ``# Catalan Sequence numbers  ` `    ``a ``=` `1` `; b ``=` `1``;  ` ` `  `    ``# Insert first n catalan  ` `    ``# elements to set  ` `    ``s.add(a);  ` `    ``if` `(n >``=` `2``) : ` `        ``s.add(b);  ` ` `  `    ``for` `i ``in` `range``(``2``, n) : ` `        ``s.add(catalan[i]);  ` `     `  `    ``temp ``=` `set``() ` `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# If catalan element is present  ` `        ``# in the array then remove it  ` `        ``# from set  ` `        ``if` `arr[i] ``in` `s : ` `            ``temp.add(arr[i]) ` `     `  `    ``s ``=` `s ``-` `temp ; ` `     `  `    ``# Return the remaining number  ` `    ``# of elements in the set  ` `    ``return` `len``(s);  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``arr ``=` `[``1``, ``1``, ``2``, ``5``, ``41``];  ` `    ``n ``=` `len``(arr) ` ` `  `    ``print``(CatalanSequence(arr, n));  ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG1  ` `{ ` ` `  `    ``static` `int` `MAX = 100000; ` ` `  `    ``// To store first N Catalan numbers  ` `    ``static` `long``[] catalan = ``new` `long``[MAX]; ` ` `  `    ``// Function to find first n Catalan numbers  ` `    ``static` `void` `catalanDP(``long` `n)  ` `    ``{ ` ` `  `        ``// Initialize first two values in table  ` `        ``catalan = catalan = 1; ` ` `  `        ``// Filong entries in catalan[]  ` `        ``// using recursive formula  ` `        ``for` `(``int` `i = 2; i <= n; i++)  ` `        ``{ ` `            ``catalan[i] = 0; ` `            ``for` `(``int` `j = 0; j < i; j++)  ` `            ``{ ` `                ``catalan[i] += catalan[j] * catalan[i - j - 1]; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Function to return the minimum changes required  ` `    ``static` `int` `CatalanSequence(``int` `[]arr, ``int` `n)  ` `    ``{ ` ` `  `        ``// Find first n Catalan Numbers  ` `        ``catalanDP(n); ` ` `  `        ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` ` `  `        ``// a and b are first two  ` `        ``// Catalan Sequence numbers  ` `        ``int` `a = 1, b = 1; ` ` `  `        ``// Insert first n catalan elements to set  ` `        ``s.Add(a); ` `        ``if` `(n >= 2)  ` `        ``{ ` `            ``s.Add(b); ` `        ``} ` ` `  `        ``for` `(``int` `i = 2; i < n; i++)  ` `        ``{ ` `            ``s.Add((``int``)catalan[i]); ` `        ``} ` ` `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` ` `  `            ``// If catalan element is present  ` `            ``// in the array then remove it from set  ` `            ``if` `(s.Contains(arr[i]))  ` `            ``{ ` `                ``s.Remove(arr[i]); ` `            ``} ` `        ``} ` ` `  `        ``// Return the remaining number of  ` `        ``// elements in the set  ` `        ``return` `s.Count; ` `    ``} ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[]arr = {1, 1, 2, 5, 41}; ` `        ``int` `n = arr.Length; ` ` `  `        ``Console.WriteLine(CatalanSequence(arr, n)); ` `    ``} ` `}  ` ` `  `// This code contributed by mits `

## PHP

= 2)
{
array_push(\$s, \$b);
}

for (\$i = 2; \$i < \$n; \$i++) { array_push(\$s, \$catalan[\$i]); } \$s = array_unique(\$s); for (\$i = 0; \$i < \$n; \$i++) { // If catalan element is present // in the array then remove it from set if (in_array(\$arr[\$i], \$s)) { unset(\$s[array_search(\$arr[\$i], \$s)]); } } // Return the remaining number of // elements in the set return count(\$s); } // Driver code \$arr = array(1, 1, 2, 5, 41); \$n = count(\$arr); print(CatalanSequence(\$arr, \$n)); // This code contributed by mits ?>

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.