Multilevel recursion in swift
Here we will discuss two level recursion in swift. If there is statement before the recursive function it will print in sequence & vice versa. Here is example code with print statement –
Data structure & algorithm using swift language.
Here we will discuss two level recursion in swift. If there is statement before the recursive function it will print in sequence & vice versa. Here is example code with print statement –
[Question]: We haveGiven an array, generate all the possible subarrays of the given array using recursion for example :
[Question ] Check weather array is palindrome Example : [1,2,2,1] ===> It’s palindrome Solution#: Here we are checking first & last element equality & increase till mid of array
[Question] : Reverse array using recursion Example: [1,2,3] = [3,2,1]. Approach #1 using two variables : – // Approach#2: Using single pointer/variable so we written base case which blocks on n/2, we are just increasing the element by +1 on every recursion till half of the array.
# Question 1: Find sum of first n numbers. Example :– n = 3 => 3+2+1 =6. We will use recursion to solve this problem. # Question 2: Find sum of array elements using recursion approach :
So here we have used recursion The names which is before function will print FIFO & vice versa
[Question]: Check if the given string has balanced parenthesis //Example 1: // //Input: s = “()” //Output: true //Example 2: // //Input: s = “[](){}” //Output: true //Example 3: // //Input: s = “(}” //Output: false
We will create a stack where we perform push, pop and other operations. Here we have covered question – Print Reverse Stacks More can be find here 2. Check Balance Parentheses.
[Question] In a given array return true if array have duplicate elements . For example 1 :[1,2,3,1] = True example 1 :[1,2,3,] = False So here we have used Dictionary which is Hashable which contains only unique keys hence we can find duplicates elements in swift.
[Question] Find Maximum and minimum of an array in swift using minimum number of comparisons #Approach: 1 Use first element as min max & start loop from second element onwards & return tuple of the min max number