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
19 changes: 19 additions & 0 deletions Find min in rotated sorted array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int findMin(int[] nums) {
int start = 0;
int end =nums.length-1;
int mid=0;
while(start<=end){
mid=start+(end-start)/2;

if(nums[mid]>=nums[end]){
start=mid+1;
}
else {
end=mid;
}
}

return nums[mid];
}
}
13 changes: 13 additions & 0 deletions best-time-to-buy-stock/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int maxProfit(int[] prices) {
int max=0;
int minAmount=prices[0];

for(int i=1;i<prices.length;i++){
int profit=prices[i]-minAmount;
max=Math.max(max,profit);
minAmount=Math.min(minAmount,prices[i]);
}
return max;
}
}
13 changes: 13 additions & 0 deletions contains duplicate/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> hash = new HashSet<>();

for(int i =0;i<nums.length;i++){
if(!hash.add(nums[i])){
return true;
}
}

return false;
}
}
17 changes: 17 additions & 0 deletions max product subarray/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int maxProduct(int[] nums) {
int max=nums[0];
int min=nums[0];
int globalMax=nums[0];
for (int i =1;i<nums.length;i++){

int tmp=max;
max=Math.max(nums[i],Math.max(max*nums[i],min*nums[i]));
min=Math.min(nums[i],Math.min(tmp*nums[i],min*nums[i]));

globalMax=Math.max(max,globalMax);
}

return globalMax;
}
}
22 changes: 22 additions & 0 deletions max subarray/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int maxSubArray(int[] nums) {
int max=nums[0];
int value=nums[0];
for(int i=1;i<nums.length;i++){
if(value<0){
if(nums[i]>=value){
value=nums[i];
max=Math.max(value,max);
continue;
}
}
else{
value+=nums[i];
max=Math.max(max,value);
}

}

return max;
}
}
23 changes: 23 additions & 0 deletions product of array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] arr=new int[nums.length];
int value=1;
int[] prod=new int[nums.length];
for(int i=0;i<nums.length;i++){
value*=nums[i];
arr[i]=value;
}

int j =1;
int k = arr.length-2;

for(int i=nums.length-1;i>0;i--){

prod[i]=arr[k]*j;
j=j*nums[i];
k--;
}
prod[0]=j;
return prod;
}
}
19 changes: 19 additions & 0 deletions sum of integers/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int getSum(int a, int b) {
int carry =(a&b)<<1;

int sum=a^b;
int total = sum ^ carry;

System.out.println(total);

while(carry!=0){
carry=(sum&carry)<<1;
sum=total;
total^=carry;

}

return total;
}
}
19 changes: 19 additions & 0 deletions twosum/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
Map<Integer,Integer> hash=new HashMap<>();

for(int i=0;i<nums.length;i++){
if(hash.containsKey(target-nums[i])){
arr[0]=hash.get(target-nums[i]);
arr[1]=i;
return arr;
}

hash.put(nums[i],i);
}

return arr;

}
}