0% found this document useful (0 votes)
54 views8 pages

DS Assignment

The document contains sample code snippets and explanations for different algorithms questions: 1) It contains code for implementing a custom ArrayList with methods like add, remove, find etc along with a Student class and main method to test the ArrayList. 2) Recursive functions are defined to find the maximum element in an array, calculate power of a number recursively and solve the Tower of Hanoi problem recursively. 3) Time and space complexities are provided for some solutions along with outputs of sample runs.

Uploaded by

Saim Saudagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views8 pages

DS Assignment

The document contains sample code snippets and explanations for different algorithms questions: 1) It contains code for implementing a custom ArrayList with methods like add, remove, find etc along with a Student class and main method to test the ArrayList. 2) Recursive functions are defined to find the maximum element in an array, calculate power of a number recursively and solve the Tower of Hanoi problem recursively. 3) Time and space complexities are provided for some solutions along with outputs of sample runs.

Uploaded by

Saim Saudagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ans1)

(a)131 + 98 = 229

(b) 229 x 4 = 916

(c) 348 + (71 x 4) = 632

(d) 348 + (9 x 4) = 384

(e) (828 – 384) / 4 = 120th element = 21

Ans2)

(1) O(1)

(2) O(1)

(3) O(N)

(4) O(N)

(5) O(N)

(6) O(N)

(7) O(N)

(8) O(N)

(9) O(N)

(10)
import java.util.Scanner;
import java.util.Scanner;

public class Q2
{

public static class MyArrayList<T extends Comparable<T>>


{
T[] arr;
int length;

MyArrayList(int size) {
this.arr = (T[]) new Comparable[size];
this.length = 0;
}

public boolean isEmpty()


{
if(length == 0)
return true;
else
return false;
}

public int length()


{
return length;
}

public void print()


{
for(int i = 0; i < length; i++)
{
System.out.println(arr[i]);
}
}

public void addAtFront(T value)


{
if(length - 1 == arr.length - 1)
{
T[] arr1 = (T[]) new Comparable[arr.length * 2];

for(int i = 0; i < arr.length; i++)


{
arr1[i + 1] = arr[i];
length++;
}

arr1[0] = value;
arr = arr1;
length++;
}

if(length == 0)
{
arr[length] = value;
length++;
}
else
{
for(int i = length - 1; i > -1; i--)
{
arr[i + 1] = this.arr[i];
}

arr[0] = value;
length++;
}
}

public void addAtEnd(T value)


{
if (length - 1 == arr.length)
{
T[] arr1 = (T[]) new Comparable[arr.length * 2];
length = 0;
for (int i = 0; i < arr.length; i++)
{
arr1[i] = arr[i];
length++;
}
arr1[length] = value;
arr = arr1;
length++;
}
else
{
arr[length] = value;
length++;
}
}

public int find(T value)


{
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == value)
{
return i;
}
}
return -1;
}

public void reverse()


{
T temp;

if(arr.length % 2 == 0)
for (int i = 0; i < (arr.length / 2); i++)
{
temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
else
{
int s = (arr.length / 2) + 1;
for (int i = 0; i < s; i++)
{
temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}

public void removeFirst(T value)


{
int index = 0;

for(int i = arr.length - 1; i > -1; i--)


{
if(arr[i] == value)
{
index = i;
}
}

for (int i = index; i < arr.length - 1; i++)


{
arr[i] = arr[i + 1];
}
length--;
}

public void removeAll(T value)


{
boolean check = true;

while (check)
{
int index = 0;

for(int i = arr.length - 1; i > -1; i--)


{
if(arr[i] == value)
{
index = i;
}
else
{
index = -1;
}
}

if (index >= 0)
{
for (int i = index; i < arr.length - 1; i++)
arr[i] = arr[i + 1];
}
else {
check = false;
}
}
}
}

static class Student implements Comparable<Student>


{
String name;
int id;
int age;

public Student(String name, int id, int age)


{
this.name = name;
this.id = id;
this.age = age;
}

public int compareTo(Student std)


{
int found = 0;

if(age > std.age)


{
found = this.age;
}
else
{
found = std.age;
}
return found;
}

public String toString()


{
return "Name is " + name + " ID is " + id + " and age is " + age;
}

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array");
int len = sc.nextInt();
MyArrayList<Student> stdList = new MyArrayList<>(len);

System.out.println(stdList.isEmpty());

Student[] std = new Student[len];

std[0] = new Student("Saim", 1, 23);


std[1] = new Student("Sallama",2,55);
std[2] = new Student("Shehryar", 3, 43);
std[3] =new Student("Sadia", 4, 85);

Student temp;
for(int i = 0; i < stdList.length - 1; i++)
{
if(std[i].compareTo(std[i + 1]) < std[i + 1].age)
{
temp = std[i];
std[i + 1] = std[i];
std[i] = temp;
}
}

int n = std.length;

for(int i = 0; i < n; i++)


{
stdList.addAtEnd(std[i]);
}

System.out.println(stdList.isEmpty());
System.out.println(stdList.find(std[2]));

stdList.print();
stdList.reverse();
stdList.print();

System.out.println();

stdList.removeFirst(std[1]);
stdList.print();

System.out.println();

stdList.removeAll(std[0]);
stdList.print();

System.out.println(stdList.length());
}

Ans3)

(1)
public class Q3Ans1
{
public static void main(String[] args)
{
int[] arr = {1,23,5,4};

int n = FindMax(arr, arr.length - 1);

System.out.println(n);
}

public static int FindMax (int[] arr, int ind)


{
if(ind == 0)
{
return arr[0];
}

return Math.max(arr[ind] , FindMax(arr, ind - 1));


}
}

Time complexity = O(N)


Space complexity =4 x 4 = 16

(2)
public class Q3Ans2
{
public static void main(String[] args)
{
power(3, 4);
}

public static int power (int b, int p)


{
if(p == 0)
{
System.out.println(b + " power " + p + " is = " + 1);
return 1;
}

System.out.println(b + " power " + p + " is = " + Math.pow(b, p));


return power(b, p - 1);
}
}

p=4

return power(b, p - 1)

P=3

return power(b, p - 1)

p=2

return power(b, p - 1)

p=1

return 22.

p=0

return 1
(3)
public class Q3Ans3
{

static void towerOfHanoi(int n, char s, char des, char t)


{
if (n == 1)
{
System.out.println("Move disk 1 from rod " + s + " to rod " +
des);
return;
}
towerOfHanoi(n-1, s, t, des);
System.out.println("Move disk " + n + " from rod " + s + " to rod
" + des);
towerOfHanoi(n-1, t, des, s);
}

public static void main(String args[])


{
int n = 3;
towerOfHanoi(n, 'A', 'C', 'B');
}
}

(a) 6

(b) 1048574

(c) 4

You might also like