Skip to main content

Posts

Showing posts with the label project

Product of Array Except Self in Python | Prefix & Suffix Explained (LeetCode 238)

Problem Overview The Product of Array Except Self is a classic problem that tests your understanding of array traversal and optimization. The task is simple to state but tricky to implement efficiently. Given an integer array nums , you need to return an array such that each element at index i is equal to the product of all the elements in nums except nums[i] . The challenge is that: Division is not allowed The solution must run in O(n) time Initial Thoughts At first glance, it feels natural to compute the total product of the array and divide it by the current element. However, this approach fails because division is forbidden and handling zeroes becomes messy. This pushed me to think differently — instead of excluding the current element, why not multiply everything around it? That’s where the prefix and suffix product pattern comes in. Key Insight: Prefix & Suffix Products For every index i : Prefix product → product of all elements to t...

Minimum Window Substring Explained | Sliding Window Technique in Python

Minimum Window Substring – Sliding Window Approach While solving the Minimum Window Substring problem, I initially struggled to manage window validity correctly. The key challenge was ensuring that the current window contains all characters of the target string with correct frequency , while still keeping the window as small as possible. Problem Intuition The problem is a classic example of a variable-size sliding window . We expand the window to the right until it becomes valid, and once valid, we try to shrink it from the left to find the minimum-length substring. To efficiently check window validity, instead of rechecking all characters every time, we maintain a frequency map and a counter that tracks how many characters are still required. Approach Create a frequency map of the target string. Use two pointers ( l and r ) to represent the sliding window. Expand the window by moving r . Decrease the required count only when a needed character is found. ...

Majority Element in an Array

Majority Element in an Array Given an integer array nums of size n , the task is to find the majority element . A majority element is defined as the element that appears more than ⌊n / 2⌋ times in the array. It is guaranteed that the majority element always exists. Problem Example Input: nums = [3, 2, 3] Output: 3 Input: nums = [2,2,1,1,1,2,2] Output: 2 Approach 1: Using Counter (Easy to Understand) A straightforward approach is to count the frequency of each element and return the one with the maximum count. Python Code from collections import Counter class Solution: def majorityElement(self, nums): freq = Counter(nums) max_count = max(freq.values()) for key in freq: if freq[key] == max_count: return key Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Although this solution is simple and readable, it us...

Python Project: AI Based Assignment Completer

Python Project: AI-Based Assignment Completer A small suite of Python scripts that generate C programs from prompts, create outputs and collect code into a .docx file. 1. File 1 — generator (generate C files) Notes: Replace the API key placeholder on the line where openai.api_key is set with your own key, or set openai.api_key elsewhere in your environment. Create a prompt.txt file in the same directory with one question per line. Create a folder named programms in the same directory — generated C files will be placed there. The Python code (unchanged): import os import openai class Ai: number=0 def automatedquery(self, query): '''this f(x) returns the responce generated by cahtgpt''' openai.api_key = "Your Api Key Here!" response = openai.Completion.create( model="text-davinci-003", prompt=query, ...