Skip to content

Commit

Permalink
Add TwO sum.
Browse files Browse the repository at this point in the history
  • Loading branch information
PRkudupu committed Jun 4, 2017
1 parent 9dc734a commit 888bbb2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
Binary file modified bin/Algorithms/Arrays/BestTimeToBuyStock.class
Binary file not shown.
Binary file modified bin/Algorithms/Arrays/TwoSum.class
Binary file not shown.
Binary file modified bin/Home.class
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Algorithms/Arrays/BestTimeToBuyStock.java
Expand Up @@ -3,7 +3,13 @@
* of the original array, and find a contiguous subarray giving maximum profit. If the difference falls below 0, reset it to zero.
*/
package Algorithms.Arrays;
/*
* Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger
than buying price)
*/
public class BestTimeToBuyStock {

public static int get(int [] prices){
Expand Down
8 changes: 4 additions & 4 deletions src/Algorithms/Arrays/TwoSum.java
Expand Up @@ -22,12 +22,12 @@ public class TwoSum {
//In this scenario in the 1st iteration 2 is inserted as key
//In the second iteration when we check for contains key we would have the key so now we know that
// [0] ,[1] is the sum of two
if(map.containsKey(target-arr[i])){
result[1]=i+1;
result[0]=map.get(target-arr[i]);
if(map.containsKey(arr[i])){
result[0]=map.get(arr[i]);
result[1]=i;
return result;
}
map.put(arr[i],arr[i+1]);
map.put(target-arr[i],i);
}
return result;

Expand Down
12 changes: 6 additions & 6 deletions src/Home.java
Expand Up @@ -144,17 +144,17 @@ public static void main(String args[]){
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/*
int [] arr= {2, 7, 11, 15};
/* */
int [] arr= {3, 2, 7, 15};
int target=9;
int[] result= new int [2];

result= Algorithms.TwoSum.get(arr,target);
result= Algorithms.Arrays.TwoSum.get(arr,target);
for(int i:result)
{
System.out.println("Tow sum array indices "+ i);

}*/
}
/***************
*
*Given arr = [1, 7, 8, 10,20,7]
Expand Down Expand Up @@ -417,10 +417,10 @@ public static void main(String args[]){
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger
than buying price)
*/
int arr [] ={7, 1, 5, 3, 6, 4};
/*int arr [] ={7, 1, 5, 3, 6, 4};
int bestTimeToBuyStock=Algorithms.Arrays.BestTimeToBuyStock.get(arr);
System.out.println("Best Time to buy stock : "+ bestTimeToBuyStock);

*/


}
Expand Down

0 comments on commit 888bbb2

Please sign in to comment.