From 061c686eca01e89c57d48ef6842ba644d9b78041 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Mon, 3 Jun 2024 10:46:15 +0530 Subject: [PATCH] Added codes for 2 and 3 June --- GeeksForGeeks/June/02-6-24/GFG.java | 97 ++++++++++++++++++++++++++++ GeeksForGeeks/June/02-6-24/README.md | 2 + GeeksForGeeks/June/03-6-24/GFG.java | 41 ++++++++++++ GeeksForGeeks/June/03-6-24/README.md | 2 + LeetCode/June/02-6-24/README.md | 3 + LeetCode/June/02-6-24/Solution.java | 21 ++++++ LeetCode/June/03-6-24/README.md | 2 + LeetCode/June/03-6-24/Solution.java | 14 ++++ 8 files changed, 182 insertions(+) create mode 100644 GeeksForGeeks/June/02-6-24/GFG.java create mode 100644 GeeksForGeeks/June/02-6-24/README.md create mode 100644 GeeksForGeeks/June/03-6-24/GFG.java create mode 100644 GeeksForGeeks/June/03-6-24/README.md create mode 100644 LeetCode/June/02-6-24/README.md create mode 100644 LeetCode/June/02-6-24/Solution.java create mode 100644 LeetCode/June/03-6-24/README.md create mode 100644 LeetCode/June/03-6-24/Solution.java diff --git a/GeeksForGeeks/June/02-6-24/GFG.java b/GeeksForGeeks/June/02-6-24/GFG.java new file mode 100644 index 0000000..8656e89 --- /dev/null +++ b/GeeksForGeeks/June/02-6-24/GFG.java @@ -0,0 +1,97 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +class IntMatrix { + public static int[][] input(BufferedReader br, int n, int m) throws IOException { + int[][] mat = new int[n][]; + + for (int i = 0; i < n; i++) { + String[] s = br.readLine().trim().split(" "); + mat[i] = new int[s.length]; + for (int j = 0; j < s.length; j++) mat[i][j] = Integer.parseInt(s[j]); + } + + return mat; + } + + public static void print(int[][] m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } + + public static void print(ArrayList> m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } +} + +class IntArray { + public static int[] input(BufferedReader br, int n) throws IOException { + String[] s = br.readLine().trim().split(" "); + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); + + return a; + } + + public static void print(int[] a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + + public static void print(ArrayList a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } +} + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t; + t = Integer.parseInt(br.readLine()); + while (t-- > 0) { + + int q; + q = Integer.parseInt(br.readLine()); + + int[][] queries = IntMatrix.input(br, q, 2); + + Solution obj = new Solution(); + ArrayList res = obj.constructList(q, queries); + + IntArray.print(res); + } + } +} + +// } Driver Code Ends + + + +class Solution { + public static ArrayList constructList(int q, int[][] queries) { + // code here + ArrayList result = new ArrayList(); + int xor=0; + for (int i=q-1;i>=0;i--) + { + if (queries[i][0]==0) + result.add(queries[i][1]^xor); + else + xor^=queries[i][1]; + } + + result.add(xor); + + Collections.reverse(result); + Collections.sort(result); + + return result; + } +} diff --git a/GeeksForGeeks/June/02-6-24/README.md b/GeeksForGeeks/June/02-6-24/README.md new file mode 100644 index 0000000..a872c89 --- /dev/null +++ b/GeeksForGeeks/June/02-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(q*log(q)) +Space complexity - O(l) diff --git a/GeeksForGeeks/June/03-6-24/GFG.java b/GeeksForGeeks/June/03-6-24/GFG.java new file mode 100644 index 0000000..c554b05 --- /dev/null +++ b/GeeksForGeeks/June/03-6-24/GFG.java @@ -0,0 +1,41 @@ +//{ 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) { + int N = Integer.parseInt(read.readLine()); + + Solution ob = new Solution(); + System.out.println(ob.numberOfConsecutiveOnes(N)); + } + } +} +// } Driver Code Ends + + +// User function Template for Java +class Solution { + static int numberOfConsecutiveOnes(int n) { + // code here + long mod = (int) 1e9 + 7, res = 1, a = 1, b = 1; + + if(n == 1 || n == 2) + return 1; + + for(int i = 3; i <= n; i++) + { + long c = (a + b) % mod; + a = b; + b = c; + + res = ((res * 1l * 2) + a) % mod; + } + + return (int) res; + } +} diff --git a/GeeksForGeeks/June/03-6-24/README.md b/GeeksForGeeks/June/03-6-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/June/03-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/June/02-6-24/README.md b/LeetCode/June/02-6-24/README.md new file mode 100644 index 0000000..8baff70 --- /dev/null +++ b/LeetCode/June/02-6-24/README.md @@ -0,0 +1,3 @@ +Time complexity - O(|s|) +Space complexity - O(1) + diff --git a/LeetCode/June/02-6-24/Solution.java b/LeetCode/June/02-6-24/Solution.java new file mode 100644 index 0000000..2fdec6e --- /dev/null +++ b/LeetCode/June/02-6-24/Solution.java @@ -0,0 +1,21 @@ +class Solution +{ + public void reverseString(char[] s) + { + int i = 0; + int j = s.length - 1; + while (i < j) + { + swap(s, i, j); + i++; + j--; + } + } + + public void swap(char[] arr, int first, int last) + { + char temp = arr[first]; + arr[first] = arr[last]; + arr[last] = temp; + } +} diff --git a/LeetCode/June/03-6-24/README.md b/LeetCode/June/03-6-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/June/03-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/June/03-6-24/Solution.java b/LeetCode/June/03-6-24/Solution.java new file mode 100644 index 0000000..31bf398 --- /dev/null +++ b/LeetCode/June/03-6-24/Solution.java @@ -0,0 +1,14 @@ +class Solution +{ + public int appendCharacters(String s, String t) + { + int i = 0; + + for (char c : s.toCharArray()) + if (c == t.charAt(i)) + if (++i == t.length()) + return 0; + + return t.length() - i; + } +}