From a346a16ceb7c94a818f14e1cd71e92af95076b89 Mon Sep 17 00:00:00 2001 From: Tanishq Malhotra <33938577+tanishq98@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:48:55 +0530 Subject: [PATCH 1/3] Added most optimised Code to find N prime number I have added the most optimized code to find N prime Number by taking care of both Time and Space Complexities. Time Complexity of this code is almost half of the other codes of prime number found online. This code can be used in interviews and competitive programming for better performance of your Solution. --- Java/MostOptimisedPrimeNumberCode.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Java/MostOptimisedPrimeNumberCode.java diff --git a/Java/MostOptimisedPrimeNumberCode.java b/Java/MostOptimisedPrimeNumberCode.java new file mode 100644 index 0000000..e8e184f --- /dev/null +++ b/Java/MostOptimisedPrimeNumberCode.java @@ -0,0 +1,24 @@ +import java.util.*; + +public class MostOptimisedPrimeNumberCode{ + +public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + int low = scn.nextInt(); + int high = scn.nextInt(); + + for(int num = low; num <= high; num++){ + int div = 2; + while(div * div <= num){ + if(num % div == 0){ + break; + } + div++; + } + + if(div * div > num){ + System.out.println(num); + } + } + } +} \ No newline at end of file From f914860e92c9148f04ca2a9d09e2b942d1b03143 Mon Sep 17 00:00:00 2001 From: Tanishq Malhotra <33938577+tanishq98@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:59:04 +0530 Subject: [PATCH 2/3] Sum of 2 Arrays- Best Solution for SDE Interviews and CP You are given a number n1, representing the size of array a1. You are given n1 numbers, representing elements of array a1. You are given a number n2, representing the size of array a2. You are given n2 numbers, representing elements of array a2. The two arrays represent digits of two numbers. You are required to add the numbers represented by two arrays and print the arrays Solution: I have attached the optimized and advanced code for this problem. Pro-tip: If both the arrays have same number of elements then it is very easy to sum those arrays. But, what if number of elements are different ?? Refer this solution to hack this problem. --- Java/SumoftwoArrays.java | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Java/SumoftwoArrays.java diff --git a/Java/SumoftwoArrays.java b/Java/SumoftwoArrays.java new file mode 100644 index 0000000..bb641b1 --- /dev/null +++ b/Java/SumoftwoArrays.java @@ -0,0 +1,52 @@ +import java.io.*; +import java.util.*; + +public class SumoftwoArrays{ + +public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int n1 = Integer.parseInt(br.readLine()); + int[] a1 = new int[n1]; + for(int i = 0; i < n1; i++){ + a1[i] = Integer.parseInt(br.readLine()); + } + + int n2 = Integer.parseInt(br.readLine()); + int[] a2 = new int[n2]; + for(int i = 0; i < n2; i++){ + a2[i] = Integer.parseInt(br.readLine()); + } + + int[] sum = new int[n1 > n2? n1: n2]; + int i = n1 - 1; + int j = n2 - 1; + int k = sum.length - 1; + int c = 0; + while(i >= 0 || j >= 0){ + int d = c; + + if(i >= 0) + d += a1[i]; + + if(j >= 0) + d += a2[j]; + + c = d / 10; + d = d % 10; + sum[k] = d; + + i--; + j--; + k--; + } + + if(c > 0){ + System.out.println(c); + } + for(int val: sum){ + System.out.println(val); + } + } + +} \ No newline at end of file From e653c0cd012ad56500e9b52428a7c9f4bf765cb7 Mon Sep 17 00:00:00 2001 From: Tanishq Malhotra <33938577+tanishq98@users.noreply.github.com> Date: Thu, 1 Oct 2020 03:07:39 +0530 Subject: [PATCH 3/3] Decimal to Any Base- Most Asked interview Ques for SDE's We all know converting a decimal or binary to other type is a very small and less pained task. But, Irony most of the Engineers and aspiring Developers don't know a single conversion on this. This is best, optimized and easy way to convert base from to any other (Octa, binary, etc.) in Java. Do comment if you develop a more optimized way than this. Because inheriting knowledge from one another make you a Successful person in your life. --- Java/DecimaltoAnyBase.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Java/DecimaltoAnyBase.java diff --git a/Java/DecimaltoAnyBase.java b/Java/DecimaltoAnyBase.java new file mode 100644 index 0000000..99cabb1 --- /dev/null +++ b/Java/DecimaltoAnyBase.java @@ -0,0 +1,26 @@ +import java.util.*; + + public class DecimaltoAnyBase{ + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + int n = scn.nextInt(); + int b = scn.nextInt(); + int dn = getValueInBase(n, b); + System.out.println(dn); + } + + public static int getValueInBase(int n, int b){ + int rv = 0; + + int p = 1; + while(n > 0){ + int d = n % b; + n = n / b; + rv += p * d; + p = p * 10; + } + + return rv; + } + } \ No newline at end of file