Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.HashMap;

class Solution {
public int[] twoSum(int[] nums, int target) {


int n=nums.length;

HashMap<Integer,Integer> map = new HashMap<>();

for(int i=0;i<n;i++){
if(!map.containsKey(target-nums[i])){
map.put(nums[i],i);
}
else{
return new int []{i,map.get(target-nums[i])};
}


}
return new int []{-1,-1};

}
}
32 changes: 32 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public static int findMax(int[] weights, int[] profit, int totalCapacity) {
return helper(weights, profit, 0, totalCapacity); // Start from index 0 with full capacity
}

private static int helper(int[] weights, int[] profit, int i, int capacity) {
// Base case: if we've considered all items, return 0 profit
if (i >= weights.length) return 0;

// Case 0: skip the current item
int case0 = helper(weights, profit, i + 1, capacity);

int case1 = 0;
// Case 1: include the current item if it fits in the remaining capacity
if (capacity - weights[i] >= 0) {
case1 = profit[i] + helper(weights, profit, i + 1, capacity - weights[i]);
}

// Return the maximum profit between including or excluding the current item
return Math.max(case0, case1);
}

// Main method to test the solution
public static void main(String args[]) {
int[] items = {10, 20, 30, 40}; // Weights of the items
int[] profit = {130, 110, 170, 190}; // Corresponding profits
int capacity = 50; // Total capacity of the knapsack

// Print the maximum profit that can be obtained
System.out.println(findMax(items, profit, capacity)); // Expected: 300
}
}