Skip to content

Commit

Permalink
Added for 10 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 10, 2024
1 parent 293a8ba commit af6ef3c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
91 changes: 91 additions & 0 deletions GeeksForGeeks/10-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//{ Driver Code Starts
//Initial Template for Java

//Initial Template for Java

/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;


// } Driver Code Ends
//User function Template for Java

class Solution{
int longSubarrWthSumDivByK(int a[], int n, int k)
{
// Complete the function
Map<Integer, Integer> map = new HashMap<>();

int max_len = 0;
int sum = 0;

for (int i = 0; i < n; i++)
{
sum += a[i];

// to handle negative values as well
int mod = ((sum % k) + k) % k;

if (mod == 0)
max_len = i + 1;

if (!map.containsKey(mod))
map.put(mod, i);
else
{
int sz = i - map.get(mod);
max_len = Math.max(max_len, sz);
}
}

return max_len;
}

}


//{ Driver Code Starts.

// Driver class
class GFG {

// 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){

String line1 = br.readLine();
String[] element = line1.trim().split("\\s+");
int sizeOfArray = Integer.parseInt(element[0]);
int K = Integer.parseInt(element[1]);

int arr [] = new int[sizeOfArray];

String line = br.readLine();
String[] elements = line.trim().split("\\s+");

for(int i = 0;i<sizeOfArray;i++){
arr[i] = Integer.parseInt(elements[i]);
}

Solution obj = new Solution();

int res = obj.longSubarrWthSumDivByK(arr, sizeOfArray, K);

System.out.println(res);


}
}
}



// } Driver Code Ends
2 changes: 2 additions & 0 deletions GeeksForGeeks/10-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/10-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
77 changes: 77 additions & 0 deletions LeetCode/10-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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
{
public int amountOfTime(TreeNode root, int start)
{
int ans = -1;
Map<Integer, List<Integer>> graph = getGraph(root);
Queue<Integer> q = new ArrayDeque<>(Arrays.asList(start));
Set<Integer> seen = new HashSet<>(Arrays.asList(start));

for (; !q.isEmpty(); ++ans)
{
for (int sz = q.size(); sz > 0; --sz)
{
final int u = q.poll();
if (!graph.containsKey(u))
continue;

for (final int v : graph.get(u))
{
if (seen.contains(v))
continue;

q.offer(v);
seen.add(v);
}
}
}

return ans;
}

private Map<Integer, List<Integer>> getGraph(TreeNode root)
{
Map<Integer, List<Integer>> graph = new HashMap<>();
// (node, parent)
Queue<Pair<TreeNode, Integer>> q =
new ArrayDeque<>(Arrays.asList(new Pair<>(root, -1)));

while (!q.isEmpty())
{
Pair<TreeNode, Integer> pair = q.poll();
TreeNode node = pair.getKey();
final int parent = pair.getValue();

if (parent != -1)
{
graph.putIfAbsent(parent, new ArrayList<>());
graph.putIfAbsent(node.val, new ArrayList<>());
graph.get(parent).add(node.val);
graph.get(node.val).add(parent);
}

if (node.left != null)
q.add(new Pair<>(node.left, node.val));

if (node.right != null)
q.add(new Pair<>(node.right, node.val));
}

return graph;
}
}

0 comments on commit af6ef3c

Please sign in to comment.