From 41e388b2c69d49c1f6ec04ba8f158a70f862b6c9 Mon Sep 17 00:00:00 2001 From: ATRI2107 Date: Sat, 8 Aug 2020 14:33:39 +0530 Subject: [PATCH 1/2] Code for minimum divisor such that sum of array is less than or equal to threshold --- .../MinimumDivisorThreshold.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 problems/src/binary_search/MinimumDivisorThreshold.java diff --git a/problems/src/binary_search/MinimumDivisorThreshold.java b/problems/src/binary_search/MinimumDivisorThreshold.java new file mode 100644 index 00000000..89d032d3 --- /dev/null +++ b/problems/src/binary_search/MinimumDivisorThreshold.java @@ -0,0 +1,33 @@ +import java.util.*; +class abc +{ + public static void main(String[] args) { + int n; + Scanner sc=new Scanner(System.in); + n=sc.nextInt(); + int a[]=new int[n]; + for(int i=0;i Date: Sat, 8 Aug 2020 15:08:27 +0530 Subject: [PATCH 2/2] Vertical Traversal of Binary Tree from left to right and top to bottom --- .../src/tree/BinaryTreeVertcalTraversal.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 problems/src/tree/BinaryTreeVertcalTraversal.java diff --git a/problems/src/tree/BinaryTreeVertcalTraversal.java b/problems/src/tree/BinaryTreeVertcalTraversal.java new file mode 100644 index 00000000..89a0c12e --- /dev/null +++ b/problems/src/tree/BinaryTreeVertcalTraversal.java @@ -0,0 +1,77 @@ +import java.util.*; +class Node +{ + int data; + Node right,left; + Node(int d) + { + data=d; + } +} +class Tree{ + class Pair + { + Node node; + int y; // y denotes height of the Node + Pair(Node node,int y) + { + this.node=node; + this.y=y; + } + } + + void dfs(Node root,int x,int y,TreeMap> tm) + { + if(root==null) return; + if(tm.containsKey(x)) + { + tm.get(x).add(new Pair(root,y)); + } + else + { + ArrayList al=new ArrayList<>(); + al.add(new Pair(root, y)); + tm.put(x,al); + } + dfs(root.left,x-1,y+1,tm); + dfs(root.right,x+1,y+1,tm); + } + ArrayList> vertical(Node root) + { + TreeMap> tm=new TreeMap<>(); + dfs(root, 0, 0, tm); + ArrayList> res=new ArrayList<>(); + for(Map.Entry> m:tm.entrySet()) + { + ArrayList temp=m.getValue(); + Collections.sort(temp,new Comparator() { + public int compare(Pair p1,Pair p2) + { + int k=Integer.compare(p1.y,p2.y); //First sort according to the height + return (k==0)?Integer.compare(p1.node.data,p2.node.data):k; //If height is equal then sort according to the node value + } + }); + ArrayList temp1=new ArrayList<>(); + for(Pair i:temp) + { + temp1.add(i.node.data); + } + res.add(temp1); + } + return res; + } +} +class abc +{ + public static void main(String[] args) { + Node root=new Node(1); + root.left=new Node(2); + root.left.left=new Node(4); + root.left.right=new Node(6); + root.right=new Node(3); + root.right.left=new Node(5); + root.right.right=new Node(7); + ArrayList> al=new Tree().vertical(root); + System.out.println(al); + } +} \ No newline at end of file