Monthly Archives: April 2018

Distribute Candies

  package test; import java.util.Arrays; import java.util.stream.Collectors; /** * @author asharda * */ public class DistributeCandies { public int distributeCandies(int[] candies) { return (int) Math.min(Arrays.stream(candies).boxed().collect(Collectors.toList()).stream().distinct().count(), candies.length / 2); } /** * @param args */ public static void main(String[] args) { … Continue reading

Posted in Uncategorized | Leave a comment

Single Number

https://leetcode.com/problems/single-number/description/   /** * SingleNumber solution * @author asharda * */ public class Solution { public int singleNumber(int[] nums) { int num = 0; for (int i = 0; i != nums.length; i++) num ^= nums[i]; return num; } public … Continue reading

Posted in Java, Java 8, Uncategorized | Leave a comment

Baseball Game

https://leetcode.com/problems/baseball-game/description/ import java.util.ArrayList; import java.util.List; public class Solution { public int calPoints(String[] ops) { int validList = 0; int lastRef = 0; int dSum = 0; int pSum = 0; List<Integer> list = new ArrayList<Integer>(); for (int i = 0; … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Leave a comment

Insertion Sort Java

package test; public class InsertionSort { public int[] sort(int []arr) { int key,i; for(int j=1;j<arr.length;j++) { key=arr[j]; i=j-1; while(i>=0 && arr[i]>key) { arr[i+1]=arr[i]; arr[i]=key; i–; } } return arr; } public static void main(String[] args) { // TODO Auto-generated method … Continue reading

Posted in Data Structures, Java, Uncategorized | Tagged | Leave a comment

Java GC

https://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java#memory-management-in-java-8211-young-generation

Posted in Uncategorized | Leave a comment

Number Complement

https://leetcode.com/problems/number-complement/description/ public class Solution { public int findComplement(int num) { String binary=Integer.toBinaryString(num); char []converted=binary.toCharArray(); for(int i=0;i<converted.length;i++) { if(converted[i]==’0′) { converted[i]=’1′; } else { converted[i]=’0′; } } return Integer.parseInt(String.valueOf(converted),2); } public static void main(String[] args) { Solution i1=new Solution(); System.out.println(“Binary value: … Continue reading

Posted in Uncategorized | Leave a comment

Remove Duplicates from an Array

package test; import java.util.HashSet; import java.util.Set; import org.testng.annotations.Test; public class ArrayDuplicate { @Test public void testRemoveArrayDuplicates() { int a[]= {1,2,1,3,4,2,5}; Set<Integer> set=new HashSet<Integer>(); for(int i=0;i<a.length;i++) { set.add(a[i]); } System.out.println(set); } }

Posted in Uncategorized | Leave a comment