From 642fa26cded36e8835832e15ee614f3bfe90c383 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Mon, 4 Mar 2024 11:37:59 +0530 Subject: [PATCH] Added codes for 3 and 4 March --- GeeksForGeeks/March/03-3-24/GFG.java | 109 ++++++++++++++++++++++++++ GeeksForGeeks/March/03-3-24/README.md | 2 + GeeksForGeeks/March/04-3-24/GFG.java | 48 ++++++++++++ GeeksForGeeks/March/04-3-24/README.md | 2 + LeetCode/March/03-3-24/README.md | 2 + LeetCode/March/03-3-24/Solution.java | 34 ++++++++ LeetCode/March/04-3-24/README.md | 2 + LeetCode/March/04-3-24/Solution.java | 30 +++++++ 8 files changed, 229 insertions(+) create mode 100644 GeeksForGeeks/March/03-3-24/GFG.java create mode 100644 GeeksForGeeks/March/03-3-24/README.md create mode 100644 GeeksForGeeks/March/04-3-24/GFG.java create mode 100644 GeeksForGeeks/March/04-3-24/README.md create mode 100644 LeetCode/March/03-3-24/README.md create mode 100644 LeetCode/March/03-3-24/Solution.java create mode 100644 LeetCode/March/04-3-24/README.md create mode 100644 LeetCode/March/04-3-24/Solution.java diff --git a/GeeksForGeeks/March/03-3-24/GFG.java b/GeeksForGeeks/March/03-3-24/GFG.java new file mode 100644 index 0000000..9194ad8 --- /dev/null +++ b/GeeksForGeeks/March/03-3-24/GFG.java @@ -0,0 +1,109 @@ +//{ Driver Code Starts +//Initial Template for Java + + + +import java.util.*; +import java.io.*; + +public class Main { + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int tc = Integer.parseInt(br.readLine().trim()); + while (tc-- > 0) { + String[] inputLine; + int n = Integer.parseInt(br.readLine().trim()); + String[] arr = br.readLine().trim().split(" "); + + String ans = new Solution().printLargest(n, arr); + System.out.println(ans); + } + } +} +// } Driver Code Ends + + +//User function Template for Java + + +class Solution +{ + String printLargest(int n, String[] arr) + { + String str=""; + + sort(arr, 0, n-1); + + for(int x=0; x0) + { + arr[k] = L[i]; + i++; + } + + else + { + arr[k] = R[j]; + j++; + } + + k++; + } + + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } + return arr; + } + + static String[] sort(String arr[], int l, int r) + { + if (l < r) + { + int m = l + (r - l) / 2; + + sort(arr, l, m); + sort(arr, m + 1, r); + + merge(arr, l, m, r); + } + return arr; + } +} diff --git a/GeeksForGeeks/March/03-3-24/README.md b/GeeksForGeeks/March/03-3-24/README.md new file mode 100644 index 0000000..fbf4193 --- /dev/null +++ b/GeeksForGeeks/March/03-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * logn) +Space complexity - O(n) diff --git a/GeeksForGeeks/March/04-3-24/GFG.java b/GeeksForGeeks/March/04-3-24/GFG.java new file mode 100644 index 0000000..a7a8589 --- /dev/null +++ b/GeeksForGeeks/March/04-3-24/GFG.java @@ -0,0 +1,48 @@ +//{ Driver Code Starts +//Initial Template for Java + +import java.util.*; +import java.lang.*; +import java.io.*; +class GFG +{ + public static void main(String[] args) throws IOException + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine().trim()); + while(T-->0) + { + int n = Integer.parseInt(br.readLine().trim()); + String[] S = br.readLine().trim().split(" "); + int[] arr = new int[n]; + for(int i = 0; i < n; i++){ + arr[i] = Integer.parseInt(S[i]); + } + Solution obj = new Solution(); + obj.swapElements(arr, n); + for(int i = 0; i < n; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + } + } +} + +// } Driver Code Ends + + +//User function Template for Java + +class Solution +{ + public void swapElements(int[] arr, int n) + { + // Code here + for (int i=0; i 0) + fast = fast.next; + + if (fast == null) + return head.next; + + while (fast.next != null) + { + slow = slow.next; + fast = fast.next; + } + + slow.next = slow.next.next; + + return head; + } +} diff --git a/LeetCode/March/04-3-24/README.md b/LeetCode/March/04-3-24/README.md new file mode 100644 index 0000000..2bf8492 --- /dev/null +++ b/LeetCode/March/04-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(sort) +Space complexity - O(sort) diff --git a/LeetCode/March/04-3-24/Solution.java b/LeetCode/March/04-3-24/Solution.java new file mode 100644 index 0000000..abf1594 --- /dev/null +++ b/LeetCode/March/04-3-24/Solution.java @@ -0,0 +1,30 @@ +class Solution +{ + public int bagOfTokensScore(int[] tokens, int power) + { + int ans = 0; + int score = 0; + int i = 0; + int j = tokens.length - 1; + + Arrays.sort(tokens); + + while (i <= j && (power >= tokens[i] || score > 0)) + { + while (i <= j && power >= tokens[i]) + { + power -= tokens[i++]; + ++score; + } + + ans = Math.max(ans, score); + if (i <= j && score > 0) + { + power += tokens[j--]; + --score; + } + } + + return ans; + } +}