Skip to content

Instantly share code, notes, and snippets.

@mrjeremyt
Created July 1, 2015 00:36
import java.util.*;
/**
* Created by MrJeremyT on 6/30/15.
*/
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static void main(String[] args){
System.out.println(solution(4, 17));
System.out.println(solution(1213));
System.out.println(solution(3));
int[] array = {-1, 1, 3, 3, 3, 2, 1, 0, -1};
System.out.println(solution(array));
}
public static int solution(int[] A) {
// write your code in Java SE 8
int total = 0;
int result = 0;
for (int end = 0, begin = 0; end < A.length && begin < A.length; ) {
begin = end + 2;
total = A[begin - 1] - A[end];
boolean ok = false;
for (int i = 0; (begin < A.length) && ((A[begin] - A[begin - 1]) == total); ) {
ok = true;
i++;
end = begin;
begin++;
result += i;
if (result > 1000000000) return -1;
}
if (!ok) end++;
}
return result;
}
public static int solution(int N) {
// write your code in Java SE 8
if (N == 0) return 1;
long sum = 0;
long val = N*N;
for(int i = 0; i <= val/4; i++) {
sum += val / (4 * i + 1) - val / (4 * i + 3);
if (sum > 1000000000) return -1;
}
return (int)sum * 4 + 1;
}
public static int solution(int N){
int num = String.valueOf(N).length();
int factorial = 1;
for (int i = num; i > 2; i--){
factorial *= i;
}
return factorial;
}
public static int solution(int A, int B) {
// write your code in Java SE 8
int result = 0;
for (int j = A; j <= B; j++) {
for (int i = 2; i <= (j / 2); i++) {
if (j % i == 0){
if ((i*i) == j){
result++;
break;
}
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment