DSA Mid01 Fall'22 Solution
DSA Mid01 Fall'22 Solution
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively. The numbers in array
A appear in ascending order while the numbers in array B appear in descending order. Write a user
defined function to produce third array C by merging arrays A and B in ascending order. Use A, B
and C as arguments in the function.
Solution in C
Solution:
#include<iostream>
void Merge(int A[], int B[], int C[], int N, int M, int &K);
int main()
cin>>n;
for(i=0;i<n;i++)
cin>>A[i];
1
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
}
cin>>m;
for(i=0;i<m;i++)
cin>>B[i];
Merge(A,B,C,n,m,k);
for(i=0;i<k;i++)
cout<<C[i]<<" ";
return 0;
void Merge(int A[], int B[], int C[], int N, int M, int &K)
K=0;
if (A[I]<B[J])
C[K++]=A[I++];
else if (A[I]>B[J])
C[K++]=B[J--];
else
C[K++]=A[I++];
2
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
J--;
C[K++]=A[T];
C[K++]=B[T];
Given a square maze containing positive numbers, find a path from the corner cell (marked as 2 in
bold) to the middle cell (marked as 0 in bold). You can move exactly ‘n’ steps from any cell in two
directions i.e. right and down. where n is value of the cell. For instance, if a cell has a value 2, the
number 2 indicates that movement along 2 cells are allowed. These 2 cells can be taken in any
combination and in any of the allowable direction. For instance, 1 step right and 1 step down will be
allowed; however, 2 cells right and 2 cells down will not be allowed as this will count to 4 steps in
total. The movement should not exceed the boundary.
Your task is to write a function using recursion with backtracking to find a path from corner cell to
middle cell in maze.
i=0 2 2 4 4 3
i=1 3 4 4 2 2
i=2 1 1 0 3 2
i=3 3 2 2 1 1
i=4 3 3 4 3 1
Where cell (0,0) with value 2 is the source and the destination is (2,2) with value 0.
Solution
#include <stdio.h>
3
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
#define N 5
printf("\n");
return true;
return false;
int sol[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
4
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
if (findpath(maze, 0, 0, sol) == false) {
return false;
printPath(sol);
return true;
if (x == 2 && y == 2)
solution[x][y] = 1;
return true;
if (Safe(maze, x, y) == true) {
solution[x][y] = 1;
5
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
return true;
return true;
solution[x][y] = 0;
return false;
return false;
int main()
{ 3, 0, 1, 1, 3 },
{ 0, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 0 },
{ 1, 0, 0, 1, 0 }};
// { 3, 4, 4, 2, 2 },
6
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
// { 1, 1, 0, 3, 2 },
// { 3, 2, 2, 1, 1 },
// { 3, 3, 4, 3, 1 }};
// int matrix[N][N] ={ { 1, 0, 1, 0, 0 },
// { 1, 0, 1, 2, 2 },
// { 1, 0, 0, 3, 2 },
// { 1, 0, 1, 1, 1 },
// { 1, 0, 1, 3, 1 }};
soultionmatrix(matrix);
return 0;
Write a function to reverse the specified portion of the given linked list.
For instance:
Input:
Linked List: 1→ 2 → 3 → 4 → 5 → 6 → 7
Start position = 2
End position = 5
Output:
1→ 5 → 4 → 3 → 2 → 6 → 7
7
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
Solution
Algorithmic Steps:
// base case
if (m > n) {
return;
prev = curr;
curr = curr->next;
curr->next = end;
end = curr;
8
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
// move to the next node
curr = next;
/*
*/
if (start)
start->next = curr;
if (prev != NULL) {
prev->next = end;
else {
head = end;
Write a function that takes a NxN 2D array and its dimension N as parameters and sort the given
array such that after sorting the values in the array are in column-wise ascending order.
Example:
9
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])
Before sorting:
2 3 2 8
9 4 54 5
1 7 4 11
6 1 9 2
After sorting: 1 2 5 9
1 3 6 9
2 4 7 11
2 4 8 54
Solution:
The 2-D array can be converted into a 1-D array and the preferred sorting algorithm can be applied
10
Downloaded by k232001 Muhammad Muzammil Siddiqui ([email protected])