0% found this document useful (0 votes)
6 views5 pages

Test 5 Solution

Uploaded by

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

Test 5 Solution

Uploaded by

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

MCQ :-

1.Predict the o/p-56 null


2.Keyword -this
3. Predict the o/p-first second third
4. Predict the o/p-c.color=”black”

Assignment:-
1. Matrix
public class mat{
int[][] matrix;

mat(int[][] mat) {
matrix = mat;
}

public static mat add(mat a, mat b) {


int n = a.matrix.length;
int m = a.matrix[0].length;
int[][] ans = new int[n][m];
mat res = new mat(ans);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];
}
}
return res;

public static mat multiply(mat a, mat b) {


int n1 = a.matrix.length;
int m1 = a.matrix[0].length;
int n2 = b.matrix.length;
int m2 = b.matrix[0].length;
int[][] ans = new int[n1][m2];
mat res = new mat(ans);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
for (int k = 0; k < n2; k++) {
res.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
}
}
}
return res;
}

public static mat transpose(mat m) {


int n1 = m.matrix.length;
int m1 = m.matrix[0].length;
int temp;
for (int i = 0; i < n1; i++) {
for (int j = i; j < m1; j++) {
temp = m.matrix[i][j];
m.matrix[i][j] = m.matrix[j][i];
m.matrix[j][i] = temp;
}
}
return m;
}

public static mat rotate(mat m) {


int n1 = m.matrix.length;
int m1 = m.matrix[0].length;
int temp;
// First take Transpose
for (int i = 0; i < n1; i++) {
for (int j = i; j < m1; j++) {
temp = m.matrix[i][j];
m.matrix[i][j] = m.matrix[j][i];
m.matrix[j][i] = temp;
}
}
// Second replace ith row with (n-i)th
for (int i = 0; i < n1 / 2; i++) {
for (int j = 0; j < m1; j++) {
temp = m.matrix[i][j];
m.matrix[i][j] = m.matrix[n1 - i - 1][j];
m.matrix[n1 - i - 1][j] = temp;
}
}
return m;
}

public void print() {


for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
2. Next Number
public class Solution {

public static LinkedListNode<Integer> addOneUtil(LinkedListNode<Integer> head) {


LinkedListNode<Integer> res = head;
LinkedListNode<Integer> temp = null, prev = null;

int carry =1, sum;

while(head != null)
{
sum = carry + head.data;
carry = (sum>=10) ? 1:0;

sum = sum%10;

head.data = sum;

temp = head ;
head = head.next;

LinkedListNode<Integer>newNode = null;
if(carry > 0)
newNode = new LinkedListNode<Integer>(carry);
temp.next = newNode;

return res;

public static LinkedListNode<Integer> reverse(LinkedListNode<Integer> head){


LinkedListNode<Integer> prev = null;
LinkedListNode<Integer> current = head ;
LinkedListNode<Integer> fwd= null;
while(current != null){
fwd = current.next;
current.next = prev;
prev = current;
current = fwd;
}
return prev;
}

public static LinkedListNode<Integer> nextLargeNumber(LinkedListNode<Integer> head) {


head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}

3.Sort Linked List


public class Solution {

public static LinkedListNode<Integer> sort(LinkedListNode<Integer> head) {


//Your code goes here

LinkedListNode<Integer> current = head, index = null;


int temp;
if(head == null){
return head;
}
else{
while(current != null){
index = current.next;
while(index != null){
if(current.data> index.data)
{
temp = current.data;
current.data = index.data;
index.data = temp;
}
index = index.next;
}
current = current.next;
}
}
return head;
}

4.Replace duplicate values

public class solution {

public static void changeLL(LinkedListNode<Integer> head) {


// Write your code here
int max = 0;
LinkedListNode<Integer> temp = head;
while(temp != null) {
if(temp.data > max) {
max = temp.data;
}
temp = temp.next;
}
boolean done[] = new boolean[max + 1];
for(int i = 0; i <= max; i++) {
done[i] = false;
}
temp = head;
while(temp != null) {
if(done[temp.data]) {
temp.data = max + 1;
max++;
} else {
done[temp.data] = true;
}
temp = temp.next;
}
}
}

You might also like