Logical task of increasing. How long does it take for bacteria? Each bacteria divides into 2 same size bacteria each second.
You can find the top number of 3.
Two strings s1 and s2 are given. You have both s1 and s2 as the s3 is minimum.
Example: apple pear => applear
How will you dictionary sort integers without converting them to strings?
For ex: 1 2 10 20 100 110 => 1 10 100 110 2 20.
Given two integer arrays A and B.
B contains exactly the same number as A except two additional numbers. Find the two elements with minimum time and space complexity.
for ex: A = {1, 4, 2, 6, 3}
B = {4, 0.7, 6, 3, 2, 1}
ans: 0 7
public class GFG_1 { String a , b; // Prints super sequence of a[0..m-1] and b[0..n-1] static void printSuperSeq(String a, String b) { int m = a.length(), n = b.length(); int[][] dp = new int[m+1][n+1]; // Fill table in bottom up manner for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { // Below steps follow above recurrence if (i == 0) dp[i][j] = j; else if (j == 0 ) dp[i][j] = i; else if (a.charAt(i-1) == b.charAt(j-1)) dp[i][j] = 1 + dp[i-1][j-1]; else dp[i][j] = 1 + Math.min(dp[i-1][j], dp[i][j-1]); } } // Create a string of size index+1 to store the result String res = ""; // Start from the right-most-bottom-most corner and // one by one store characters in res[] int i = m, j = n; while (i > 0 && j > 0) { // If current character in a[] and b are same, // then current character is part of LCS if (a.charAt(i-1) == b.charAt(j-1)) { // Put current character in result res = a.charAt(i-1) + res; // reduce values of i, j and indexs i--; j--; } // If not same, then find the larger of two and // go in the direction of larger value else if (dp[i-1][j] < dp[i][j-1]) { res = a.charAt(i-1) + res; i--; } else { res = b.charAt(j-1) + res; j--; } } // Copy remaining characters of string 'a' while (i > 0) { res = a.charAt(i-1) + res; i--; } // Copy remaining characters of string 'b' while (j > 0) { res = b.charAt(j-1) + res; j--; } // Print the result System.out.println(res); } /* Driver program to test above function */ public static void main(String args[]) { String a = "apple"; String b = "pear"; printSuperSeq(a, b); } }
Algorithm: Compare quotients and reminders import java.util.Arrays; import java.util.Comparator; public class DictionarySort { public static void main(String[] args) { Integer[] array = new Integer[] { 1, 2, 10, 20, 100, 110 }; Arrays.sort(array, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub int quotient1 = o1 / 10; int reminder1 = o1 % 10; int quotient2 = o2 / 10; int reminder2 = o2 % 10; if (quotient1 != 0 && quotient2 != 0) { if (quotient1 == quotient2) return 0; int msd1 = 0; int msd2 = 0; if (quotient1 >= 10) msd1 = quotient1 / 10; else msd1 = quotient1; if (quotient2 >= 10) msd2 = quotient2 / 10; else msd2 = quotient2; if (msd1 == msd2) return quotient1 - quotient2; if (msd1 != msd2) return msd1 - msd2; } if (quotient1 == 0 && quotient2 == 0) return reminder1 - reminder2; if (quotient2 != 0 && quotient1 == 0) { while (quotient2 >= 10) quotient2 /= 10; return reminder1 - quotient2; } if (quotient1 != 0 && quotient2 == 0) { while (quotient1 >= 10) quotient1 /= 10; return quotient1 - reminder2; } return 1; } }); System.out.println(Arrays.toString(array)); } }
public int[] findTwo(int[] a, int[] b) { int sumA = 0, squareSumA = 0; for (int i = 0; i < a.length; i++) { sumA += a[i]; squareSumA += a[i] * a[i]; } int sumB = 0, squareSumB = 0; for (int i = 0; i < b.length; i++) { sumB += b[i]; squareSumB += b[i] * b[i]; } int twosum = sumB - sumA; int squareSum = squareSumB - squareSumA; int param1 = 2; int param2 = 2 * twosum; int param3 = twosum * twosum - squareSum; int[] res = new int[2]; int sqrtdiff = (int) Math.sqrt(param2 * param2 - 4 * param1 * param3); res[0] = (param2 - sqrtdiff) / (2 * param1); res[1] = (param2 + sqrtdiff) / (2 * param1); return res; }
Source: https://habr.com/ru/post/345832/
All Articles