Skip to content

Commit

Permalink
Added files for 15 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 15, 2024
1 parent 6953d24 commit 88962d0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
70 changes: 70 additions & 0 deletions GeeksForGeeks/April/15-4-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/15-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n+q)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/April/15-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
40 changes: 40 additions & 0 deletions LeetCode/April/15-4-24/Solution.java
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);
}
}

0 comments on commit 88962d0

Please sign in to comment.