Algo
Algo
Given an array of integers, write an algorithm and a program to left rotate the array by specific
number of elements.*/
Step1: START
Step2: Input: An array arr, starting index start, and ending index end.
Step3: While start is less than end:
Step4: Swap the elements at start and end.
Step5: Swap the elements at start and end.
Step6: Increment start by 1
Step7: Decrement end by 1.
Step8: Output: The array arr with the elements between start and end reversed.
Step9: STOP
Step10: START
Step11: Input: An array arr of size n, and the number of positions k to rotate.
Step12: Calculate k as k % n to handle cases where k is greater than n.
Step13: Reverse the first k elements of the array.
Step14: Reverse the remaining n-k elements of the array.
Step15: Reverse the entire array.
Step16: Output: The array arr rotated to the left by k positions.
Step17: STOP
Step1: START
Step2: Input: An array arr, size n, elements a and b.
Step3: Initialize last_pos_a and last_pos_b to -1, and dis to n.
Step4: For each element in the array: If the element is equal to a: Update last_pos_a to the current index.
Step5: If last_pos_b is not -1: Calculate the distance between last_pos_a and last_pos_b.
Step6: If the calculated distance is less than dis: Update dis to the calculated distance.
Step7: If the element is equal to b: Update last_pos_b to the current index.
Step8: If last_pos_a is not -1: Calculate the distance between last_pos_b and last_pos_a.
Step9: If the calculated distance is less than dis: Update dis to the calculated distance.
Step10: Output: Return dis if it is not equal to n, otherwise return -1.
Step11: STOP
Step1: START
Step2: Input: Number of test cases T.
Step3: For each test case:
Step4: Read the size of the array n.
Step5: Read the elements of the array arr.
Step6: Read the elements a and b for finding the distance.
Step7: Call Minimum_Distance to find the minimum distance between a and b.
Step8: If the distance is not -1: Print the minimum distance between a and b.
Step9: Else: Print that either a or b is not present in the array.
Step10: Output: The minimum distance between a and b for each test case.
Step11: STOP
Step1: START
Step2: Input: An array arr and its size n.
Step3: Initialize res to 0.
Step4: For each element in the array: Perform XOR operation between res and the current element.
Update res with the result of the XOR operation.
Step5: Output: Return res, which is the element that occurs an odd number of times.
Step6: STOP
Step1: START
Step2: Input: Number of test cases T.
Step3: For each test case:
Read the number of elements in the array n.
Read the elements of the array arr.
Call odd to find the element that occurs an odd number of times.
Step4: If the returned number is 0: Print "No element occurs an odd number of times."
Step5: Else:Print the number that occurred an odd number of times.
Step6: STOP
Set found to 1.
Break the loop.
Else if arr[i][j] is greater than key:
Decrement j by 1.
Else:
Increment i by 1.
5. If found is 1:
Print "Element is present".
6. Else:
Print "Element is not present".
7. STOP
If arr[i][j] is 1:
Increment count.
Decrement j by 1.
If a is less than count:
Set a to count.
Set ans to i + 1.
Increment i by 1.
5. If ans is not -1:
Print "Row with maximum number of 1's is: ans".
6. Else:
Print "No 1's are present in the matrix".
7. STOP
1. START
2. Create: Initialize an empty stack with a given size.
3. Push(k):
Return 0 (unbalanced).
Pop the top element from the stack.
If the popped element does not match the closing bracket:
Return 0 (unbalanced).
5. If the stack is empty:
Return 1 (balanced).
6. Else:
Return 0 (unbalanced).
7. STOP
If the character is (:
Increment left by 1.
Else:
Increment right by 1.
If left is equal to right:
Calculate len as 2 * right.
If len is greater than max_len:
Return.
4. Push the character str[c] onto the stack.
5. Call reverse recursively with c + 1.
6. If c is less than len:
Pop the top character from the stack and assign it to str[len - c - 1].
7. STOP
/*Question 11: Design an algorithm and a program to implement two stacks by using only one array i.e.
both the stacks should use the same array for push and pop operation. Array should be divided in such a
manner that space should be efficiently used if one stack contains very large number of elements and
other one contains only few elements.
1. START
2. Input: Size of the array size.
3. Initialize top1 to -1 and top2 to size.
4. Push1(value):
1. START
2. Input: Number of test cases T.
3. For each test case:
1. START
2. Create():
Set front to 0.
Increment rear by 1 and assign k to queue[rear].
4. DeQueue():
If the queue is empty:
Print "Queue is empty".
Else:
Print the dequeued element queue[front] and increment front by 1.
5. IsEmpty():
If front is -1 or front is greater than rear:
Return true.
Else:
Return false.
6. Size():
Return rear - front + 1.
7. STOP
1. START
2. Input: Size of the queue n.
3. Create: Initialize front to -1, rear to -1, and top to -1.
4. EnQueue(k):
If rear is equal to n - 1:
Set front to 0.
Increment rear by 1 and assign k to queue[rear].
5. DeQueue():
If the queue is empty:
Print "Queue underflow".
Else:
Print the dequeued element queue[front] and increment front by 1.
If front is greater than rear:
Set front and rear to -1.
6. Push(k):
If top is equal to n - 1:
Print "Stack overflow".
Else:
Increment top by 1 and assign k to stack[top].
7. Pop():
If the stack is empty:
Print "Stack underflow".
Else:
Return stack[top] and decrement top by 1.
8. Reverse():
While the queue is not empty:
Dequeue an element and push it onto the stack.
While the stack is not empty:
Pop an element and enqueue it back to the queue.
9. STOP
1. START
2. Create: Initialize front to -1 and rear to -1.
3. InsertFront(k):
1. START
2. Create: Initialize front to -1 and rear to -1.
3. EnQueue(k):
1. START
2. Create: Initialize top1 to -1 and top2 to -1.
3. Push(stack, k):
Set size to s.
Initialize front and rear to -1.
11. EnQueue(k):
If the queue is full:
1. START
2. Create(k):
1. START
2. Create():
1. START
2. Create():
1. START
2. Create(k):
Call InsertEnd(k).
Else:
Set the new node's next pointer to the current node's next pointer.
Set the new node's prev pointer to the current node.
If the current node's next is not NULL:
/*Q 23: Given a doubly linked list, design an algorithm and write a program to reverse this list without
using any extra space. */START
1. START
2. Input: A sorted doubly linked list list.
3. If list->head is NULL:
Return.
4. Initialize current to list->head.
5. While current is not NULL and current->next is not NULL:
If current->data is equal to current->next->data:
1. START
2. Create(k):
/*Q 26: Design an algorithm and write a program to concatenate two circularly linked lists */)
1. START
2. Input: Two circularly linked lists list1 and list2.
3. If both list1 and list2 are empty:
1. START
2. Input: A circularly linked list list, a position position, and two empty circularly linked lists list1 and list2.
3. If list is empty:
1. START
2. Input: A linked list head.
3. Initialize oddList and evenList to NULL.
4. Initialize oddTail and evenTail to NULL.
5. Initialize a counter count to 1.
6. While head is not NULL:
If count is odd:
If oddList is NULL:
1. START
2. Input: A linked list head and an integer n.
3. Initialize two pointers, first and second, to head.
4. Initialize a counter count to 0.
5. If head is NULL:
If first is NULL:
1. START
2. Input: A linked list head.
3. Initialize prev to NULL, current to head, and next to NULL.
4. While current is not NULL:
1. START
2. CreateTree(data):
Return CreateTree(data).
If data is less than root->data:
Set root->left to InsertNode(root->left, data).
Else:
Set root->right to InsertNode(root->right, data).
Return root.
4. FindMinNode(root):
While root is not NULL and root->left is not NULL:
Move root to root->left.
Return root.
5. DeleteNode(root, data):
If root is NULL:
Return root.
If data is less than root->data:
Set root->left to DeleteNode(root->left, data).
Else if data is greater than root->data:
Set root->right to DeleteNode(root->right, data).
Else:
If root->left is NULL:
1. START
2. Input: A binary tree root.
3. If root is not NULL:
Call Inorder(root->left).
Print root->data.
Call Inorder(root->right).
4. STOP
Postorder Traversal
1. START
2. Input: A binary tree root.
3. If root is not NULL:
Call Postorder(root->left).
Call Postorder(root->right).
Print root->data.
4. STOP
Preorder Traversal
1. START
2. Input: A binary tree root.
3. If root is not NULL:
Print root->data.
Call Preorder(root->left).
Call Preorder(root->right).
4. STOP
1. START
2. Input: An infix expression infix.
3. Initialize an empty stack stack and an empty string postfix.
4. For each character current in infix:
If current is an operand:
1. START
2. Create(data):
Return Create(data).
If data is less than root->data:
Set root->left to Insert(root->left, data).
Else:
Set root->right to Insert(root->right, data).
Return root.
4. FindMin(root):
While root is not NULL and root->left is not NULL:
Move root to root->left.
Return root.
5. Delete(root, data):
If root is NULL:
Return root.
If data is less than root->data:
Set root->left to Delete(root->left, data).
Else if data is greater than root->data:
Set root->right to Delete(root->right, data).
Else:
If root->left is NULL:
1. START
2. Input: A binary search tree root.
3. InOrderTraversal(root, arr, index):
If root is NULL:
Return.
Call InOrderTraversal(root->left, arr, index).
Set arr[index++] to root->data.
Call InOrderTraversal(root->right, arr, index).
4. SortedArrayToBST(arr, start, end):
If start is greater than end:
Return NULL.
Set mid to (start + end) / 2.
Create a new node root with arr[mid].
Set root->left to SortedArrayToBST(arr, start, mid - 1).
Set root->right to SortedArrayToBST(arr, mid + 1, end).
Return root.
5. ConvertToBalancedBST(root):
Initialize an array arr and an index index to 0.
Call InOrderTraversal(root, arr, &index).
Return SortedArrayToBST(arr, 0, index - 1).
6. FindMin(root):
While root is not NULL and root->left is not NULL:
Move root to root->left.
Return root.
7. FindMax(root):
While root is not NULL and root->right is not NULL:
Move root to root->right.
Return root.
8. STOP
1. START
2. Input: A binary search tree root.
3. If root is NULL:
Return -1.
4. Initialize a queue queue and set front and rear to -1.
5. Enqueue root to queue.
6. Initialize maxLevel to 0, maxNodes to 0, and currentLevel to 0.
7. While front is less than rear:
Set nodeCount to rear - front.
Increment currentLevel by 1.
Initialize nodesAtCurrentLevel to 0.
While nodeCount is greater than 0:
1. START
2. Create(capacity):
1. START
2. CreateNode(data):
Return 1.
If index is greater than or equal to nodeCount:
Return 0.
Return isCompleteBinaryTree(root->left, 2 * index + 1, nodeCount) and isCompleteBinaryTree(root->right,
2 * index + 2, nodeCount).
4. isMaxHeap(root):
If root is NULL:
Return 1.
If root->left is not NULL and root->data is less than root->left->data:
Return 0.
If root->right is not NULL and root->data is less than root->right->data:
Return 0.
Return isMaxHeap(root->left) and isMaxHeap(root->right).
5. countNodes(root):
If root is NULL:
Return 0.
Return 1 + countNodes(root->left) + countNodes(root->right).
6. isHeap(root):
If root is NULL:
Return 1.
Set nodeCount to countNodes(root).
Set index to 0.
Return isCompleteBinaryTree(root, index, nodeCount) and isMaxHeap(root).
7. STOP
1. START
2. CreateNode(data):
Return.
Set largest to root.
If root->left is not NULL and root->left->data is greater than largest->data:
Set largest to root->left.
If root->right is not NULL and root->right->data is greater than largest->data:
Set largest to root->right.
If largest is not root:
Swap root->data with largest->data.
Call Heapify(largest).
5. BuildMaxHeap(root):
If root is NULL:
Return.
Call BuildMaxHeap(root->left).
Call BuildMaxHeap(root->right).
Call Heapify(root).
6. ExtractMax(root):
If root is NULL:
Return NULL.
Swap root->data with root->left->data.
Move root to root->left.
Call Heapify(root).
Return root.
7. FindKthLargest(root, k):
Call BuildMaxHeap(root).
Initialize count to 0.
While count is less than k:
Set root to ExtractMax(root).
Increment count by 1.
Return root->data.
8. STOP