Dsa Assignment
Dsa Assignment
Dsa Assignment
else
{
if (curr_count > max_count)
{
max_count = curr_count;
reserve = arr[i - 1];
}
curr_count = 1;
}
}
{
max_count = curr_count;
reserve= arr[n - 1];
}
return reserve;
}
}
Question : 2
Check Whether an
Undirected Graph is a Tree
or Not
import java.util.*;
Source(int vertexCount) {
this.vertexCount = vertexCount;
this.adj = new LinkedList[vertexCount];
for (int i = 0; i < vertexCount; ++i) {
adj[i] = new LinkedList<Integer>();
}
}
// return;
//}
adj[v].add(w);
adj[w].add(v);
}
private boolean isCyclic(int v, boolean visited[], int parent) {
// Write code here
visited[v] = true;
Integer i;
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext())
{
i = it.next();
if (!visited[i])
{
if (isCyclic(i, visited, v))
return true;
else if (i != parent)
return true;
}
return false;
visited[i] = false;
return true;
}
int n = sc.nextInt();
k = sc.nextInt();
int stream[] = new int[n];
stream[i] = sc.nextInt();
}
}
return heap.peek();
}
Question : 4
Sort Nearly Sorted Array
import java.util.*;
PriorityQueue<Integer> priorityQueue
= new PriorityQueue<>();
priorityQueue.add(arr[i]);
int index = 0;
arr[index++] = priorityQueue.peek();
priorityQueue.poll();
priorityQueue.add(arr[i]);
while (itr.hasNext()) {
arr[index++] = priorityQueue.peek();
priorityQueue.poll();
}
public static void main(String[] args) {
int n = sc.nextInt();
int k = sc.nextInt();
arr[i] = sc.nextInt();
sortArray(arr,n, k);
for (int i = 0; i < arr.length; i++) {
}
Question : 5
Find Sum Between pth and
qth Smallest Elements
import java.util.*;
Arrays.sort(arr);
int result=0;
for(int i=p;i<q-1;i++)
result+= arr[i];
return result;
arr[i] = sc.nextInt();
int p = sc.nextInt();
int q = sc.nextInt();
System.out.println(sumBetweenPthToQthSmallestElement(arr, p, q));
Question : 6
Find All Symmetric Pairs in
an Array
import java.util.*;
import java.util.HashMap;
HashMap<Integer,Integer>hashing=new HashMap<Integer,Integer>();
for(int i=0;i<arr.length;i++){
int first = arr[i][0];
int second=arr[i][1];
Integer value=hashing.get(second);
if(value!=null && value==first)
System.out.println(second+" "+first);
else
hashing.put(first, second);
}
}
symmetricPair(arr);
}
}
Question : 7
Find All Common Element in
All Rows of Matrix
import java.util.*;
return;
mp.put(mat[0][j],1);
{
if (mp.get(mat[i][j]) != null && mp.get(mat[i][j]) == i)
mp.put(mat[i][j], i + 1);
if (i == row - 1)
matrix[i][j] = sc.nextInt();
printElementInAllRows(matrix);
}
Question : 8
Find Itinerary in Order
import java.util.*;
reverseMap.put(entry.getValue(), entry.getKey());
if (!reverseMap.containsKey(entry.getKey()))
start = entry.getKey();
break;
}
}
if (start == null)
System.out.println("Invalid Input");
return;
String to = tickets.get(start);
System.out.println(start + "->" + to );
start = to;
to = tickets.get(to);
int n = sc.nextInt();
tickets.put(sc.next(),sc.next());
}
findItinerary(tickets);
question :9
import java.util.*;
public static int search(int arr[], int left, int right, int key) {
return -1;
int mid = (left + right) / 2;
if (arr[mid] == key)
return mid;
int n = sc.nextInt();
int arr[] = new int[n];
arr[i] = sc.nextInt();
if (i != -1) {
System.out.println(i);
} else {
System.out.println("-1");
}
}
Question : 10
Find Median After
Merging Two Sorted
Arrays
import java.util.*;
int j = 0;
int count;
if (i == n)
median1 = median2;
median2 = arr2[0];
break;
else if (j == n)
median1 = median2;
median2 = arr1[0];
break;
{
median1 = median2;
median2 = arr1[i];
i++;
else
median1 = median2;
median2 = arr2[j];
j++;
}
}
int n = sc.nextInt();
arr1[i] = sc.nextInt();
arr2[i] = sc.nextInt();