-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a30d802
commit ce6a835
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
import java.util.ArrayList; | ||
|
||
class IntArray { | ||
public static int[] input(BufferedReader br, int n) throws IOException { | ||
String[] s = br.readLine().trim().split(" "); | ||
int[] a = new int[n]; | ||
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); | ||
|
||
return a; | ||
} | ||
|
||
public static void print(int[] a) { | ||
for (int e : a) System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
|
||
public static void print(ArrayList<Integer> a) { | ||
for (int e : a) System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t; | ||
t = Integer.parseInt(br.readLine()); | ||
while (t-- > 0) { | ||
|
||
int n; | ||
n = Integer.parseInt(br.readLine()); | ||
|
||
Solution obj = new Solution(); | ||
ArrayList<Integer> res = obj.getPrimes(n); | ||
|
||
IntArray.print(res); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution | ||
{ | ||
public static ArrayList<Integer> getPrimes(int n) | ||
{ | ||
// code here | ||
ArrayList<Integer> list = new ArrayList<>(); | ||
list.add(-1); | ||
list.add(-1); | ||
for (int i=2; i<n; i++) | ||
{ | ||
if (isPrime(i) && isPrime(n-i)) | ||
{ | ||
list.set(0, i); | ||
list.set(1, n-i); | ||
break; | ||
} | ||
} | ||
|
||
return list; | ||
} | ||
|
||
public static boolean isPrime(int n) | ||
{ | ||
if (n==0 || n==1) | ||
return false; | ||
for(int i=2; i<=Math.sqrt(n); i++) | ||
{ | ||
if (n % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n * sqrt(n)) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution | ||
{ | ||
public int minPatches(int[] nums, int n) | ||
{ | ||
int ans = 0; | ||
int i = 0; | ||
long miss = 1; | ||
|
||
while (miss <= n) | ||
{ | ||
if (i < nums.length && nums[i] <= miss) | ||
miss += nums[i++]; | ||
else | ||
{ | ||
miss += miss; | ||
++ans; | ||
} | ||
} | ||
return ans; | ||
} | ||
} |