Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21st - 24th Problems #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions Sowmya/Arrays/3Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(set(nums))==1 and nums[0]==0:
return [[0,0,0]]
hashmap = {}
result = set()

for i in range(len(nums)):
hashmap[nums[i]]=i

for i in range(len(nums)):
for j in range(i+1, len(nums)):
k = -(nums[i]+nums[j])
if k in hashmap.keys() and (hashmap[k]!=i and hashmap[k]!=j):
result.add(tuple(sorted([nums[i], nums[j], k])))

return result
18 changes: 18 additions & 0 deletions Sowmya/Arrays/BuySellStock1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class BuySellStock1 {
public int maxProfit(int[] prices) {
int min_price = Integer.MAX_VALUE;
int profit = 0;
for(int i = 0; i < prices.length; i++){
if(min_price > prices[i]){
min_price = prices[i];
}
else if(min_price < prices[i]){
int diff = prices[i] - min_price;
if(diff > profit){
profit = diff;
}
}
}
return profit;
}
}
20 changes: 20 additions & 0 deletions Sowmya/Arrays/ChocolateDistribution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;
class ChocolateDistribution
{
public long findMinDiff (ArrayList<Integer> a, int n, int m)
{
int min_diff = Integer.MAX_VALUE;
Collections.sort(a);
int i = 0;
int j = m - 1;
while(j < n){
int diff = a.get(j) - a.get(i);
if(diff < min_diff){
min_diff = diff;
}
i += 1;
j += 1;
}
return min_diff;
}
}
26 changes: 26 additions & 0 deletions Sowmya/Arrays/ContainsMostWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ContainsMostWater {
public int maxArea(int[] height) {
int max_water = 0;
int i = 0;
int j = height.length - 1;

while(i<j){
if(height[i] < height[j]){
int product = height[i]*(j-i);
i += 1;
if(product > max_water){
max_water = product;
}
}
else{
int product = height[j]*(j-i);
j -= 1;
if(product > max_water){
max_water = product;
}
}
}

return max_water;
}
}
22 changes: 22 additions & 0 deletions Sowmya/Arrays/DuplicateNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class DuplicateNum {
public int findDuplicate(int[] nums) {
int tortoise = 0;
int hare = 0;
for(int i = 0; i<nums.length; i++){
tortoise = nums[tortoise];
hare = nums[nums[hare]];
if(tortoise == hare){
break;
}
}
tortoise = 0;
for(int i = 0; i<nums.length; i++){
tortoise = nums[tortoise];
hare = nums[hare];
if(tortoise == hare){
break;
}
}
return tortoise;
}
}
16 changes: 16 additions & 0 deletions Sowmya/Arrays/FindDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.*;
class FindDuplicates {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> result = new ArrayList<Integer>();
for(int i = 0; i<nums.length; i++){
int ind = Math.abs(nums[i])-1;
if(nums[ind] < 0){
result.add(Math.abs(nums[i]));
}
else{
nums[ind] = 0 - nums[ind];
}
}
return result;
}
}
18 changes: 18 additions & 0 deletions Sowmya/Arrays/MajorityElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class MajorityElement {
public int majorityElement(int[] nums) {
int selected_num = 0;
int count = 0;
for(int i = 0; i<nums.length; i++){
if(count == 0){
selected_num = nums[i];
}
if(nums[i]==selected_num){
count += 1;
}
else{
count -= 1;
}
}
return selected_num;
}
}
24 changes: 24 additions & 0 deletions Sowmya/Arrays/MaxPoints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class MaxPoints {
public int maxScore(int[] cardPoints, int k) {
int len = cardPoints.length;
int size = len - k;
int window = 0;
int points = 0;
for(int i = 0; i<len; i++){
points += cardPoints[i];
}
for(int i = 0; i<size; i++){
window += cardPoints[i];
}
int result = points - window;
int j = 0;
for(int i = size; i<len; i++){
window -= cardPoints[j++];
window += cardPoints[i];
if((points - window) > result){
result = points - window;
}
}
return result;
}
}
33 changes: 33 additions & 0 deletions Sowmya/Arrays/MergeSorted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class MergeSorted {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int p1 = m - 1;
int p2 = n - 1;
int p = m + n - 1;
while((p1>=0)&&(p2>=0)&&(p>=0)){
if(nums1[p1]>nums2[p2]){
nums1[p] = nums1[p1];
p1 -= 1;
}
else{
nums1[p] = nums2[p2];
p2 -= 1;
}
p -= 1;
}

if(p1 >= 0){
while(p>=0){
nums1[p] = nums1[p1];
p -= 1;
p1 -= 1;
}
}
if(p2 >= 0){
while(p>=0){
nums1[p] = nums2[p2];
p -= 1;
p2 -= 1;
}
}
}
}
14 changes: 14 additions & 0 deletions Sowmya/Arrays/MoveZeros.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class MoveZeros {
public void moveZeroes(int[] nums) {
int k = 0;
for(int i = 0; i<nums.length; i++){
if(nums[i]!=0){
nums[k++] = nums[i];
}
}

for(int i = k; i<nums.length; i++){
nums[i] = 0;
}
}
}
18 changes: 18 additions & 0 deletions Sowmya/Arrays/RemoveDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class RemoveDuplicates {
public int removeDuplicates(int[] nums) {
int i = 0;
for(int num : nums){
if(i==0){
i+=1;
}
else if(nums[i-1]!=num){
nums[i]= num;
i+=1;
}
}
for(int j = 0; j<nums.length; j++){
System.out.print(nums[j] + " ");
}
return i;
}
}
19 changes: 19 additions & 0 deletions Sowmya/Arrays/SetZeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j]==0:
for k in range(len(matrix[i])):
if matrix[i][k]!=0:
matrix[i][k]='a'
for k in range(len(matrix)):
if matrix[k][j]!=0:
matrix[k][j]='a'

for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j]=='a':
matrix[i][j]=0
25 changes: 25 additions & 0 deletions Sowmya/Arrays/SortColors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class SortColors {
public void sortColors(int[] nums) {
int low = 0;
int high = nums.length - 1;
int mid = 0;
for(int i = 0; i<nums.length; i++){
if(nums[mid]==0){
int temp = nums[low];
nums[low] = nums[mid];
nums[mid] = temp;
low += 1;
mid += 1;
}
else if(nums[mid]==1){
mid += 1;
}
else if(nums[mid]==2){
int temp = nums[mid];
nums[mid] = nums[high];
nums[high] = temp;
high -= 1;
}
}
}
}
21 changes: 21 additions & 0 deletions Sowmya/Arrays/SubDivisible.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.*;
class SubDivisible {
public int subarraysDivByK(int[] nums, int k) {
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
int result = 0;
map.put(0,1);
int sum = 0;
for(int i = 0; i<nums.length; i++){
sum += nums[i];
int remainder = sum%k;
if(remainder < 0){
remainder += k;
}
if(map.containsKey(remainder)){
result += map.get(remainder);
}
map.put(remainder, map.getOrDefault(remainder, 0)+1);
}
return result;
}
}
17 changes: 17 additions & 0 deletions Sowmya/Arrays/SubSumEqualsK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.*;
class SubSumEqualsK {
public int subarraySum(int[] nums, int k) {
HashMap <Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, 1);
int sum = 0;
int result = 0;
for(int i = 0; i<nums.length; i++){
sum += nums[i];
if(map.containsKey(sum-k)){
result += map.get(sum - k);
}
map.put(sum, map.getOrDefault(sum, 0)+1);
}
return result;
}
}
14 changes: 14 additions & 0 deletions Sowmya/Arrays/TwoSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.*;
class TwoSum {
public int[] twoSum(int[] nums, int target) {
HashMap <Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i<nums.length; i++){
int difference = target - nums[i];
if(hm.containsKey(difference)){
return new int[] {i, hm.get(difference)};
}
hm.put(nums[i], i);
}
return new int[] {};
}
}
32 changes: 32 additions & 0 deletions Sowmya/Mathematics/AddBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class AddBinary {
public String addBinary(String a, String b) {
String result = "";
int i = a.length() - 1;
int j = b.length() - 1;
int carry = 0;
while((i>=0)&&(j>=0)){
int n1 = a.charAt(i) - '0';
int n2 = b.charAt(j) - '0';
result = String.valueOf(n1^n2^carry) + result;
carry = (n1 & n2) | (carry & (n1 ^ n2));
i -= 1;
j -= 1;
}
while(i >= 0){
int n1 = a.charAt(i) - '0';
result = String.valueOf(n1^carry) + result;
carry &= n1;
i -= 1;
}
while(j >= 0){
int n2 = b.charAt(j) - '0';
result = String.valueOf(n2^carry) + result;
carry &= n2;
j -= 1;
}
if(carry==1){
result = '1' + result;
}
return result;
}
}
Loading