Skip to content

Commit

Permalink
Added codes for 2 and 3 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 3, 2024
1 parent 4da5cd4 commit 061c686
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 0 deletions.
97 changes: 97 additions & 0 deletions GeeksForGeeks/June/02-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<ArrayList<Integer>> 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<Integer> 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<Integer> res = obj.constructList(q, queries);

IntArray.print(res);
}
}
}

// } Driver Code Ends



class Solution {
public static ArrayList<Integer> constructList(int q, int[][] queries) {
// code here
ArrayList<Integer> result = new ArrayList<Integer>();
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/02-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(q*log(q))
Space complexity - O(l)
41 changes: 41 additions & 0 deletions GeeksForGeeks/June/03-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/03-6-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)
3 changes: 3 additions & 0 deletions LeetCode/June/02-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Time complexity - O(|s|)
Space complexity - O(1)

21 changes: 21 additions & 0 deletions LeetCode/June/02-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions LeetCode/June/03-6-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)
14 changes: 14 additions & 0 deletions LeetCode/June/03-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 061c686

Please sign in to comment.