Skip to content

Commit

Permalink
Added codes for 29 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 29, 2024
1 parent 19750d7 commit 39050f1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions GeeksForGeeks/February/29-2-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/29-2-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)
2 changes: 2 additions & 0 deletions LeetCode/February/29-2-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)
48 changes: 48 additions & 0 deletions LeetCode/February/29-2-24/Solution.java
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;
}
}

0 comments on commit 39050f1

Please sign in to comment.