-
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
2e8f603
commit 6953d24
Showing
4 changed files
with
94 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,53 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
|
||
public class GFG { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while (t-- > 0) { | ||
int n = sc.nextInt(); | ||
int arr[] = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = sc.nextInt(); | ||
} | ||
Solution obj = new Solution(); | ||
obj.xor1ToN(n, arr); | ||
obj.printArr(n, arr); | ||
obj.setToZero(n, arr); | ||
obj.printArr(n, arr); | ||
} | ||
sc.close(); | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
class Solution | ||
{ | ||
public void printArr(int n, int arr[]) | ||
{ | ||
// code here | ||
for(int a:arr) | ||
System.out.print(a+" "); | ||
|
||
System.out.println(); | ||
} | ||
|
||
public void setToZero(int n, int arr[]) | ||
{ | ||
// code here | ||
Arrays.fill(arr,0); | ||
} | ||
|
||
public void xor1ToN(int n, int arr[]) | ||
{ | ||
// code here | ||
for(int i=0;i<n;i++) | ||
{ | ||
arr[i]^=i; | ||
} | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(h) |
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,37 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public int sumOfLeftLeaves(TreeNode root) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
int ans = 0; | ||
|
||
if (root.left != null) | ||
{ | ||
if (root.left.left == null && root.left.right == null) | ||
ans += root.left.val; | ||
else | ||
ans += sumOfLeftLeaves(root.left); | ||
} | ||
|
||
ans += sumOfLeftLeaves(root.right); | ||
|
||
return ans; | ||
} | ||
} |