Skip to content

Instantly share code, notes, and snippets.

View gauravkr0071's full-sized avatar
❤️

Gaurav gauravkr0071

❤️
View GitHub Profile
@gauravkr0071
gauravkr0071 / Minimum Operations to Reduce X to Zero.cpp
Last active August 5, 2021 14:22
Minimum Operations to Reduce X to Zero
/*
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.
Return the minimum number of operations to reduce x to exactly 0 if it is possible, otherwise, return -1
BASED ON LONGEST SUBARRAY WITH K SUM
Example 1:
Input: nums = [1,1,4,2,3], x = 5
Output: 2
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
Example 2:
@gauravkr0071
gauravkr0071 / longest subarray with k sum.cpp
Created August 5, 2021 14:22
longest subarray with k sum
/*
find the longest subbary with sum k,
k can also be zero, return -1 if no array with sum k is present
*/
unordered_map<int, int>u;
int len=INT_MIN;
for(int i=0;i<nums.size();i++)
{
temp+=nums[i];
@gauravkr0071
gauravkr0071 / number of subarray having sum k.cpp
Created August 5, 2021 14:39
number of subarray having sum k
/*
Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k.
k can also be zero
Example 1:
Input: nums = [1,1,1], k = 2
Output: 2
Example 2:
@gauravkr0071
gauravkr0071 / Minimum Size Subarray Sum.cpp
Last active August 5, 2021 14:46
Minimum Size Subarray Sum
/*
Given an array of __POSITIVE INTEGERS__ nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.
DO NOT work if elements are negative
Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
@gauravkr0071
gauravkr0071 / Minimum window substring.cpp
Created August 6, 2021 09:06
Minimum window substring
*/
Minimum Window Substring
Hard
7469
471
Add to List
@gauravkr0071
gauravkr0071 / Longest Substring with At Most Two Distinct Characters.cpp
Created August 6, 2021 09:08
Longest Substring with At Most Two Distinct Characters
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> map(128, 0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++==0) counter++;
while(counter>2) if(map[s[begin++]]--==1) counter--;
d=max(d, end-begin);
}
return d;
}
@gauravkr0071
gauravkr0071 / Longest Substring Without Repeating Characters.cpp
Created August 6, 2021 09:09
Longest Substring Without Repeating Characters
int lengthOfLongestSubstring(string s) {
vector<int> map(128,0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++>0) counter++;
while(counter>0) if(map[s[begin++]]-->1) counter--;
d=max(d, end-begin); //while valid, update d
}
return d;
}
@gauravkr0071
gauravkr0071 / subarray with maximum sum.cpp
Last active August 6, 2021 10:52
subarray with maximum sum
#include<iostream>
#include<climits>
using namespace std;
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
@gauravkr0071
gauravkr0071 / Find the smallest subarray having atleast one duplicate.cpp
Last active August 7, 2021 20:48
Find the smallest subarray having atleast one duplicate
//METHOD 1- BRUTE FORCE
//METHOD-2- HASH MAP,
/*
Given an array arr of N elements, the task
is to find the length of the smallest subarray of
the given array that contains at least one duplicate
element. A subarray is formed from consecutive elements of
an array. If no such array exists, print “-1”.
link- https://www.geeksforgeeks.org/find-the-smallest-subarray-having-atleast-one-duplicate/
@gauravkr0071
gauravkr0071 / Two Sum Less Than K.java
Created August 9, 2021 18:27
Two Sum Less Than K
/*
1099. Two Sum Less Than K (https://leetcode.com/problems/two-sum-less-than-k/): Given an array A of integers and integer K,
return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K.
If no i, j exist satisfying this equation, return -1.
1099. Two Sum Less Than K (#1 Sort + Two pointer).java
*/
// Sort + Two pointer solution
// Time: O(nlogn), 1ms