Skip to content

Commit

Permalink
Added codes for 28 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 28, 2024
1 parent 49aaa38 commit eeec2c3
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
85 changes: 85 additions & 0 deletions GeeksForGeeks/April/28-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//{ Driver Code Starts
import java.util.*;
import java.io.*;

class Node{
int data;
Node next;

Node(int x){
data = x;
next = null;
}

}
class GFG{
static void printList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
int n = sc.nextInt();
Node head = new Node(sc.nextInt());
Node tail = head;
for(int i=0; i<n-1; i++)
{
tail.next = new Node(sc.nextInt());
tail = tail.next;
}
Solution g = new Solution();
head = g.deleteMid(head);
printList(head);
t--;
}
}
}


// } Driver Code Ends


/*
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
*/

class Solution
{
Node deleteMid(Node head)
{
// This is method only submission.
// You only need to complete the method.
if (head == null)
return null;
if (head.next == null)
return null;

Node slow = head;
Node fast = head;

Node prev = null;

while (fast != null && fast.next != null)
{
fast = fast.next.next;
prev = slow;
slow = slow.next;
}

// Delete the middle node
prev.next = slow.next;

return head;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/28-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(1)
2 changes: 2 additions & 0 deletions LeetCode/April/28-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(n)
51 changes: 51 additions & 0 deletions LeetCode/April/28-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution
{
public int[] sumOfDistancesInTree(int n, int[][] edges)
{
int[] ans = new int[n];
int[] count = new int[n];
Set<Integer>[] tree = new Set[n];

Arrays.fill(count, 1);

for (int i = 0; i < n; ++i)
tree[i] = new HashSet<>();

for (int[] edge : edges)
{
int u = edge[0];
int v = edge[1];
tree[u].add(v);
tree[v].add(u);
}

postorder(tree, 0, -1, count, ans);
preorder(tree, 0, -1, count, ans);
return ans;
}

private void postorder(Set<Integer>[] tree, int node, int parent, int[] count, int[] ans)
{
for (int child : tree[node])
{
if (child == parent)
continue;

postorder(tree, child, node, count, ans);
count[node] += count[child];
ans[node] += ans[child] + count[child];
}
}

private void preorder(Set<Integer>[] tree, int node, int parent, int[] count, int[] ans)
{
for (int child : tree[node])
{
if (child == parent)
continue;

ans[child] = ans[node] - count[child] + (tree.length - count[child]);
preorder(tree, child, node, count, ans);
}
}
}

0 comments on commit eeec2c3

Please sign in to comment.