Skip to content

Commit

Permalink
Added codes for 16 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 16, 2024
1 parent a30d802 commit ce6a835
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
79 changes: 79 additions & 0 deletions GeeksForGeeks/June/16-6-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/16-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * sqrt(n))
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/June/16-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
21 changes: 21 additions & 0 deletions LeetCode/June/16-6-24/Solution.java
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;
}
}

0 comments on commit ce6a835

Please sign in to comment.