package Companies; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; public class ClearWater { public static void main(String[] args) { List arr = new ArrayList<>(); arr.add(10); arr.add(-5); arr.add(6); splitIntoTwo(arr); } // Question : Merge the given 2 arrays into one public static List mergeArrays(List a, List b) { int lenA = a.size(); int lenB = b.size(); if(lenA == 0 && lenB == 0) return Collections.EMPTY_LIST; List c = new ArrayList<>(lenA+lenB); int i = 0; int j = 0; while(i < lenA || j < lenB) { if(i < lenA && j < lenB && a.get(i) < b.get(j)) { c.add(a.get(i)); i++; } else if(i < lenA && j < lenB && a.get(i) == b.get(j)) { c.add(a.get(i)); c.add(b.get(j)); i++; j++; } else if(i < lenA && j < lenB && a.get(i) > b.get(j)) { c.add(b.get(j)); j++; } else if(i < lenA && j == lenB) { c.add(a.get(i)); i++; } else if(i == lenA && j < lenB) { c.add(b.get(j)); j++; } } return c; } // Question : Find if the given list of numbers has duplicate public static boolean containsDuplicate(List numbers) { int len = numbers.size(); Set set = new HashSet<>(); for(int i = 0; i < len; i++) { if(!set.add(numbers.get(i))) return true; } return false; } // Question : Split the given list of integers into 2, such that sum of left partition elememts // is greater than that of right partition. public static int splitIntoTwo(List arr) { int count = 0; int lTotal = 0; int rTotal = 0; int len = arr.size(); if(len == 0) return 0; lTotal = arr.get(0); for(int i = 1; i < len; i++) { rTotal+=arr.get(i); } if(lTotal > rTotal) count++; for(int i = 1; i < len-1; i++) { lTotal+=arr.get(i); rTotal-=arr.get(i); if(lTotal > rTotal) count++; } return count; } } // Question : Task was to implement below classes to provide the area class Circle { private float radius; public Circle(float radius) { this.radius = radius; } public int getArea() { double area = (float)3.14159265*radius*radius; return (int)Math.ceil(area); } } class Rectangle { private float height; private float width; public Rectangle(float height, float width) { this.height = height; this.width = width; } public int getArea() { double area = (float)height*width; return (int)Math.ceil(area); } } class Square { private float width; public Square(float width) { this.width = width; } public int getArea() { double area = (float)width*width; return (int)Math.ceil(area); } }