-
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
2200dd6
commit 642fa26
Showing
8 changed files
with
229 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,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; | ||
} | ||
} |
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 * logn) | ||
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,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; | ||
} | ||
} | ||
} |
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,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,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; | ||
} | ||
} |
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(sort) | ||
Space complexity - O(sort) |
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,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; | ||
} | ||
} |