RP
BAN USERpublic static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println(calculate(1, 2, 3, 4));
}
private static Integer calculate(int a, int b, int c, int d) throws InterruptedException, ExecutionException {
ExecutorService executerService = Executors.newFixedThreadPool(2);
Future<Integer> asyncResult1 = executerService.submit(() -> a + b);
Future<Integer> asyncResult2 = executerService.submit(() -> c + d);
executerService.shutdown();
return asyncResult1.get() * asyncResult2.get();
}
@Sibendu Dey, Its like there are n nested loops iterating until sum. but for each iteration we are interested only in printing the sum in n values. so are waiting for two values to calculate from the loops and as the length turns to n-1 calculating the third/index value.
However we don't need to recurse just to print so removing that as well. Here it is.
public static void recursivePrint(int sum, int n) {
int data[] = new int[sum];
int len = 0;
int originalSum = sum;
recursivePrintImproved(sum, n, data, len, originalSum, n);
}
private static void recursivePrintImproved(int sum, int n, int data[],
int len, int originalSum, int originalN) {
// System.out.println("len: " + len + " sum: " + sum + " i: "
// + (originalSum - sum) + " n:" + n);
for (int i = 0; i <= sum; i++) {
int sum1 = 0;
if (len == originalN - 1) {
for (int j = 0; j < len; j++) {
sum1 += data[j];
System.out.print(data[j] + " ");
}
data[len] = originalSum - sum1;
System.out.print(data[len] + " ");
System.out.println();
break;
} else {
data[len] = i;
recursivePrintImproved(sum - i, n - 1, data, len + 1,
originalSum, originalN);
}
}
}
void printArrayMultiplicationBI(int[] array) {
BigInteger multiplication = new BigInteger("1");
for (int a : array) {
multiplication = multiplication.multiply(new BigInteger(Integer.toString(a)));
}
for (int a : array) {
System.out.print(multiplication.divide(new BigInteger(Integer.toString(a)))+" ");
}
}
public static int greatestSmallerTidyNumber(int n) {
int k = -1;
char[] chars = String.valueOf(n).toCharArray();
boolean foundbig = false;
for (int i = 0; i < chars.length - 1; i++) {
if (foundbig) {
chars[i] = '9';
continue;
}
if (chars[i] > chars[i + 1]) {
k = i;
while (--k >= 0) {
if (chars[k] != chars[i]) {
break;
}
}
i = k + 1;
if (chars[i] == '1') {
chars[i] = '9';
} else {
--chars[i];
}
foundbig = true;
}
}
if (foundbig) {
chars[chars.length - 1] = '9';
}
if (Integer.valueOf(new String(chars).trim()) > n) {
chars[chars.length - 1] = ' ';
}
return Integer.valueOf(new String(chars).trim());
}
@NoOne, here is the improved one which will work for all edge cases.
public static int greatestSmallerTidyNumber(int n) {
int k = -1;
char[] chars = String.valueOf(n).toCharArray();
boolean foundbig = false;
for (int i = 0; i < chars.length - 1; i++) {
if (foundbig) {
chars[i] = '9';
continue;
}
if (chars[i] > chars[i + 1]) {
k = i;
while (--k >= 0) {
if (chars[k] != chars[i]) {
break;
}
}
i = k + 1;
if (chars[i] == '1') {
chars[i] = '9';
} else {
--chars[i];
}
foundbig = true;
}
}
if (foundbig) {
chars[chars.length - 1] = '9';
}
if (Integer.valueOf(new String(chars).trim()) > n) {
chars[chars.length - 1] = ' ';
}
return Integer.valueOf(new String(chars).trim());
}
public void recursivePrint(int sum, int n) {
int data[] = new int[sum];
int len = 0;
int originalSum = sum;
recursivePrintImproved(sum, n, data, len, originalSum, n);
}
private void recursivePrintImproved(int sum, int n, int data[], int len, int originalSum, int originalN) {
if (n == 0) {
for (int j = 0; j < len; j++) {
System.out.print(data[j] + " ");
}
System.out.println();
return;
}
for (int i = 0; i <= sum; i++) {
int sum1 = 0;
if (len == originalN - 1) {
for (int j = 0; j < len; j++) {
sum1 += data[j];
}
i = originalSum - sum1;
}
data[len] = i;
recursivePrintImproved(sum - i, n - 1, data, len + 1, originalSum, originalN);
}
}
static class TreeNode {
TreeNode left;
TreeNode right;
int data;
}
public static int smallestGreaterInteger(TreeNode root, int k) {
int smallestGreaterInteger = -1;
while (root != null) {
if (root.data > k) {
smallestGreaterInteger = root.data;
root = root.left;
} else {
root = root.right;
}
}
return smallestGreaterInteger;
}
- RP April 21, 2017public static int greatestSmallerTidyNumber(int n) {
char[] chars = String.valueOf(n).toCharArray();
boolean foundbig = false;
int lastEqualIndex = -1;
for (int i = 0; i < chars.length - 1; i++) {
if (foundbig) {
chars[i] = '9';
continue;
}
if (chars[i] == chars[i + 1]) {
lastEqualIndex = i;
continue;
}
if (chars[i] > chars[i + 1]) {
if (lastEqualIndex != -1 && lastEqualIndex == i - 1) {
--chars[i - 1];
}
if (chars[i] == '1') {
chars[i] = '9';
} else {
--chars[i];
}
foundbig = true;
}
}
if (foundbig) {
chars[chars.length - 1] = '9';
}
if (Integer.valueOf(new String(chars).trim()) > n) {
chars[chars.length - 1] = ' ';
}
return Integer.valueOf(new String(chars).trim());
}
- RP March 26, 2018