GFG : Count distinct elements in every window
Problem : Given an array of integers and a number K. Find the count of distinct elements in every window of size K in the array. Solution:
Learn to Code and Code to Learn
Your Journey to Code Mastery
Problem : Given an array of integers and a number K. Find the count of distinct elements in every window of size K in the array. Solution:
Problem : Due to the rise of covid-19 cases in India, this year BCCI decided to organize knock-out matches in IPL rather than a league. Today is matchday 2 and it is between the most loved team Chennai Super Kings and the most underrated team – Punjab Kings. Stephen Fleming, the head coach of CSK, analyzing the…
Problem : You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Solution…
Problem: You are given an integer array nums and an integer k. Find the maximum subarray sum of all the subarrays of nums that meet the following conditions: The length of the subarray is k, andAll the elements of the subarray are distinct.Return the maximum subarray sum of all the subarrays that meet the conditions….
Read More “LeetCode : 2461. Maximum Sum of Distinct Subarrays With Length K” »
Problem: You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding…
Sliding window technique is used to reduce the use of nested loops and replace it with a single loop, thereby reducing the time complexity. Hand written notes
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example…
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input:…
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, …, n] and you want to…
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example 1: Input: nums = [1,2,3,1] Output: true Example 2: Input: nums = [1,2,3,4] Output: false Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Solution: