Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions BalancedParantheses.java
Original file line number Diff line number Diff line change
@@ -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<size;i++){
if(a.charAt(i)!='(' && a.charAt(i)!=')') check = -1;
if(a.charAt(i)=='(') s.push(a.charAt(i));
else{
if(!s.empty()) s.pop();
else return false;
}
}
if(check==-1) return false;
else{
if(s.empty()) return true;
else return false;
}

}
public static void main(String[] args) {
String a = "((()))";
boolean result = new BalancedParantheses().solution(a);
System.out.println(result);

}

}
32 changes: 32 additions & 0 deletions FrogJumps.java
Original file line number Diff line number Diff line change
@@ -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);


}

}
51 changes: 51 additions & 0 deletions OverlappingRectangles.java
Original file line number Diff line number Diff line change
@@ -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);
}
}