-
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
92bd7de
commit 8f76ae0
Showing
8 changed files
with
203 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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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(1) |
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,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; | ||
} | ||
} | ||
} |
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(1) |
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,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; | ||
} | ||
} |