esoner.sezgin
BAN USERpublic class Selfwork {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(0, 10, 4, 6, 8, 1, 9, 2, 3, 7);
int k = 5;
int numberOfSubArrays = find(elements, 5);
System.out.println(numberOfSubArrays);
}
private static int find(List<Integer> elements, int k) {
List<Integer> eliminatedList = elements.stream().filter(e -> e < 5).collect(Collectors.toList());
int size = eliminatedList.size();
return combination(size, k);
}
private static int combination(int set, int k) {
if(k == 0) {
return 0;
}
int upResult = 1;
int downResult = 1;
int tempSet = set;
int tempK = k;
while(tempK >= 1) {
upResult *= tempSet--;
downResult *= tempK--;
}
int result = upResult / downResult;
return result + combination(set, --k);
}
}
public String calculate(String a, String b) {
int aLen = a.length();
int bLen = b.length();
if(aLen < bLen) {
int diffCount = bLen - aLen;
a = "0".repeat(diffCount) + a;
} else if(aLen > bLen) {
int diffCount = aLen - bLen;
b = "0".repeat(diffCount) + b;
}
Stack<String> xNum = new Stack<>();
Stack<String> yNum = new Stack<>();
for (char c : a.toCharArray()) {
xNum.add(String.valueOf(c));
}
for (char c : b.toCharArray()) {
yNum.add(String.valueOf(c));
}
return result = add("", xNum, yNum, "0");
}
public String add(String result, Stack<String> num1, Stack<String> num2, String carry) {
if(num1.isEmpty()) {
return result + carry;
}
String bit1 = num1.pop();
String bit2 = num2.pop();
long numberOfOnes = (bit1 + bit2 + carry).chars().filter(c -> c == '1').count();
String newCarry = null;
String tempResult = null;
if(numberOfOnes == 0) {
newCarry = "0";
tempResult = "0";
} else if(numberOfOnes == 1) {
newCarry = "0";
tempResult = "1";
} else if(numberOfOnes == 2) {
newCarry = "1";
tempResult = "0";
} else if(numberOfOnes == 3) {
newCarry = "1";
tempResult = "1";
}
return add(tempResult, num1, num2, newCarry) + result;
}
- esoner.sezgin September 16, 2020