3.recursion Answers
3.recursion Answers
PART 1
#include <stdio.h>
if (n == 0)
return 0;
void main()
scanf("%d", &n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d", &arr[i]);
Output (Program 1) :-
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
PART 1
#include <stdio.h>
return;
/*Swapping characters at start and end indices and recursively calling reverseString for the
remaining substring*/
str[start] = str[end];
str[end] = temp;
void main()
char str[100];
str[strcspn(str, "\n")] = 0;
int n = strlen(str);
reverseString(str, 0, n-1);
Output (Program 2) :-
PART 1
#include <stdio.h>
start++;
end--;
return 1;
return 0;
// Recursive case
void main()
char str[100];
str[strcspn(str, "\n")] = 0;
Output (Program 3) :-
Case 1:
Case 2:
Is "Man" a palindrome? No
PART 1
#include <stdio.h>
if (exponent == 0)
return 1;
else
void main()
double base;
int exponent;
scanf("%lf", &base);
scanf("%d", &exponent);
Output (Program 4) :-
Enter base: 2
Enter exponent: -3
2.000000^-3 = 0.125000
PART 1
#include <stdio.h>
if (n == 1)
return;
void main()
int n;
scanf("%d", &n);
Output (Program 5) :-
PART 1
#include <stdio.h>
#include <string.h>
*x = *y;
*y = temp;
if (l == r)
else
swap((str + l), (str + i)); // Swap current character with the start
void main()
// User input
str[strcspn(str, "\n")] = 0;
Output (Program 6) :-
ABC
ACB
BAC
BCA
CBA
CAB
PART 2
#include <stdio.h>
#include <stdlib.h>
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
insertNode(&head, value);
break;
case 2:
scanf("%d", &value);
deleteNode(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
break;
case 5:
exit(0);
default:
void menu()
printf("\nMenu:\n");
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
if (head == NULL)
return 0;
return 1 + lengthOfList(head->next);
Output (Program 1) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
PART 2
#include <stdio.h>
#include <stdlib.h>
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
insertNode(&head, value);
break;
case 2:
scanf("%d", &value);
deleteNode(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
scanf("%d", &value);
if (searchElement(head, value))
else
break;
case 5:
exit(0);
default:
void menu()
printf("\nMenu:\n");
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
if (head == NULL)
return 0;
if (head->data == value)
return 1;
Output (Program 2) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
PART 2
#include <stdio.h>
#include <stdlib.h>
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
Node* mergeSortedLists(Node *head1, Node *head2); //function to merge two sorted linked lists in
a sorted manner
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
scanf("%d", &pos);
if (pos == 1)
insertNode(&head1, value);
else if(pos == 2)
insertNode(&head2, value);
else
break;
case 2:
scanf("%d", &value);
scanf("%d", &pos);
if (pos == 1)
deleteNode(&head1, value);
else if(pos == 2)
deleteNode(&head2, value);
else
break;
case 3:
scanf("%d", &pos);
if (pos == 1)
displayList(head1);
else if(pos == 2)
displayList(head2);
else if(pos == 3)
displayList(head_merged);
else
break;
case 4:
exit(0);
default:
void menu()
printf("\nMenu:\n");
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
if (head1 == NULL)
return head2;
if (head2 == NULL)
return head1;
result = head1;
else
result = head2;
Output (Program 3) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
PART 2
#include <stdio.h>
#include <stdlib.h>
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
insertNode(&head, value);
break;
case 2:
scanf("%d", &value);
deleteNode(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
reverseLinkedList(&head);
break;
case 5:
exit(0);
default:
void menu()
printf("\nMenu:\n");
printf("3. Display the current list\n"); //TODO: the word "current" is added here
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
current = next;
*head = prev;
Output (Program 4) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
PART 2
#include <stdio.h>
#include <stdlib.h>
int c=0;
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
insertNode(&head, value);
c++;
break;
case 2:
scanf("%d", &value);
deleteNode(&head, value);
c--;
break;
case 3:
displayList(head);
break;
case 4:
findMiddleRecursive(head, 1);
break;
case 5:
exit(0);
default:
void menu()
printf("\nMenu:\n");
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
if(a==((c+1)/2))
return;
Output (Program 5) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
PART 3
#include <stdio.h>
#include <stdlib.h>
int data;
} Node; //making the alias, so that there's no need to write struct Node every time when required
// Function prototypes
void menu();
void main()
while (1)
menu();
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &value);
insertNode(&head, value);
break;
case 2:
scanf("%d", &value);
deleteNode(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
if(isPalindrome(&head, head))
else
break;
case 5:
exit(0);
default:
void menu()
printf("\nMenu:\n");
newNode->data = value;
newNode->next = NULL;
// When there's absolutely no Node present, then the first Node is the newly created Node, which
becomes the head
if (*head == NULL)
*head = newNode;
else
// Else the whole Linked List is traversed, until end, and then the newly created Node is added at
the end
Node *temp = *head; // Taking a pointer variable temp, so that the address in head doesn't
get altered after a Linked List traversal
temp = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
temp = temp->next;
printf("NULL\n");
if (right == NULL)
return 1;
if (!isPal)
return 0;
*left = (*left)->next;
return isEqual;
Output (Program 1) :-
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
Menu:
1. Array Sum
1. Base Case(s):
If there is only one element (`size == 1`), the sum is that element itself.
2. Problem Decomposition:
For an array of size `n`, the problem can be decomposed into calculating the sum of the first `n-1`
elements and then adding the `n`th element.
3. Composition of Solutions:
The solution to the problem is obtained by recursively summing the elements of the array from the
first to the second-last element, and then adding the last element (`arraySum(arr, n) = arr[n-1] +
arraySum(arr, n-1)`).
2. String Reversal
1. Base Case(s):
If the string is empty (`length == 0`) or has only one character (`length == 1`), it is already reversed.
2. Problem Decomposition:
The problem can be broken down by fixing the first character and reversing the rest of the string.
After reversing the substring that excludes the first character, the first character is appended to the
end of the reversed substring.
3. Composition of Solutions:
The solution to reversing the entire string is achieved by recursively reversing the substring that
excludes the first character and then appending the first character to the end (`reverseString(str) =
reverseString(str + 1) + str[0]`).
3. Palindrome Check
1. Base Case(s):
If the string is empty (`length == 0`) or has only one character (`length == 1`), it is a palindrome.
2. Problem Decomposition:
The problem can be reduced by checking if the first and last characters are the same and then
recursively checking if the substring that excludes these characters is a palindrome.
3. Composition of Solutions:
The solution is constructed by comparing the outermost characters and recursively confirming the
same for the inner substring (isPalindrome(str) = (str[0] == str[length-1]) &&
isPalindrome(str[1:length-2])`).
4. Power Function
1. Base Case(s):
If the exponent is `1`, the result is the base itself (`base^1 = base`).
2. Problem Decomposition:
The problem can be reduced by multiplying the base by the result of the power function with one
less exponent (`exponent - 1`).
3. Composition of Solutions:
The solution is obtained by recursively calculating the power for the reduced exponent and
multiplying by the base (`power(base, exponent) = base * power(base, exponent - 1)`).
5. Tower of Hanoi
Objective: Move `n` disks from a source peg to a destination peg using an auxiliary peg.
1. Base Case(s):
If there is only one disk (`n == 1`), move it directly from the source peg to the destination peg.
2. Problem Decomposition:
1. Move the top `n-1` disks from the source peg to the auxiliary peg.
2. Move the `n`th disk from the source peg to the destination peg.
3. Move the `n-1` disks from the auxiliary peg to the destination peg.
3. Composition of Solutions:
The solution is constructed by recursively solving for `n-1` disks and using the results to achieve the
movement of the `n`th disk and the subsequent reorganization (`solveHanoi(n) = solveHanoi(n-1,
source, auxiliary, destination) + move(n, source, destination) + solveHanoi(n-1, auxiliary, destination,
source)`).
6. Permutations
1. Base Case(s):
If the string is empty (`length == 0`) or has only one character (`length == 1`), the only permutation is
the string itself.
2. Problem Decomposition:
The problem can be decomposed by fixing one character and recursively finding all permutations of
the remaining substring. This process is repeated for each character in the string.
3. Composition of Solutions:
The solution is constructed by recursively generating permutations for the substrings and combining
them by prefixing each permutation with the fixed character (`permute(str) = {fixed_char +
permute(rest_of_str)}` for each character in the string).
Base Case(s):
If the linked list is empty (`head` is `NULL`) or has only one node, it is a palindrome.
Problem Decomposition:
To check if a linked list is a palindrome, we can use two pointers: a slow pointer that moves one step
at a time and a fast pointer that moves two steps at a time. This will help in reaching the middle of
the linked list.
The recursive decomposition involves comparing nodes from the start and end moving toward the
center of the list:
1. Identify the Middle: Using the two-pointer technique, the slow pointer reaches the middle when
the fast pointer reaches the end.
2. Recursively Compare Nodes: Start comparing the elements from the start and the end. This can
be achieved by using a helper function that passes a reference to the start of the list and a reference
that moves to the end of the list during each recursive call.
Composition of Solutions:
The recursive function checks if the elements at the front and the corresponding elements at the
back (as the recursion unfolds) are the same:
1. The function first recursively traverses to the end of the linked list.
2. On the way back, it compares each node from the end with the corresponding node from the
front using a reference or a pointer.
3. If all elements match, then the linked list is a palindrome; otherwise, it is not.