# 11. Container With Most Water

11. Container With Most Water

``````class Solution {
public int maxArea(int[] height) {
int maxArea =  Integer.MIN_VALUE;
int l = 0;
int r = height.length - 1;
while(l < r){
maxArea = Integer.max(maxArea, Integer.min(height[l],height[r])*(r-l));
if(height[l] < height[r]) l++;
else r--;
}
return maxArea;
}
}``````

# 62. Unique Paths

62. Unique Paths

Dynamic Programming

``````class Solution {
public int uniquePaths(int m, int n) {
if(n ==1 && m ==1) return 1;
int[][] grid = new int[m][n];

for(int i = 1;  i <m; i++){
grid[i][0] = 1;
}

for(int i = 1; i < n; i++){
grid[0][i] = 1;
}
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
grid[i][j] =grid[i-1][j] + grid[i][j-1];
}
}
return grid[m-1][n-1];
}
}``````

# 605. Can Place Flowers

605. Can Place Flowers

```class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if(flowerbed.length == 1 && flowerbed[0] == 0) n--;
for(int i = 0; i < flowerbed.length; i++){
if(i ==0 && flowerbed[i] == 0 && i+1 <  flowerbed.length && flowerbed[i+1] == 0){
flowerbed[i] = 1;
n--;
}else if( i == flowerbed.length -1 &&
flowerbed[i] == 0 && i-1 >= 0 &&
flowerbed[i-1] == 0){
flowerbed[i] = 1;
n--;
}
else if(flowerbed[i] == 0  &&
i-1 >= 0 &&
flowerbed[i-1] == 0  &&
i+1 <  flowerbed.length &&
flowerbed[i+1] == 0){
flowerbed[i] = 1;
n--;
}
}
if(n <= 0) return true;
else return false;
}
}```

# Birthday Cake Candles

Birthday Cake Candles

```    public static int birthdayCakeCandles(List<Integer> candles) {
// Write your code here
TreeMap<Integer,Integer> f = new TreeMap<>();
for (Integer a: candles)
f.put(a,f.getOrDefault(a, 0)+1);
return f.lastEntry().getValue();
}

```

# Mini-Max Sum

https://www.hackerrank.com/challenges/mini-max-sum/problem

```   public static void miniMaxSum(List<Integer> arr) {
// Write your code here
Collections.sort(arr);
BigInteger sum = BigInteger.ZERO;
for (int i = 0; i < arr.size(); i++){
sum =sum.add(BigInteger.valueOf(arr.get(i)));
}
System.out.print(sum.subtract(BigInteger.valueOf(arr.get(arr.size()-1).intValue()))+" ");
System.out.print(sum.subtract(BigInteger.valueOf(arr.get(0).intValue())));

}

```

# Compare the Triplets

https://www.hackerrank.com/challenges/compare-the-triplets/problem

``````    public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
// Write your code here
List<Integer> out = new ArrayList<Integer>(2);
out.add(0);
out.add(0);
for (int i = 0; i < a.size(); i++) {
if (a.get(i) > b.get(i))
out.set(0, out.get(0) + 1);
else if (a.get(i) < b.get(i))
out.set(1, out.get(1) + 1);
}

return out;
}``````

# 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.
Therefore, the answer is 6.

``````    public static long countPairs(List<Integer> arr) {
// Write your code here
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;
}
```

# Slow Sums

```	public int getTotalTime(int[] arr) {
Arrays.sort(arr);
int p = arr[arr.length-1];
int sum = 0;
for (int i = arr.length - 2; i >=0; i--) {
p += arr[i];
sum = sum +p;
}
return sum;
}
```