From 06045a1e6c8fa9d6984d34cbdfe788be8ae6ef61 Mon Sep 17 00:00:00 2001 From: Neel Date: Mon, 22 Apr 2019 17:04:10 +0530 Subject: [PATCH 01/10] Add files via upload --- challenges/April22.java | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 challenges/April22.java 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); + } + + +} From 532c22b2eb37a1fa1265492eeefc8ae70b68d983 Mon Sep 17 00:00:00 2001 From: Neel Date: Sun, 28 Apr 2019 23:38:38 +0530 Subject: [PATCH 02/10] Add files via upload --- challenges/ArrayProdExclude_Itself.java | 48 ++++++++++ challenges/FstMissngPosInt.java | 120 ++++++++++++++++++++++++ challenges/Xor_linked_list.java | 5 + 3 files changed, 173 insertions(+) create mode 100644 challenges/ArrayProdExclude_Itself.java create mode 100644 challenges/FstMissngPosInt.java create mode 100644 challenges/Xor_linked_list.java 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;i Date: Sun, 19 May 2019 14:00:58 +0530 Subject: [PATCH 03/10] Add files via upload This problem was asked by Facebook. Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). For example, given the string "([])[]({})", you should return true. Given the string "([)]" or "((()", you should return false. --- challenges/DCP27braces.java | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenges/DCP27braces.java diff --git a/challenges/DCP27braces.java b/challenges/DCP27braces.java new file mode 100644 index 0000000..602fea5 --- /dev/null +++ b/challenges/DCP27braces.java @@ -0,0 +1,43 @@ +package handson; + +public class DCP27braces +{ + char[] stac=new char[10]; + int top=-1; + void push(char ch) + { + stac[++top]=ch; + } + char pop() + { + return stac[top--]; + } + public static void main(String ag[]) + { char res='a'; + int flag=1; + DCP27braces ob=new DCP27braces(); + String s="[{[]}]{}"; + for(int i=0;i Date: Mon, 3 Jun 2019 02:19:36 +0530 Subject: [PATCH 04/10] Add files via upload day #42 --- challenges/SmSubSet.java | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 challenges/SmSubSet.java diff --git a/challenges/SmSubSet.java b/challenges/SmSubSet.java new file mode 100644 index 0000000..668ac7b --- /dev/null +++ b/challenges/SmSubSet.java @@ -0,0 +1,52 @@ +package handson; +/* This problem was asked by Google. + +Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null. + +Integers can appear more than once in the list. You may assume all numbers in the list are positive. + +For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24. +*/ + + +public class SmSubSet +{ + //int[] x=new int[4];int m; + int[] x= {0,0,0,0,0,0}; + int[] w= {1,2,5,9,12,61}; + int m=24; + public void disp(int[] ar) + { + 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); + } + +} From 359aaae01d61e540fd16c1abd02e8178599a52ef Mon Sep 17 00:00:00 2001 From: Neel Date: Tue, 4 Jun 2019 11:38:04 +0530 Subject: [PATCH 05/10] Add files via upload --- challenges/Inversion_day44.java | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 challenges/Inversion_day44.java diff --git a/challenges/Inversion_day44.java b/challenges/Inversion_day44.java new file mode 100644 index 0000000..837c219 --- /dev/null +++ b/challenges/Inversion_day44.java @@ -0,0 +1,77 @@ +package handson; + + +/* Java program for Merge Sort */ +class MergeSort +{ + static int finalSum=0;//stores the number of inversion + public static void mergeSort(int[] a, int n) { + if (n < 2) + { + return; + } + int mid = n / 2; + int[] l = new int[mid]; + int[] r = new int[n - mid]; + + for (int i = 0; i < mid; i++) { + l[i] = a[i]; + } + for (int i = mid; i < n; i++) { + r[i - mid] = a[i]; + } + mergeSort(l, mid); + mergeSort(r, n - mid); + + merge(a, l, r, mid, n - mid); + + } + public static void merge(int[] a, int[] l, int[] r, int left, int right) + { + int sum=0;//count the no. of inversions in each merge of the arrays + int i = 0, j = 0, k = 0; + while (i < left && j < right) { + if (l[i] <= r[j]) { + a[k++] = l[i++]; + } + else { + sum+=left-i;//increase the count if elem from 2nd array if placed fst to elem from from fst array + a[k++] = r[j++];//increases by the no. of element left in 1st array + } + } + while (i < left) { + a[k++] = l[i++]; + } + while (j < right) { + a[k++] = r[j++]; + + } + finalSum+=sum; + } + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i Date: Tue, 4 Jun 2019 20:43:46 +0530 Subject: [PATCH 06/10] Add files via upload --- challenges/Itinerary_day41.java | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenges/Itinerary_day41.java diff --git a/challenges/Itinerary_day41.java b/challenges/Itinerary_day41.java new file mode 100644 index 0000000..3efb7c1 --- /dev/null +++ b/challenges/Itinerary_day41.java @@ -0,0 +1,45 @@ +package handson; +/* This problem was asked by Facebook. + +Given an unordered list of flights taken by someone, each represented as (origin, destination) pairs, and a starting airport, compute the person's itinerary. If no such itinerary exists, return null. If there are multiple possible itineraries, return the lexicographically smallest one. All flights must be used in the itinerary. + +For example, given the list of flights [('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL', 'YYZ'), ('HKO', 'ORD')] and starting airport 'YUL', you should return the list ['YUL', 'YYZ', 'SFO', 'HKO', 'ORD']. + +Given the list of flights [('SFO', 'COM'), ('COM', 'YYZ')] and starting airport 'COM', you should return null. + +Given the list of flights [('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')] and starting airport 'A', +you should return the list ['A', 'B', 'C', 'A', 'C'] even though ['A', 'C', 'A', 'B', 'C'] is also a valid itinerary. However, the first one is lexicographically smaller. + + */ + +public class Itinerary_day41 +{ + + public static String hashe[][]= {{"a","b"},{"a","c"},{"b","c",},{"c","a"}}; + public static int vis[]= {0,0,0,0}; + public static int count=0; + public static void main(String a[]) + { + String toSearch="a"; + int j=0; + System.out.print(toSearch+","); + while(count !=4)//no. of flights. here its=4 + { + if(toSearch.equals(hashe[j][0])&&vis[j]==0)//to match the origin flight and ensure its not taken again since there there is different origin from same destination, so if ignored can be mistakenly taken as cycle + { count++; + vis[j]=1; + System.out.print(hashe[j][1]+","); + toSearch=hashe[j][1]; + j=0; + } + else + { + j++; + } + } + + } + } + + + From 7060d9989f529fbb7d49f05eea54833d02f01e1a Mon Sep 17 00:00:00 2001 From: Neel Date: Wed, 5 Jun 2019 17:28:42 +0530 Subject: [PATCH 07/10] Add files via upload --- challenges/uniqueelem_day40.java | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenges/uniqueelem_day40.java 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 Date: Thu, 6 Jun 2019 11:53:03 +0530 Subject: [PATCH 08/10] Add files via upload --- challenges/LongestPal_day45.java | 73 ++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 challenges/LongestPal_day45.java diff --git a/challenges/LongestPal_day45.java b/challenges/LongestPal_day45.java new file mode 100644 index 0000000..9da38f8 --- /dev/null +++ b/challenges/LongestPal_day45.java @@ -0,0 +1,73 @@ +package handson; + +public class LongestPal_day45 +{ + static String s="bananasannanab"; + public static int max(int a,int b)// to find maximum of two + { + return (a>b)?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