From 81f5d47a49b862bdd04320a2e3484bad633140bd Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 15 Jul 2016 18:52:18 +0530 Subject: [PATCH 1/3] commit1-overlappingrectangles --- OverlappingRectangles.java | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 OverlappingRectangles.java diff --git a/OverlappingRectangles.java b/OverlappingRectangles.java new file mode 100644 index 0000000..eb6751d --- /dev/null +++ b/OverlappingRectangles.java @@ -0,0 +1,51 @@ +/** + * Created by Vatsal Gosaliya on 15-Jul-16. + * + * PROBLEM: Given are two rectangles defined over the 2-D cartesian coordinate system. + * Each rectangle is expressed using the top-left corner, width and height. + * The aim is to detect whether they overlap, and compute the overlapping + * area, if they do. + */ + +class OverlappingRectangles { + private double x,y,width,height; + + OverlappingRectangles(double x, double y, double width, double height){ + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + OverlappingRectangles intersection(OverlappingRectangles rect2) { + double newX = Math.max(this.x, rect2.x); + double newY = Math.min(this.y, rect2.y); + //System.out.println("newX = "+newX); + //System.out.println("newY = "+newY); + + double newWidth = Math.min(this.x + this.width, rect2.x + rect2.width) - newX; + double newHeight = newY - Math.max(this.y - this.height, rect2.y - rect2.height); + //System.out.println("newWidth = "+newWidth); + //System.out.println("newHeight = "+newHeight); + + if (newWidth <= 0d || newHeight <= 0d){ + System.out.print("No intersection."); + return null; + } + + return new OverlappingRectangles(newX, newY, newWidth, newHeight); + } + + + public double getArea(){ + return this.width*this.height; + } + + public static void main(String args[]){ + OverlappingRectangles r1 = new OverlappingRectangles(2,8,3,4); + OverlappingRectangles r2 = new OverlappingRectangles(1,7,6,2); + OverlappingRectangles r = r1.intersection(r2); + double areaOfIntersection = r == null ? 0 : r.getArea(); + System.out.print("Area of intersection : "+areaOfIntersection); + } +} From 3cd5313c09dc3f89b14b1830166722e06faef6c4 Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 15 Jul 2016 19:01:45 +0530 Subject: [PATCH 2/3] commit2-frogjumps --- FrogJumps.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 FrogJumps.java diff --git a/FrogJumps.java b/FrogJumps.java new file mode 100644 index 0000000..ded5251 --- /dev/null +++ b/FrogJumps.java @@ -0,0 +1,32 @@ +/** + * Created by Vatsal on 15-Jul-16. + * PROBLEM: Given the current position(x) of a frog and destination(y), + * compute the number of jumps required to reach from x to y, + * if the covers D blocks in a single jump. + */ + +class FrogJumps { + + public int solution(int x, int y, int D){ + int jumps; + + if(x==y) return 0; + + else{ + int distance = Math.abs((y-x)); + if(distance%D==0) jumps = distance/D; + else jumps = (distance/D)+1; + + return jumps; + } + } + + public static void main(String[] args) { + FrogJumps t = new FrogJumps(); + int jumps = t.solution(0, 10, 3); + System.out.println("No. of jumps = "+jumps); + + + } + +} From 5491ba1ae813e9639eaea530d276dfcc9a9dec6d Mon Sep 17 00:00:00 2001 From: Vatsal Gosaliya Date: Fri, 15 Jul 2016 19:21:23 +0530 Subject: [PATCH 3/3] commit3-BalancedParantheses --- BalancedParantheses.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BalancedParantheses.java diff --git a/BalancedParantheses.java b/BalancedParantheses.java new file mode 100644 index 0000000..f48ce38 --- /dev/null +++ b/BalancedParantheses.java @@ -0,0 +1,37 @@ +/** + * Created by Vatsal Gosaliya on 15-Jul-16. + * + * PROBLEM: Check if the given string contains a balanced pattern of parantheses. + */ + +import java.util.Stack; +class BalancedParantheses { + + public boolean solution(String a){ + int size = a.length(); + int check = 0; + Stack s = new Stack(); + + for(int i=0;i