diff --git a/challenges/April22.java b/challenges/April22.java new file mode 100644 index 0000000..e53ce29 --- /dev/null +++ b/challenges/April22.java @@ -0,0 +1,46 @@ +package hanson; +/*Good morning! Here's your coding interview problem for today. + +This problem was recently asked by Google. + +Given a list of numbers and a number k, return whether any two numbers from the list add up to k. + +For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. + +Bonus: Can you do this in one pass?*/ +import java.io.*; +import java.util.HashSet; + +public class April22 +{ + + + + static void printpairs(int arr[],int sum) + { + HashSet s = new HashSet(); + for (int i=0; i=0 && s.contains(temp)) + { + System.out.println("Pair with given sum " + + sum + " is (" + arr[i] + + ", "+temp+")"); + } + s.add(arr[i]); + } + } + + // Main to test the above function + public static void main (String[] args) + { + int A[] = {10,15,3,7}; + int n = 17; + printpairs(A, n); + } + + +} diff --git a/challenges/ArrayProdExclude_Itself.java b/challenges/ArrayProdExclude_Itself.java new file mode 100644 index 0000000..9dba152 --- /dev/null +++ b/challenges/ArrayProdExclude_Itself.java @@ -0,0 +1,48 @@ +package hanson; +/* #02 + This problem was asked by Uber. + +Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. + +For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. + +Follow-up: what if you can't use division? */ + +//import java.io.*; +/*using division tech , we can solve this in O(n) time. +we cannot use division technique.. */ + +public class ArrayProdExclude_Itself +{ + static int[] res_arr=new int[100]; + + + static void calc(int arr[],int n) + { + int prod=1; + for (int i=0;i 0) + arr[x - 1] = -arr[x - 1]; + } + + // Return the first index value at which + // is positive + for(i = 0; i < size; i++) + if (arr[i] > 0) + return i+1; // 1 is added becuase indexes + // start from 0 + + return size+1; + } + + /* Find the smallest positive missing + number in an array that contains + both positive and negative integers */ + static int findMissing(int arr[], int size) + { + // First separate positive and + // negative numbers + int shift = segregate (arr, size); + int arr2[] = new int[size-shift]; + int j=0; + for(int i=shift;ib)?a:b; + } + public static void dispSubstring(int start,int end)// to display part of string from start to end + { + System.out.println("longest substring is:-"); + for(int i=start;i<=end;i++) + { + System.out.print(s.charAt(i)); + } + } + public static void main(String a[]) + { + + + int arr[][]=new int[s.length()][s.length()]; // a 2d array of nXn size + for(int i=1;i=1;l--) // for diagonl movement of the array + { + if(s.charAt(i)!=s.charAt(j)) + arr[i][j]=max(arr[i][j-1],arr[i+1][j]); + else + arr[i][j]=2+arr[i+1][j-1]; + i++;j++; + + } + } + System.out.println("Dynamic table developed is:-");; + for(int i=0;i=m)&&(s+w[k+1]<=m)) + { + x[k]=0; + sos(s,k+1,r-w[k]); + } + + + } + public static void main(String arg[]) + { + SmSubSet s=new SmSubSet(); + + s.sos(0,0,90); + } + +} diff --git a/challenges/Xor_linked_list.java b/challenges/Xor_linked_list.java new file mode 100644 index 0000000..3ce89a2 --- /dev/null +++ b/challenges/Xor_linked_list.java @@ -0,0 +1,5 @@ +package hanson; + +public class Xor_linked_list { + +} diff --git a/challenges/uniqueelem_day40.java b/challenges/uniqueelem_day40.java new file mode 100644 index 0000000..a6b706e --- /dev/null +++ b/challenges/uniqueelem_day40.java @@ -0,0 +1,37 @@ +package handson; +/* + * This problem was asked by Google. + +Given an array of integers where every integer occurs three times except for one integer, which only occurs once, find and return the non-duplicated integer. + +For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19. + +Do this in O(N) time and O(1) space. + */ + +public class uniqueelem_day40 +{ + public static void main(String a[]) + { int arr[]= {6,1,3,3,3,6,6}; + int largest=0; + for(int i=0;iarr[i])?largest:arr[i]; + } + int hash[]=new int[largest+1];//creating hash table of ssize of largest element of array + + for(int i=0;i