Skip to content

Instantly share code, notes, and snippets.

View rfaisal's full-sized avatar

Faisal Rahman rfaisal

  • Facebook
  • Vancouver, BC
View GitHub Profile
@rfaisal
rfaisal / temp.java
Last active December 18, 2015 22:29
Class A{
public int a;
//dfgdf
}
package dynamic_programming;
/**
* Calculate the Fibonacci numbers up to n.
* When calculated, any Fibonacci number <=n can be returned without recalculating.
* Class Contributors: Faisal Rahman
* @author Faisal Rahman
*
*/
public class Fibonacci {
public class RemoveDuplicates
{
public string removeDuplicates(string input)
{
StringBuilder sb = new StringBuilder(input);
int index=0;
for (int i = 1; i<sb.Length; i++)
{
if(sb[index]!=sb[i]){
index++;
@rfaisal
rfaisal / DancingSentence.java
Last active December 19, 2015 09:09
DancingSentence: TopCoder SRM 279
//Link: https://github.com/rfaisal/hellouniverse/blob/master/Java/src/strings/DancingSentence.java
public static String convert(String s){
StringBuilder sb = new StringBuilder(s);
boolean dancingFlag=true;
for(int i=0;i<sb.length();i++){
if(sb.charAt(i)==' ')
continue;
else if(dancingFlag)
sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
else
@rfaisal
rfaisal / MultipleOf3And5.java
Created July 5, 2013 17:12
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
public class MultipleOf3And5 {
private int max;
public MultipleOf3And5(int max){
this.max=max;
}
public int calculate(){
int sum=0;
int j=1;
int i=15;
while(i<max){
@rfaisal
rfaisal / LargestPrimeFactor.java
Last active December 19, 2015 09:49
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
public class LargestPrimeFactor {
private long num;
public LargestPrimeFactor(long num){
this.num=num;
}
public long calculate(){
for(long i=1;i<=num/2;i++)
if(num%i==0)
if(isPrime(num/i))
return num/i;
public class PalindromeMaker {
public String make(String baseString) throws Exception{
int[] hash= new int[26];
for(int i=0;i<baseString.length();i++){
char ch=baseString.charAt(i);
if(ch<'A' || ch>'Z') throw new Exception("Wrong Input");
hash[ch-'A']++;
}
boolean check=false;
StringBuilder begin=new StringBuilder();
@rfaisal
rfaisal / Codeforces6C.java
Last active January 10, 2020 10:53
Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the pla…
/**
* Problem Link: http://codeforces.com/problemset/problem/6/C
* Alice = getDivideIndex(..)+1;
* Bob = arrayLength - Alice;
*/
public class Codeforces6C {
private static int getDivideIndexRec(int[] arr, int i, int j, boolean isLeft){
if(i==j){
if(isLeft) return i-1;
else return i;
@rfaisal
rfaisal / AmountApproximation.java
Created July 8, 2013 19:11
We must pay D dollars. Unfortunately, we only have bills of two denominations: p1 dollars and p2 dollars. So, we want to overpay as little as possible. You will be given ints D, p1 and p2. Return the minimum number of dollars greater than or equal to D that can be paid with the given bills. Assume that we have an infinite supply of both p1 and p…
public class AmountApproximation {
public static int approximate(int D, int p1, int p2){
int max_p1=(int)(D/p1)+1;
int max_p2=(int)(D/p2)+1;
int min=Integer.MAX_VALUE;
for(int i=0;i<=max_p1;i++){
for(int j=0;j<=max_p2;j++){
int sum=p1*i+p2*j;
if(sum>=D && sum<min)
min=sum;