Skip to content

Instantly share code, notes, and snippets.

View shailrshah's full-sized avatar
🖥️
Available

Shail Shah shailrshah

🖥️
Available
View GitHub Profile
@shailrshah
shailrshah / BinarySearch.java
Last active October 11, 2017 13:14
Binary Search
static int binarySearchIterative(int[] a, int key) {
int low = 0;
int high = a.length - 1;
while(low <= high) {
int mid = (low + high) / 2;
if(key == a[mid])
return mid;
else if (key < a[mid])
@shailrshah
shailrshah / InPlaceRev.java
Created February 6, 2017 22:42
Reversing each alphabet of the words, Keeping the order of the words and whitespaces intact
import java.util.Scanner;
class InPlaceRev{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sbAns = new StringBuilder();
int i = 0;
while(i < str.length()){
StringBuilder sbRev = new StringBuilder();
@shailrshah
shailrshah / Roman.java
Last active March 5, 2017 00:26
Convert Roman numerals to Arabic ones
import java.util.HashMap;
import java.util.Random;
/**
* Created by shail on 03/04/17.
*/
public class Roman {
public static int toArabic(String roman) {
HashMap<Character, Integer> hm = new HashMap();
hm.put('M', 1000);
/**
* Created by shail on 03/04/17.
*/
public class Likes {
public static String likes1(String[] people){
if(people.length == 0)
return "No one likes this";
else if(people.length == 1)
return people[0] + " likes this";
@shailrshah
shailrshah / maxSubArray.java
Last active November 13, 2017 17:28
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6
public int maxSubArray(int[] nums) {
if(nums.length == 0) return 0;
int localMax = nums[0];
int globalMax = localMax;
for(int i = 1; i < nums.length; i++) {
localMax = Math.max(nums[i], localMax + nums[i]);
globalMax = Math.max(localMax, globalMax);
}
@shailrshah
shailrshah / MatRotate.java
Last active October 8, 2017 22:34
2D Matrix Clockwise Rotation
/**
* Created by shail on 4/10/17.
*/
class MatRotate {
static void toString(int[][]a, String message) {
System.out.println(message);
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[0].length; j++)
System.out.print(a[i][j]);
@shailrshah
shailrshah / Substring.java
Last active November 13, 2017 18:00
Find out if s2 is a substring of s1
/**
* Created by shail on 4/11/17.
*/
public class Substring {
static boolean isStartsWith(int start, String s1, String s2){
int n = s2.length();
for(int i1 = start, i2 = 0; i2 < n; i1++, i2++)
if(s1.charAt(i1) != s2.charAt(i2))
return false;
@shailrshah
shailrshah / WordReverse.java
Created April 14, 2017 18:11
Reverse all Words of a given String
public class WordReverse {
static String reverse(String str){
char[] word = str.toCharArray();
for(int i = 0; i < word.length/2; i++){
char temp = word[i];
word[i] = word[word.length-1-i];
word[word.length-1-i] = temp;
}
@shailrshah
shailrshah / Sort.java
Last active September 30, 2017 02:39
Sort a list of objects by a property of the class
import java.util.Comparator;
import java.util.Arrays;
class Sorting {
public static void main(String[] args) {
Person p1 = new Person("Chagan", 145, 49);
Person p2 = new Person("Lagan", 150, 49);
Person p3 = new Person("Magan", 154, 22);
Person p4 = new Person("Gagan", 155, 34);
Person[] people = new Person[]{p1, p2, p3, p4};
@shailrshah
shailrshah / Stairs.java
Created September 29, 2017 23:32
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer.
class Stairs {
static HashMap<Integer, Integer> hm;
public int climbStairs(int n) {
hm = new HashMap<>();
hm.put(0, 1);
return helper(n);
}