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 / UnfriendlyNumbers.java
Last active December 20, 2015 02:28
There is one friendly number and N unfriendly numbers. We want to find how many numbers are there which exactly divide the friendly number, but does not divide any of the unfriendly numbers.
public class UnfriendlyNumbers {
private static long gcd(long a, long b){
if(b==0) return a;
else return gcd(b,a%b);
}
public static long calc(long[]arr, long k){
HashSet<Long> hs= new HashSet<Long>();
for(int i=0;i<arr.length;i++){
hs.add(gcd(arr[i],k));
}
@rfaisal
rfaisal / SumSquareDiff.java
Created July 24, 2013 18:23
Find the difference between the sum of the squares of the first n natural numbers and the square of the sum.
public class SumSquareDiff {
public static long calculate(int n){
int sum_of_sq=0;
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
sum_of_sq+=i*i;
}
return sum*sum-sum_of_sq;
}
@rfaisal
rfaisal / LargestProductOf_m_ConsecutiveDigits.java
Created July 24, 2013 18:34
Find the greatest product of five consecutive digits in the 1000-digit number.
public class LargestProductOf_m_ConsecutiveDigits {
public static int calculate(String s,int m) throws Exception{
int max=0;
for(int i=0;i<=s.length()-m;i++){
int prod=getProd(s, i, m);
if(prod>max) max=prod;
}
return max;
}
private static int getProd(String s, int i,int m) throws Exception{
@rfaisal
rfaisal / SumOfPrimes.java
Created July 24, 2013 23:48
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.
public class SumOfPrimes {
public static long calculate(int n){
long sum=2;
for(int i=3;i<n;i++)
if(isPrime(i))
sum+=i;
return sum;
}
private static boolean isPrime(int p){
for(int i=2;i<=Math.sqrt(p);i++)
@rfaisal
rfaisal / SpecialPythagoreanTripletProd.java
Created July 24, 2013 23:54
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2. Given n, find the Pythagorean triplet for which a + b + c = n, return abc. If none exists return 0.
public class SpecialPythagoreanTripletProd {
public static int calculate(int n){
for(int a=2;a<n;a++){
if((n*n-2*n*a)%(2*n-2*a)==0){
int b=(n*n-2*n*a)/(2*n-2*a);
int ret= a*b*(n-a-b);
if(ret>0) return ret;
}
}
return 0;//no Triplet
@rfaisal
rfaisal / StringSimilarity.java
Last active November 11, 2019 19:43
For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings “abc” and “abd” is 2, while the similarity of strings “aaa” and “aaab” is 3. Calculate the sum of similarities of a string S with each of it’s suffixes.
public class StringSimilarity {
public static int calculate(String s){
char[]arr=s.toCharArray();
int length=arr.length;
int count=length;
for(int i=1;i<length;i++){
int len=length-i;
int j=0;
for(;j<len;j++)
if(arr[j]!=arr[j+i]){
@rfaisal
rfaisal / HTMLTagsDetection.java
Last active December 20, 2015 12:09
Given an array of html as strings, print all the tags separated by ; lexicographically. Omit duplicates.
public class HTMLTagsDetection {
public static String toStringLexicographically(String[] htmls){
HashSet<String> tags= new HashSet<String>();
for(int i=0;i<htmls.length;i++){
detect(tags, htmls[i]);
}
String[] sorted=new String[tags.size()];
tags.toArray(sorted);
Arrays.sort(sorted);
return implodeArray(sorted,";");
@rfaisal
rfaisal / UtopianIdentificationNumber.java
Created August 1, 2013 05:53
A new identification number is given for every Citizen of the Country Utopia and it has the following format. The string must begin with between 0 and 3 (inclusive) lowercase letters. Immediately following the letters, there must be a sequence of digits. The length of this segment must be between 2 and 8, both inclusive. Immediately following th…
public class UtopianIdentificationNumber {
public static boolean test(String s){
int stage=1;
int count=0;
for(int i=0;i<s.length();i++){
char c= s.charAt(i);
if(stage==1){
if(!(c>='a' && c<='z')){
if((i-count)>=0 && (i-count)<=3){
count=i;
@rfaisal
rfaisal / SplittingCandies.java
Created September 8, 2013 22:59
Cyael is a teacher at a very famous school in Byteland and she is known by her students for being very polite to them and also to encourage them to get good marks on their tests. Then, if they get good marks she will reward them with candies :) However, she knows they are all very good at Mathematics, so she decided to split the candies evenly t…
import java.util.Scanner;
public class SplittingCandies {
public static long getStudentCandies(long N, long K){
if(K==0) return 0;
return N/K;
}
public static long getTeacherCandies(long N, long K){
if(K==0) return N;
return N%K;
@rfaisal
rfaisal / HelloHello.java
Created September 8, 2013 23:21
Chef talks a lot on his mobile phone. As a result he exhausts his talk-value (in Rokdas) very quickly. One day at a mobile recharge shop, he noticed that his service provider gives add-on plans which can lower his calling rates (Rokdas/minute). One of the plans said "Recharge for 28 Rokdas and enjoy call rates of 0.50 Rokdas/min for one month". …
import java.util.Scanner;
public class HelloHello {
private class Plan{
int activation;
double rates;
int planNo;
int minsPerMonths;
int months;
public Plan(int planNo,int activation,double rates,int minsPerMonths,int months){
this.planNo=planNo;