-
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
19750d7
commit 39050f1
Showing
4 changed files
with
104 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,52 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
|
||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int tc = Integer.parseInt(br.readLine()); | ||
while (tc-- > 0) { | ||
int n = Integer.parseInt(br.readLine()); | ||
int[] arr = new int[n]; | ||
String[] inputLine = br.readLine().split(" "); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = Integer.parseInt(inputLine[i]); | ||
} | ||
|
||
System.out.println(new Solution().sumBitDifferences(arr, n)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
long sumBitDifferences(int[] arr, int n) | ||
{ | ||
// code here | ||
long ans = 0; | ||
for (int i = 0; i < 32; i++) | ||
{ | ||
long count = 0; | ||
|
||
for (int j = 0; j < n; j++) | ||
if ((arr[j] & (1 << i)) != 0) | ||
count++; | ||
|
||
ans += (count * (n - count) * 2); | ||
} | ||
return ans; | ||
} | ||
} |
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(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,48 @@ | ||
/** | ||
* 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 boolean isEvenOddTree(TreeNode root) | ||
{ | ||
Queue<TreeNode> q = new LinkedList<>(Arrays.asList(root)); | ||
boolean isEven = true; | ||
|
||
for (; !q.isEmpty(); isEven = !isEven) | ||
{ | ||
int prevVal = isEven ? Integer.MIN_VALUE : Integer.MAX_VALUE; | ||
|
||
for (int sz = q.size(); sz > 0; --sz) | ||
{ | ||
TreeNode node = q.poll(); | ||
|
||
if (isEven && (node.val % 2 == 0 || node.val <= prevVal)) | ||
return false; // invalid case on even level | ||
|
||
if (!isEven && (node.val % 2 == 1 || node.val >= prevVal)) | ||
return false; // invalid case on odd level | ||
|
||
prevVal = node.val; | ||
if (node.left != null) | ||
q.offer(node.left); | ||
|
||
if (node.right != null) | ||
q.offer(node.right); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |