-
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
5b404dc
commit fcd9a48
Showing
4 changed files
with
111 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,87 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
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()); | ||
|
||
int x; | ||
x = Integer.parseInt(br.readLine()); | ||
|
||
int y; | ||
y = Integer.parseInt(br.readLine()); | ||
|
||
int[] arr = IntArray.input(br, n); | ||
|
||
int[] brr = IntArray.input(br, n); | ||
|
||
Solution obj = new Solution(); | ||
long res = obj.maxTip(n, x, y, arr, brr); | ||
|
||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
public static long maxTip(int n, int x, int y, int[] arr, int[] brr) | ||
{ | ||
// code here | ||
int a[][] = new int[n][2]; | ||
for(int i=0; i<n; i++) | ||
{ | ||
a[i][0] = arr[i]; | ||
a[i][1] = brr[i]; | ||
} | ||
Arrays.sort(a, (o1,o2) -> Math.abs(o2[0] - o2[1]) - Math.abs(o1[0] - o1[1])); | ||
|
||
long ans=0; | ||
for(int i[] : a) | ||
{ | ||
if((i[0] > i[1] && x > 0) || (y == 0)) | ||
{ | ||
ans += i[0]; | ||
x--; | ||
} | ||
else | ||
{ | ||
ans += i[1]; | ||
y--; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(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,20 @@ | ||
class Solution { | ||
public int[] relativeSortArray(int[] arr1, int[] arr2) { | ||
int[] ans = new int[arr1.length]; | ||
int[] count = new int[1001]; | ||
int i = 0; | ||
|
||
for (int a : arr1) | ||
++count[a]; | ||
|
||
for (int a : arr2) | ||
while (count[a]-- > 0) | ||
ans[i++] = a; | ||
|
||
for (int num = 0; num < 1001; ++num) | ||
while (count[num]-- > 0) | ||
ans[i++] = num; | ||
|
||
return ans; | ||
} | ||
} |