-
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
6953d24
commit 88962d0
Showing
4 changed files
with
114 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,70 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class Array { | ||
|
||
// Driver code | ||
public static void main (String[] args) throws IOException{ | ||
// Taking input using buffered reader | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int testcases = Integer.parseInt(br.readLine()); | ||
// looping through all testcases | ||
while(testcases-- > 0){ | ||
int n=Integer.parseInt(br.readLine()); | ||
String line1 = br.readLine(); | ||
String line2 = br.readLine(); | ||
String[] a1 = line1.trim().split("\\s+"); | ||
String[] b1 = line2.trim().split("\\s+"); | ||
int a[]=new int[n]; | ||
int b[]=new int[n]; | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
a[i]=Integer.parseInt(a1[i]); | ||
b[i]=Integer.parseInt(b1[i]); | ||
} | ||
int q = Integer.parseInt(br.readLine()); | ||
int query[]=new int[q]; | ||
for(int i=0;i<q;i++) | ||
{ | ||
query[i]=Integer.parseInt(br.readLine()); | ||
} | ||
Solution ob=new Solution(); | ||
int ans[]=ob.countElements(a,b,n,query,q); | ||
for(int i=0;i<q;i++) | ||
System.out.println(ans[i]); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
|
||
class Solution | ||
{ | ||
public static int[] countElements(int a[], int b[], int n, int query[], int q) | ||
{ | ||
int[] res=new int[query.length]; | ||
|
||
for(int i=0;i<query.length;i++) | ||
{ | ||
int count=0; | ||
int k = a[query[i]]; | ||
for(int j=0;j<n;j++) | ||
{ | ||
if(b[j]<=k) count++; | ||
} | ||
res[i]=count; | ||
} | ||
return res; | ||
} | ||
} |
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+q) | ||
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(h) |
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,40 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
private int ans = 0; | ||
|
||
public int sumNumbers(TreeNode root) | ||
{ | ||
dfs(root, 0); | ||
return ans; | ||
} | ||
|
||
private void dfs(TreeNode root, int path) | ||
{ | ||
if (root==null) | ||
return; | ||
|
||
if (root.left == null && root.right == null) | ||
{ | ||
ans += path * 10 + root.val; | ||
return; | ||
} | ||
|
||
dfs(root.left, path * 10 + root.val); | ||
dfs(root.right, path * 10 + root.val); | ||
} | ||
} |