Skip to content

Commit

Permalink
Added codes for 23 and 24 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 24, 2024
1 parent 92bd7de commit 8f76ae0
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 0 deletions.
42 changes: 42 additions & 0 deletions GeeksForGeeks/March/23-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class Main {
// Driver code
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
Solution obj = new Solution();
int ans[] = obj.Series(n);
for (int i : ans) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
// } Driver Code Ends


class Solution {

int[] Series(int n)
{
// code here
if(n==1)
return new int[]{0,1};

int[] ans=new int[n+1];
ans[0]=0;
ans[1]=1;

for(int i=2;i<=n;i++)
ans[i] = (ans[i-1]+ans[i-2]) % ((int)Math.pow(10,9)+7);

return ans;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/23-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)
63 changes: 63 additions & 0 deletions GeeksForGeeks/March/24-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;

class GFG {
static File file;
static BufferedReader in;
static PrintWriter out;

public static void main(String args[]) throws IOException {
in=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());

while (t-- > 0) {

String s[] = in.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
s = in.readLine().trim().split(" ");
Stack<Integer> st = new Stack<>();
for (int i = 0; i < n; i++) {
st.push(Integer.parseInt(s[i]));
}
Solution ob = new Solution();
Stack<Integer> S = ob.insertAtBottom(st, x);
for (int i : S) {
out.print(i + " ");
}
out.println();
}
out.close();
}
}

// } Driver Code Ends


//User function Template for Java
class Solution
{
public Stack<Integer> insertAtBottom(Stack<Integer> st, int x)
{
Stack<Integer> temp = new Stack<>();

while (!st.empty())
{
temp.push(st.peek());
st.pop();
}

st.push(x);

while (!temp.empty())
{
st.push(temp.peek());
temp.pop();
}

return st;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/24-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)
2 changes: 2 additions & 0 deletions LeetCode/March/23-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(1)
66 changes: 66 additions & 0 deletions LeetCode/March/23-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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 void reorderList(ListNode head)
{
if (head == null || head.next == null)
return;

ListNode mid = findMid(head);
ListNode reversed = reverse(mid);
merge(head, reversed);
}

private ListNode findMid(ListNode head)
{
ListNode prev = null;
ListNode slow = head;
ListNode fast = head;

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

return slow;
}

private ListNode reverse(ListNode head)
{
ListNode prev = null;
ListNode curr = head;

while (curr != null)
{
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}

return prev;
}

private void merge(ListNode l1, ListNode l2)
{
while (l2 != null)
{
ListNode next = l1.next;
l1.next = l2;
l1 = l2;
l2 = next;
}
}
}
2 changes: 2 additions & 0 deletions LeetCode/March/24-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(1)
24 changes: 24 additions & 0 deletions LeetCode/March/24-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution
{
public int findDuplicate(int[] nums)
{
int slow = nums[nums[0]];
int fast = nums[nums[nums[0]]];

while (slow != fast)
{
slow = nums[slow];
fast = nums[nums[fast]];
}

slow = nums[0];

while (slow != fast)
{
slow = nums[slow];
fast = nums[fast];
}

return slow;
}
}

0 comments on commit 8f76ae0

Please sign in to comment.