-
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
4da5cd4
commit 061c686
Showing
8 changed files
with
182 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,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; | ||
} | ||
} |
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(q*log(q)) | ||
Space complexity - O(l) |
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,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; | ||
} | ||
} |
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,3 @@ | ||
Time complexity - O(|s|) | ||
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |