diff --git a/java-program/FlattenLL.java b/java-program/FlattenLL.java new file mode 100644 index 0000000..c46ad1d --- /dev/null +++ b/java-program/FlattenLL.java @@ -0,0 +1,50 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +// QUESTION +// You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. +// Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list. + + + + +/* +class Node { + public int val; + public Node prev; + public Node next; + public Node child; +}; +*/ + + +//FUNCTION IMPLEMENTATION + public Node flatten(Node head) { + if( head == null) return head; + + Node p = head; + while( p!= null) { + /* CASE 1: if no child, proceed */ + if( p.child == null ) { + p = p.next; + continue; + } + + /* CASE 2: got child, find the tail of the child and link it to p.next */ + Node temp = p.child; + // Find the tail of the child + while( temp.next != null ) + temp = temp.next; + // Connect tail with p.next, if it is not null + temp.next = p.next; + if( p.next != null ) p.next.prev = temp; + // Connect p with p.child, and remove p.child + p.next = p.child; + p.child.prev = p; + p.child = null; + } + return head; + } \ No newline at end of file diff --git a/java-program/JumpGame.java b/java-program/JumpGame.java new file mode 100644 index 0000000..b1d4cc4 --- /dev/null +++ b/java-program/JumpGame.java @@ -0,0 +1,52 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +import java.util.*; +public class JumpGame{ + + public static void main(String args[]){ + JumpGame s = new JumpGame(); + int arr[] = {2,4,6,3,4,6}; + + if(s.isPosssible(arr)){ + System.out.println(s.minJumps(arr));} + else{ + System.out.println("Not Possible"); + } + } + + + public boolean isPosssible(int[] nums) { + + int n = nums.length; + + //last index from where we can reach to end cell + //As from last index (n - 1) itself reach to end cell, + int lastIndex = n - 1; + + for(int i = n - 1; i >= 0; i--){ + if(i + nums[i] >= lastIndex){ + lastIndex = i; + } + } + + //check from 0 reach to end cell + return lastIndex == 0; + } + + + +public int minJumps(int[] A) { + int jumps = 0, curEnd = 0, curFarthest = 0; + for (int i = 0; i < A.length - 1; i++) { + curFarthest = Math.max(curFarthest, i + A[i]); + if (i == curEnd) { + jumps++; + curEnd = curFarthest; + } + } + return jumps; +} \ No newline at end of file diff --git a/java-program/MergeKSortedLists.java b/java-program/MergeKSortedLists.java new file mode 100644 index 0000000..84b6a76 --- /dev/null +++ b/java-program/MergeKSortedLists.java @@ -0,0 +1,57 @@ +// QUESTION +// You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. +// Merge all the linked-lists into one sorted linked-list and return it. + + + + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + + +//FUNCTION IMPLEMENTATION + +private static ListNode mergeKLists(ListNode[] lists) { + return partition(0, lists.length - 1, lists); +} + +private static ListNode partition(int l, int r, ListNode[] lists) { + if (r < l) return null; + if (r == l) return lists[r]; + if (r - l == 1) return merge(lists[l], lists[r]); + + var m = (l + r) / 2; + var left = partition(l, m, lists); + var right = partition(m + 1, r, lists); + + if (right == null) return left; + if (left == null) return right; + + return merge(left, right); +} + +private static ListNode merge(ListNode l1, ListNode l2) { + var dummy = new ListNode(); + var curr = dummy; + + while (l1 != null && l2 != null) + { + if (l1.val < l2.val) + { curr.next = l1; l1 = l1.next; } + else + { curr.next = l2; l2 = l2.next; } + curr = curr.next; + } + + curr.next = l1 != null ? l1 : l2; + + return dummy.next; +} \ No newline at end of file diff --git a/java-program/NoOfProvinces.java b/java-program/NoOfProvinces.java new file mode 100644 index 0000000..466fb84 --- /dev/null +++ b/java-program/NoOfProvinces.java @@ -0,0 +1,75 @@ + + +// QUESTION +// There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c. +// A province is a group of directly or indirectly connected cities and no other cities outside of the group. +// You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise. +// Return the total number of provinces. + + + +//FUNCTION IMPLEMENTATION + +class Solution { + public int findCircleNum(int[][] M) { //<-entry + + int[] unionFindSet = new int[M.length]; + for (int i = 0; i< unionFindSet.length; i++){ + unionFindSet[i] = -1; + } + + for (int i = 0; i < M.length; i++) { + for (int j = i + 1; j < M.length; j++) { + if (M[i][j] == 1) { + union(unionFindSet, i, j); + } + } + } + + int count = 0; + for (int i = 0; i< unionFindSet.length; i++){ + if (unionFindSet[i] < 0) count++; + } + return count; + + } + + + public static int findRoot(int[] unionSet, int v) { + int index = v; + int value = unionSet[v]; + while (value > 0) { + index = value; + value = unionSet[value]; + } + + return index; + } + + public static void union(int[] unionSet, int first, int second) { + int x = findRoot(unionSet, first); + int y = findRoot(unionSet, second); + + if(x == y) { + return; + } + if (Math.abs(unionSet[x]) <= Math.abs(unionSet[y])) { + unionSet[y] -= Math.abs(unionSet[x]); + unionSet[x] = y; + while ( first != y) { + int parent = unionSet[first]; + unionSet[first] = y; + first = parent; + } + } else { + unionSet[x] -= Math.abs(unionSet[y]); + unionSet[y] = x; + while ( second != x) { + int parent = unionSet[second]; + unionSet[second] = x; + second = parent; + } + } + } + +} \ No newline at end of file diff --git a/java-program/ReverseNodesinKGroup.java b/java-program/ReverseNodesinKGroup.java new file mode 100644 index 0000000..e372b36 --- /dev/null +++ b/java-program/ReverseNodesinKGroup.java @@ -0,0 +1,51 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + + +// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. +// k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. +// You may not alter the values in the list's nodes, only nodes themselves may be changed. + + +public /*class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + }*/ + +//FUNCTION IMPLEMENTATION + public ListNode reverseKGroup(ListNode head, int k) { + int c=k; + ListNode h=head; + ListNode s=head; + ListNode p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + k--; + if(k==0){ + h.next=reverseKGroup(next,c); + return s; + } + s=next; + } + + if(s==null&&k!=0){ + s=p; + p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + s=next; + + }return p; + }return null; + } +} \ No newline at end of file