Wednesday, September 28, 2022

Merge 2 sorted linked list in reverse order

Given two linked lists of size N and M, which are sorted in non-decreasing order. The task is to merge them in such a way that the resulting list is in decreasing order.

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains length of both linked lists N and M respectively. Next two lines contains N and M elements of two linked lists.

Output:
For each testcase, print the merged linked list which is in non-increasing order.

User Task:
The task is to complete the function mergeResult() which takes reference to the heads of both linked list and returns the pointer to the merged linked list.

Constraints:
1 <=T<= 50
1 <= N, M <= 1000

Example:
Input:

2
4 3
5 10 15 40 
2 3 20
2 2
1 1
2 4

Output:
40 20 15 10 5 3 2
4 2 1 1 

Explanation:
Testcase 1:
 After merging the two lists in decreasing order, we have new lists as 40->20->15->10->5->3->2.


My Answer:

    Node mergeResult(Node node1, Node node2)
    {
	    ArrayList<Integer> arrayList = new ArrayList<>();
	    while( node1 != null || node2 != null) {
                if( node1 == null ) {
                    arrayList.add(node2.data);
                    node2 = node2.next;
                } else if( node2 == null ) {
                    arrayList.add(node1.data);
                    node1 = node1.next;
                } else if ( node1.data <= node2.data ) {
	                arrayList.add(node1.data);
	                node1 = node1.next;
	        } else {
	            arrayList.add(node2.data);
	            node2 = node2.next;
	        }
	    }
	    
	    Node node = new Node(arrayList.get(arrayList.size()-1));
	    Node prevNode = node;
	    for (int i = arrayList.size()-2; i >= 0 ; i-- ) {
	        Node nextNode = new Node(arrayList.get(i));
	        prevNode.next = nextNode;
	        prevNode = nextNode;
	    }
	    
	    return node;
    }

Monday, September 26, 2022

Remove every k'th node

 Problem:

Given a singly linked list, your task is to remove every kth node from the linked list.
Input:
The first line of input contains number of test cases T. Then T test cases follow. Every test case contains 3 lines. First line of every test case contains an integer N denoting the size of the linked list . The second line contains N space separated values of the linked list. The third line contains an integer K.
Output:
Output for each test case will be space separated values of the nodes of the new transformed linked list.
Example:
Input:
2
8
1 2 3 4 5 6 7 8
3
4
1 2 3 4
2
Output:
1 2 4 5 7 8
1 3


My Answer:

  Node delete(Node head, int k)
    {
	// Your code here	 
	    if (head == null || k == 0) return head;
	    if ( k == 1 ) return null;
	    
	    int nth = 1;
	    Node node = head;
	    while (node.next != null) {
	        if ( (nth+1) % k == 0 ) {
	            if ( node.next.next != null) {
	                node.next = node.next.next;
	            } else {
	                node.next = null;
	                break;
	            }
	            
	            nth=nth+1;
	        }
	        nth = nth + 1;
	        node = node.next;
	    }
	    return head;
    }

Saturday, September 24, 2022

Missing number in array

Problem:

Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.

Example 1:

Input:
N = 5
A[] = {1,2,3,5}
Output: 4

My Answer:

    int MissingNumber(int array[], int n) {
        // Your Code Here
        int sumN = 0;
        int sumArr = 0;
        
        for(int i=1 ; i <= n ; i++) {
            sumN = sumN + i;
        }
        
        for (int j=0 ; j < n-1 ; j++ ) {
            sumArr = sumArr + array[j];
        }
        
        return sumN - sumArr;
    }

Is Binary Number Multiple of 3

Problem:

Given a binary number, Find, if given binary number is a multiple of 3. The given number can be big upto 2^10000. It is recommended to finish the task using one traversal of input binary string.

Example 1:

Input: S = "011"
Output: 1
Explanation: "011" decimal equivalent
is 3, which is divisible by 3.

My Answer:

    int isDivisible(String s) {
        // code here\
        java.math.BigInteger bInt = new java.math.BigInteger(s, 2);
        
        
        if ( bInt.mod(new java.math.BigInteger("3")) == java.math.BigInteger.ZERO ) {
            return 1;
        }
        
        return 0;
    }

Search in a matrix

  Problem: Given a matrix  mat [][] of size  N  x  M , where every row and column is sorted in increasing order, and a number  X  is given. ...