-
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
816d947
commit 1add395
Showing
4 changed files
with
130 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,100 @@ | ||
//{ Driver Code Starts | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.LinkedList; | ||
import java.util.Scanner; | ||
import java.util.*; | ||
|
||
class Node { | ||
int data; | ||
Node next; | ||
|
||
Node(int key) { | ||
data = key; | ||
next = null; | ||
} | ||
} | ||
|
||
class count_pairs { | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
|
||
while (t-- > 0) { | ||
int n1 = sc.nextInt(); | ||
Integer arr1[] = new Integer[n1]; | ||
|
||
for (int i = 0; i < n1; i++) arr1[i] = sc.nextInt(); | ||
|
||
int n2 = sc.nextInt(); | ||
Integer arr2[] = new Integer[n2]; | ||
|
||
for (int i = 0; i < n2; i++) arr2[i] = sc.nextInt(); | ||
|
||
LinkedList<Integer> head1 = new LinkedList<>(Arrays.asList(arr1)); | ||
LinkedList<Integer> head2 = new LinkedList<>(Arrays.asList(arr2)); | ||
|
||
int x = sc.nextInt(); | ||
Solution gfg = new Solution(); | ||
System.out.println(gfg.countPairs(head1, head2, x)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
// your task is to complete this function | ||
|
||
/* | ||
class Node | ||
{ | ||
int data; | ||
Node next; | ||
Node(int key) | ||
{ | ||
data = key; | ||
next = null; | ||
} | ||
} | ||
*/ | ||
|
||
class Solution { | ||
|
||
public static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x) | ||
{ | ||
// add your code here | ||
int count = 0; | ||
|
||
Collections.sort(head1); | ||
Collections.sort(head2,Collections.reverseOrder()); | ||
|
||
Iterator<Integer> itr1 = head1.iterator(); | ||
Iterator<Integer> itr2 = head2.iterator(); | ||
|
||
Integer num1 = itr1.hasNext() ? itr1.next() : null; | ||
Integer num2 = itr2.hasNext() ? itr2.next() : null; | ||
|
||
while(num1 != null && num2 != null) | ||
{ | ||
if ((num1 + num2) == x) | ||
{ | ||
num1 = itr1.hasNext() ? itr1.next() : null; | ||
num2 = itr2.hasNext() ? itr2.next() : null; | ||
|
||
count++; | ||
} | ||
|
||
else if ((num1 + num2) > x) | ||
num2 = itr2.hasNext() ? itr2.next() : null; | ||
|
||
else | ||
num1 = itr1.hasNext() ? itr1.next() : null; | ||
|
||
} | ||
|
||
return count; | ||
} | ||
} |
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(n1 * n2) | ||
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(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,26 @@ | ||
class Solution | ||
{ | ||
public int[][] insert(int[][] intervals, int[] newInterval) | ||
{ | ||
int n = intervals.length; | ||
List<int[]> ans = new ArrayList<>(); | ||
int i = 0; | ||
|
||
while (i < n && intervals[i][1] < newInterval[0]) | ||
ans.add(intervals[i++]); | ||
|
||
while (i < n && intervals[i][0] <= newInterval[1]) | ||
{ | ||
newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
++i; | ||
} | ||
|
||
ans.add(newInterval); | ||
|
||
while (i < n) | ||
ans.add(intervals[i++]); | ||
|
||
return ans.toArray(int[][] ::new); | ||
} | ||
} |