PracticeSet DS Solved
PracticeSet DS Solved
DATA STRUCTURE
SOLVED BY:
PROF. BRIJENDRA SIR (BRAJ KUMAR)
(जावा का बाप)
“क्यूं पड़े हो चक्कर में, कोई नह ूं है टक्कर में”
YouTube Channel:
https://youtube.com/@roomno547
PRACTICE SET DSA
Question 1: Arrange all the given time complexities in ascending order. O(n), O(n^2), O(n!), O(log n), O(3^n), O(n log
n), O(10), O(n^4)
Solution: Here are the given time complexities arranged in ascending order:
1. O(10)
2. O(log n)
3. O(n)
4. O(n log n)
5. O(n^2)
6. O(n^4)
7. O(3^n)
8. O(n!)
Question 2: Consider the linear arrays A[ 3:24], B[-3:12]. Find the number of elements in each array.
Solution:
Question 3: Suppose an array, B[-5 ..... +4 ] having Base address (BA) = 100 and size of an element = 4 bytes, find the
location of B[-2].
Solution:
Location=Base Address+(Index−Lower Bound)×Size of an Element
Location of B[−2]=100+((−2)−(−5))×4
Location of B[−2]=112
So, the location of B[−2] is 112.
Question 4: Apply the binary search on the given numbers to find location of 25 (Show all steps). Given Array:
2,4,7,12,14,17,23,25,35,41,47
Solution:
Given array: 2, 4, 7, 12, 14, 17, 23, 25, 35, 41, 47
Result:
The target value 25 is found at index 7.
System.out.println(binarySearch(arr,target));
}
}
NOTE:
int mid = (st+end)/2; // --> this formula sometimes result to overflow & give error
int mid = st + (end-st)/2; // better way to find mid
Question 5: Sort the given numbers using bubble sort(show all steps). Given Array: 15,32,27,31,8,11
Solution:
Given array: 15, 32, 27, 31, 8, 11
Pass 1:
Compare 15 and 32. No swap needed. Array: 15, 32, 27, 31, 8, 11
Compare 32 and 27. Swap needed. Array: 15, 27, 32, 31, 8, 11
Compare 32 and 31. Swap needed. Array: 15, 27, 31, 32, 8, 11
Compare 32 and 8. Swap needed. Array: 15, 27, 31, 8, 32, 11
Compare 32 and 11. Swap needed. Array: 15, 27, 31, 8, 11, 32
Pass 2:
Compare 15 and 27. No swap needed. Array: 15, 27, 31, 8, 11, 32
Compare 27 and 31. No swap needed. Array: 15, 27, 31, 8, 11, 32
Compare 31 and 8. Swap needed. Array: 15, 27, 8, 31, 11, 32
Compare 31 and 11. Swap needed. Array: 15, 27, 8, 11, 31, 32
Pass 3:
Compare 15 and 27. No swap needed. Array: 15, 27, 8, 11, 31, 32
Compare 27 and 8. Swap needed. Array: 15, 8, 27, 11, 31, 32
Compare 27 and 11. Swap needed. Array: 15, 8, 11, 27, 31, 32
Pass 4:
Compare 15 and 8. Swap needed. Array: 8, 15, 11, 27, 31, 32
Compare 15 and 11. Swap needed. Array: 8, 11, 15, 27, 31, 32
Pass 5:
Compare 8 and 11. No swap needed. Array: 8, 11, 15, 27, 31, 32
Now, the array is sorted: 8, 11, 15, 27, 31, 32. The bubble sort algorithm has completed sorting the given numbers.
BUBBLE SORT PROGRAM(Additional):
import java.util.Scanner;
BubbleSort(arr);
System.out.println("Sorted Array: ");
for (int i =0; i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
Question 6: Show all the steps of Tower of Hanoi to move 4 disks from source peg to destination peg.
Solution:
Now, all 4 disks have been successfully moved from peg A to peg C using peg B as a helper.
Solution:
package PracticeSet;
import java.util.Scanner;
Output:
Question 8: Write a java program to find sum of digits of an integer using tail recursion.
Solution:
import java.util.Scanner;
public class Solution8 {
public static int sumOfDigit(int n ){
if (n>=0 && n<=9){
return n;
}
return sumOfDigit(n/10) + n%10;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
System.out.print("Sum of digits of "+n+" is: ");
System.out.println(sumOfDigit(n));
}
}
Output:
Question 9: Write a Java program to replace a specific element with an other element in array each time.
Solution:
package PracticeSet;
import java.util.Arrays;
import java.util.Scanner;
Solution:
Question 11: Write a Java program to print elements which are appeared single time in array of integers.
Solution:
package PracticeSet;
import java.util.Arrays;
public class Solution11 {
public static void main(String[] args) {
int []arr = {1,2,2,3,4,5,5};
System.out.println("Array: "+ Arrays.toString(arr));
for(int i =0;i<arr.length;i++){
int count =0;
int key = arr[i];
for (int j=0;j<arr.length;j++){
if (i!=j && key==arr[j]){
count++;
break;
}
}
if (count==0){
System.out.println(arr[i]);
}
}
}
}
Question 12: Write a program in Java to find the smallest element in array using recursion.
Solution:
package PracticeSet;
public class Solution12 {
static int getMin(int arr[], int n) {
if (n == 1)
return arr[0];
Solution:
package PracticeSet;
Solution:
Pseudocode:
• Initialize an array array with the values [2, 4, 3, 6, 8, 5, 10].
• Set the target product to 20.
• Print the original array: array.
• Create two nested loops to iterate through the array:
o Outer loop: Iterate from index 0 to array.length - 1.
o Inner loop: Iterate from index 1 to array.length for each iteration of the outer loop.
• Inside the inner loop, check if the product of the current pair (array[i] and array[j]) is equal to the target
product (20). a. If the product is equal to the target product, print the pair: (" + array[i] + ", " + array[j] + ")".
package PracticeSet;
import java.util.Arrays;
Solution:
package PracticeSet;
public class Solution15 {
public static void main(String[] args) {
int[] array1 = {2, 4, 6, 8, 10};
int[] array2 = {4, 8, 12, 14, 16};
System.out.println("Array 1: ");
printArray(array1);
System.out.println("\nArray 2: ");
printArray(array2);
Solution:
package PracticeSet;
import java.util.Arrays;
import java.util.Scanner;
System.out.println(Arrays.toString(result));
}
}
Output:
Question 17: Solve a Tower of Hanoi problem for n disks and 4 towers.
Answer:
package PracticeSet;
Output:
Question 18: Drive the formula for address calculation of 3-D array in row major order.
Answer:
Question 19: Write a program in Java to find sum of odd elements of even rows and even elements of odd rows.
Answer:
package PracticeSet;
Output:
Question 20: Write a Java program to print first row from left to right then last column from top to bottom then last
row from right to left then first column from bottom to top in NxN matrix.
Answer:
package PracticeSet;
Output: