Skip to content

Commit

Permalink
Added codes for 3 and 4 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 4, 2024
1 parent 2200dd6 commit 642fa26
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 0 deletions.
109 changes: 109 additions & 0 deletions GeeksForGeeks/March/03-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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; x<n; x++)
{
str=str+arr[x];
}

return str;
}

static String[] merge(String arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;

String L[] = new String[n1];
String R[] = new String[n2];

for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];

for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

int i = 0, j = 0;
int k = l;

while (i < n1 && j < n2)
{
if (((L[i]+R[j]).compareTo(R[j]+L[i]))>0)
{
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/03-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(n)
48 changes: 48 additions & 0 deletions GeeksForGeeks/March/04-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<n-2; i++)
{
int temp = arr[i];
arr[i] = arr[i+2];
arr[i+2] = temp;
}
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/04-3-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)
2 changes: 2 additions & 0 deletions LeetCode/March/03-3-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)
34 changes: 34 additions & 0 deletions LeetCode/March/03-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution
{
public ListNode removeNthFromEnd(ListNode head, int n)
{
ListNode slow = head;
ListNode fast = head;

while (n-- > 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;
}
}
2 changes: 2 additions & 0 deletions LeetCode/March/04-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(sort)
Space complexity - O(sort)
30 changes: 30 additions & 0 deletions LeetCode/March/04-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 642fa26

Please sign in to comment.