-
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
293a8ba
commit af6ef3c
Showing
4 changed files
with
172 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,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 |
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,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,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; | ||
} | ||
} |