Skip to content

Instantly share code, notes, and snippets.

Node Delete(Node head, int position) {
if (head==null){
return head;
}
if(position==0){
return head.next;
}
head.next = Delete(head.next, position-1);
return head;
}
public class MedianofTwoSortedArrays{
/* Solution
* Take minimum size of two array. Possible number of partitions are from 0 to m in m size array.
* Try every cut in binary search way. When you cut first array at i then you cut second array at (m + n + 1)/2 - i
* Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is partition around which lies the median.
*
* Time complexity is O(log(min(x,y))
* Space complexity is O(1)
*
/**
* Given an array of integers, find two numbers such that they add up to a specific target number.
*
* The function twoSum should return indices of the two numbers such that they add up to the target,
* where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
*
* You may assume that each input would have exactly one solution.
*
* Input: numbers={2, 7, 11, 15}, target=9
* Output: index1=1, index2=2
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.alg.leetup;
import java.util.Arrays;
/**
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.alg.ctci.linkedlist;
/**
*
* You have two numbers represented by a linked list, where each node contains a
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class PairDifferenceK {
static int pairs(int[] a,int k) {
/* Complete this function */
//sort the array first
package com.alg.leetup;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
package com.alg.leetcode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
/**
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
* Find all unique quadruplets in the array which gives the sum of target.
*
* Note:
* Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a &le; b &le; c &le; d)
* The solution set must not contain duplicate quadruplets.
* For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
*
* A solution set is: