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/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