 # Bitwise AND

Given an array of non-negative integers, count the number of unordered pairs of array
elements such that their bitwise AND is a power of 2.
For example, let’s say the array is arr = [10, 7, 2, 8, 3], and let ‘&’ denote the bitwise AND
operator. There are 6 unordered pairs of its elements that have a bitwise AND that is a
power of two:
For indices (0,1), 10 & 7 = 2, which is a power of 2.
For indices (0,2), 10 & 2 = 2, which is a power of 2.
For indices (0,3), 10 & 8 = 8, which is a power of 2.
For indices (0,4), 10 & 3 = 2, which is a power of 2.
For indices (1,2), 7 & 2 = 2, which is a power of 2.
For indices (2,4), 2 & 3 = 2, which is a power of 2.

``````    public static long countPairs(List<Integer> arr) {
int count = 0;
for(int i = 0; i < arr.size(); i++){
for(int j= i+1; j < arr.size(); j++){
double a = Math.log(arr.get(i).intValue()&arr.get(j).intValue())/Math.log(2) ;
if(a == (int) a)
count++;
}
}
return count;
}`````` # Equalize the Array

``` public static int equalizeArray(List<Integer> arr) {
HashMap<Integer, Integer> frequ = new HashMap<>();
for (Integer a : arr)
frequ.put(a, frequ.getOrDefault(a, 0) + 1);

int maxFrequ = Integer.MIN_VALUE;
for (Integer a : frequ.keySet()) {
maxFrequ = Integer.max(maxFrequ, frequ.get(a));
}
return arr.size() - maxFrequ;

}
``` # Counting Triangles

```	class Sides {
int a;
int b;
int c;

Sides(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}

}

public int compare(Sides o1, Sides o2) {
int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
Arrays.sort(o1Sides);
int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
Arrays.sort(o2Sides);
for (int i = 0; i < o1Sides.length; i++)
if (o1Sides[i] != o2Sides[i])
return -1;

return 0;
}

public int countDistinctTriangles(ArrayList<Sides> arr) {
if (arr.size() == 0)
return 0;
Collections.sort(arr, new Comparator<Sides>() {

@Override
public int compare(Sides o1, Sides o2) {
int[] o1Sides = new int[] { o1.a, o1.b, o1.c };
Arrays.sort(o1Sides);
int[] o2Sides = new int[] { o2.a, o2.b, o2.c };
Arrays.sort(o2Sides);
for (int i = 0; i < o1Sides.length; i++)
if (o1Sides[i] != o2Sides[i])
return -1;

return 0;
}
});
int count = 1;
for (int i = 1; i < arr.size(); i++) {
if (compare(arr.get(i), arr.get(i - 1)) != 0)
count++;
}
return count;
}
``` # Balanced Split

```	public boolean balancedSplitExists(int[] arr) {
Arrays.sort(arr);
int i = 0;
int j = arr.length - 1;
int leftSum = arr;
int rightSum = arr[arr.length-1];
while(i <= j) {
if(leftSum < rightSum) {
i++;
leftSum+=arr[i];
}else if (leftSum > rightSum) {
j--;
rightSum+=arr[j];
} else break;
}
if(j-i == 1) return true;
else return false;
}
``` # Contiguous Subarrays

``````	public static int[] countSubarrays(int[] arr) {
int[] output = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int j = i;
int less = 0;
while(j>=0 && arr[j]<=arr[i]) {
less++;
j--;
}
output[i] = less;

j=i;
int greater = 0;
while(j < arr.length && arr[j] <= arr[i]) {
greater++;
j++;
}
output[i]+=greater-1;//one from duplication
}
return output;
}`````` # Number of Visible Nodes

```	  class Node {
int data;
Node left;
Node right;
Node() {
this.data = 0;
this.left = null;
this.right = null;
}
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}

int visibleNodes(Node root) {
int count = 0;
while(!q.isEmpty()) {
Node n = q.poll();
count = level.poll();
if(n.right != null ) {
}
if(n.left != null ) {
}
}
return count;
}
``` # Magical Candy Bags

```	public int maxCandies(int[] arr, int K) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Comparator.reverseOrder());
for (int i : arr)
int maxCandies = 0;
for (int i = 1; i <= K; i++) {
int rem = maxHeap.remove();
maxCandies += rem;
}

return maxCandies;
}
``` # Minimum Length Substrings

```	public static int minLengthSubstring(String s, String t) {
int min = Integer.MAX_VALUE;
boolean found = false;
for (int i = 0; i <= s.length(); i++) {
for (int j = i; j <= s.length(); j++) {
String takedString = s.substring(i, j);
boolean contained = true;
HashMap<Character, Integer> h = new HashMap<Character, Integer>();
for (Character c : t.toCharArray()) {
h.put(c, h.getOrDefault(c, 0) + 1);
}
for (Character c : takedString.toCharArray()) {
h.put(c, h.getOrDefault(c, 0) - 1);
}

for (Character c : h.keySet()) {
if (h.get(c) > 0)
contained = false;
}
if (contained) {
found = true;
min = Integer.min(min, takedString.length());
}
}
}
if (!found)
return -1;
return min;
}
```