Category Archives: LeetCode Solutions

Divide Two Integers

https://leetcode.com/problems/divide-two-integers/description/ package src.test.java.examples;package src.test.java.examples; public class Dividend { public int divide(int dividend, int divisor) { int count = 0; boolean flag = false; if (dividend < 0 && divisor > 0) { flag = true; } else if (dividend > … Continue reading

Posted in Java, LeetCode Solutions, Uncategorized | Tagged | Leave a comment

Knapsack Problem using Java

/** * Knapsack Problem to maximize value given the weight * @author asharda * */ public class Knapsack { public int maxValue(int []weight,int []value,int maxWeight) { int sum[]=new int [value.length]; sum[0]=value[0]; int max=value[0]; for(int i=1;i<value.length;i++) { if(weight[i-1]+weight[i]<=maxWeight) { sum[i]=Math.max(value[i], value[i-1]+value[i]); … Continue reading

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

Median of Two Sorted Arrays -LeetCode

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)   ”’ Created on Jun 23, 2017 @author: asharda ”’ import numpy as … Continue reading

Posted in LeetCode Solutions, Uncategorized | Leave a comment

Reverse an Integer -LeetCode

import java.util.ArrayList; import java.util.List; import org.testng.annotations.Test; /** * @author asharda * */ public class TestReverseInteger { @Test public void testReverseInteger() { int num=123; int div=num; int rem=0; List<Integer> list=new ArrayList<Integer>(); while(div>100) { div=num/10; rem=num%10; list.add(rem); } list.add(div%10); list.add(div/10); Integer [] … Continue reading

Posted in Java, LeetCode Solutions, Uncategorized | Leave a comment

Single Element in a Sorted Array

/** * Find Single Non Duplicate Element in Array. * @author asharda * */ public class Solution { public static void main(String[] args) { int []arr={3,4,5,3,4,5,6}; System.out.println(“Result is “+singleNonDuplicate(arr)); } /** * Find single Non Duplicate * @param nums * … Continue reading

Posted in Java, LeetCode Solutions | Leave a comment

Divide function

package tests; import org.testng.annotations.Test; /** * Divide without standard divison and using add. * @author asharda * */ public class DivisonTest { public int divide(int dividend,int divisor) throws Exception { int count=0; int temp=divisor; if((divisor ==0) && (dividend!=0)) { throw new … Continue reading

Posted in Java, LeetCode Solutions, Uncategorized | Leave a comment