From 8be25c7fc9ca69d4c07090c92314cf31c80d469c Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 12 Mar 2024 19:03:04 +0530 Subject: [PATCH] Added codes for 12 March --- GeeksForGeeks/March/12-3-24/GFG.java | 91 +++++++++++++++++++++++++++ GeeksForGeeks/March/12-3-24/README.md | 2 + LeetCode/March/12-3-24/README.md | 2 + LeetCode/March/12-3-24/Solution.java | 36 +++++++++++ 4 files changed, 131 insertions(+) create mode 100644 GeeksForGeeks/March/12-3-24/GFG.java create mode 100644 GeeksForGeeks/March/12-3-24/README.md create mode 100644 LeetCode/March/12-3-24/README.md create mode 100644 LeetCode/March/12-3-24/Solution.java diff --git a/GeeksForGeeks/March/12-3-24/GFG.java b/GeeksForGeeks/March/12-3-24/GFG.java new file mode 100644 index 0000000..c67e958 --- /dev/null +++ b/GeeksForGeeks/March/12-3-24/GFG.java @@ -0,0 +1,91 @@ +//{ Driver Code Starts +//Initial Template for Java + +import java.io.*; +import java.util.*; + +class GFG { + public static void main(String args[]) throws IOException { + BufferedReader read = + new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(read.readLine()); + while (t-- > 0) { + String S[] = read.readLine().split(" "); + + long a = Long.parseLong(S[0]); + long b = Long.parseLong(S[1]); + long c = Long.parseLong(S[2]); + long n = Long.parseLong(S[3]); + long m = Long.parseLong(S[4]); + + Solution ob = new Solution(); + System.out.println(ob.genFibNum(a,b,c,n,m)); + } + } +} +// } Driver Code Ends + + +//User function Template for Java + +class Solution { + static long genFibNum(Long a, Long b, Long c, long n, long m) + { + // code here + if (n <= 2) + { + return 1 % m; + } + + long[][] mat = + { + {a, b, 1}, + {1, 0, 0}, + {0, 0, 1} + }; + + long[][] res = + { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }; + + n -= 2; + while (n > 0) + { + if ((n & 1) == 1) + { + res = multiply(res, mat, m); + } + + mat = multiply(mat, mat, m); + n >>= 1; + } + + return (res[0][0] + res[0][1] + c * res[0][2]) % m; + } + + + static long[][] multiply(long[][] A, long[][] B, long m) + { + int size = A.length; + long[][] result = new long[size][size]; + for (int i = 0; i < size; ++i) + { + for (int j = 0; j < size; ++j) + { + long sum = 0; + for (int k = 0; k < size; ++k) + { + sum += A[i][k] * B[k][j]; + sum %= m; + } + + result[i][j] = sum; + } + } + + return result; + } +}; diff --git a/GeeksForGeeks/March/12-3-24/README.md b/GeeksForGeeks/March/12-3-24/README.md new file mode 100644 index 0000000..ba09c45 --- /dev/null +++ b/GeeksForGeeks/March/12-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(logn) +Space complexity - O(1) diff --git a/LeetCode/March/12-3-24/README.md b/LeetCode/March/12-3-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/March/12-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/March/12-3-24/Solution.java b/LeetCode/March/12-3-24/Solution.java new file mode 100644 index 0000000..f3233e5 --- /dev/null +++ b/LeetCode/March/12-3-24/Solution.java @@ -0,0 +1,36 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution +{ + public ListNode removeZeroSumSublists(ListNode head) + { + ListNode dummy = new ListNode(0, head); + int prefix = 0; + Map prefixToNode = new HashMap<>(); + prefixToNode.put(0, dummy); + + for (; head != null; head = head.next) + { + prefix += head.val; + prefixToNode.put(prefix, head); + } + + prefix = 0; + + for (head = dummy; head != null; head = head.next) + { + prefix += head.val; + head.next = prefixToNode.get(prefix).next; + } + + return dummy.next; + } +}