-
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
ac5e2ed
commit 1cb3fde
Showing
4 changed files
with
102 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,62 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
//Initial Template for Java | ||
|
||
//Initial Template for Java | ||
|
||
/*package whatever //do not write package name here */ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class Array { | ||
|
||
// Driver code | ||
public static void main (String[] args) throws IOException{ | ||
// Taking input using buffered reader | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int testcases = Integer.parseInt(br.readLine()); | ||
|
||
// looping through all testcases | ||
while(testcases-- > 0){ | ||
int sizeOfArray = Integer.parseInt(br.readLine()); | ||
int arr [] = new int[sizeOfArray]; | ||
|
||
String line = br.readLine(); | ||
String[] elements = line.trim().split("\\s+"); | ||
for(int i = 0;i<sizeOfArray;i++){ | ||
arr[i] = Integer.parseInt(elements[i]); | ||
} | ||
|
||
Solution obj = new Solution(); | ||
arr = obj.game_with_number(arr, sizeOfArray); | ||
for(int i=0; i< sizeOfArray;i++){ | ||
System.out.print(arr[i] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
// Function for finding maximum and value pair | ||
public static int[] game_with_number (int arr[], int n) | ||
{ | ||
// Complete the function | ||
for (int i=0; i<n-1; i++) | ||
arr[i] |= arr[i+1]; | ||
|
||
return arr; | ||
} | ||
} |
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,36 @@ | ||
/** | ||
* 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 diameterOfBinaryTree(TreeNode root) | ||
{ | ||
maxDepth(root); | ||
return ans; | ||
} | ||
|
||
private int ans = 0; | ||
|
||
private int maxDepth(TreeNode root) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
int l = maxDepth(root.left); | ||
int r = maxDepth(root.right); | ||
ans = Math.max(ans, l + r); | ||
return 1 + Math.max(l, r); | ||
} | ||
} |