Skip to content

Commit

Permalink
Added codes for 12 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 12, 2024
1 parent 71d0beb commit 8be25c7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
91 changes: 91 additions & 0 deletions GeeksForGeeks/March/12-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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;
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/12-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/March/12-3-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)
36 changes: 36 additions & 0 deletions LeetCode/March/12-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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<Integer, ListNode> 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;
}
}

0 comments on commit 8be25c7

Please sign in to comment.