Day 15 | Programming Classes at TAP Academy 🧠 Multiple Arrays 🔹 1. Sorted arrays Instead of brute force, we learned to observe patterns first. Example: Finding the largest repeating element in a sorted array. 👉 Instead of scanning from the beginning, start from the end. 👉 Why? Because the largest element always sits at the last. 👉 Compare adjacent elements once. If equal → done. If not → move backward. Mini logic: for (int i = n-1; i > 0; i--) { if (arr[i] == arr[i-1]) return arr[i]; } Simple logic. Clean code. Faster solution. 🔹 2. One problem → multiple variations The same structure can solve: • largest repeating element • smallest repeating element • largest repeating even element • prime repeating element Just tweak the condition: if (arr[i] == arr[i-1] && arr[i] % 2 == 0) Once logic is strong, variations become easy. 🔹 3. Common elements in two sorted arrays Big lesson here: avoid nested loops. Instead: 👉 Use two pointers (i & j) 👉 Compare step-by-step 👉 Move only the smaller pointer 👉 Print once when equal Mini logic: while (i < n && j < m) { if (a[i] == b[j]) { print(a[i]); i++; j++; } else if (a[i] < b[j]) i++; else j++; } Cleaner. Faster. No duplicates. 🔹 4. Merging sorted arrays = thinking like the brain Before coding, ask: 👉 What should the result look like? 👉 Why does each number go where it goes? Mini logic: while (i < n && j < m) { if (a[i] <= b[j]) res[k++] = a[i++]; else res[k++] = b[j++]; } Then don’t forget leftovers: while (i < n) res[k++] = a[i++]; while (j < m) res[k++] = b[j++]; That tiny detail decides whether your code passes or fails. #Programming #ProblemSolving #DSA #CodingJourney #LearningToCode #LogicBuilding #TAPAcademy #Java #Upskilling #Learning #Arrays #Logics
Arrays Programming Lessons at TAP Academy
More Relevant Posts
-
Day 22 | Programming Classes at TAP Academy 🧾 Count Variations in Strings ✨ 💥 Problem 1: Count Words in a String General Approach: words = spaces + 1; Looks correct but… 👉 What if multiple spaces exist? 👉 What if spaces appear at the start/end? logic breaks. ✅ Correct Insight A word starts when: current character = space next character ≠ space 💡 Final Code public static int countWords(String s) { int count = 0; for (int i = 0; i < s.length() - 1; i++) { if (s.charAt(i) == ' ' && s.charAt(i + 1) != ' ') { count++; } } // Handle starting space edge case if (s.charAt(0) == ' ') { return count; } else { return count + 1; } } 💥 Problem 2: Count Vowels in a String 💡 Logic A character is a vowel if: 👉 It matches a, e, i, o, u (both cases) ✅ Code int count = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { count++; } } 💥 Problem 3: Count Consonants Mostly say: 👉 “Not vowel = consonant” ❌ Wrong. Because: digits -> not vowels special characters -> not vowels But they are NOT consonants. ✅ Proper Approach Check if character is alphabet Then check: vowel -> vowel count else -> consonant 💡 Code public static int countConsonants(String s) { int cc = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); // Check if alphabet if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // Check if NOT vowel → consonant if (!(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')) { cc++; } } } return cc; } 💥 Final Boss Problem 👉 Count: Vowels Consonants Numbers Special characters 💡 Code public static void countCharacters(String s) { int vc = 0, cc = 0, nc = 0, sc = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { vc++; } else { cc++; } } else if (ch >= '0' && ch <= '9') { nc++; } else { sc++; } } } #Java #Programming #CodingInterview #DataStructures #ProblemSolving #LearnToCode #Developers #CodingJourney #TechSkills #Code #InterviewPreparation #CoreJava #Upskilling #Learning #DSA #Strings #Logics #Thinking #TAPAcademy
To view or add a comment, sign in
-
-
Day 17 | Programming Classes at TAP Academy SubArrays😊 Yesterday’s problem looked like: 👉 “Print subarrays of size k” Today, it evolved into something deeper: 👉 “Find sum of subarrays” 👉 “Count subarrays with sum = K” 👉 “Print subarrays with sum = K” 👉 “Find the largest subarray with sum = K” 🚨 The Reality Check Same base logic. Different outcomes. One small mistake → entire logic breaks. 🔥 Core Logic (Sliding Window Style Thinking) Instead of printing elements: Maintain a running sum Reset it at the right place Compare with target (K) sum += arr[j]; But here’s the catch 👇 ⚠️ Most Common Mistake (and it kills your output) If you don’t reset sum: sum = 0; // MUST reset per subarray You don’t get: 6 9 12 15 ... You get: 6 15 27 42 ... Because the sum keeps accumulating across subarrays That’s not a bug. That’s your logic leaking. 🧠 Real Understanding > Let’s break the loops: size loop -> decides subarray length i loop -> starting index j loop -> elements inside subarray you’ll never debug fast. 💡 Upgrade 1: Count Subarrays with Sum = K if(sum == k) { count++; } Simple. But placement matters: Inside i loop ❌ Outside all loops ❌ After computing sum for one subarray ✅ 💡 Upgrade 2: Print Subarrays (Not Just Count) Big mistake people made: System.out.print(arr[j]); // outside loop ❌ This prints one element, not subarray. Correct mindset: 👉 Subarray = loop 👉 Always use another loop to print it 💡 Upgrade 3: Largest Subarray with Sum = K Game changes here. Instead of: for(size = 1 → n) Reverse it: for(size = n → 1) Why? Because: 👉 First match = largest subarray Then stop execution immediately: return; // clean exit Not break, not System.exit(). ⚠️ Hidden Trap: break vs return break → exits only ONE loop return → exits the entire method That’s why your code prints multiple outputs even after finding answer. 🧩 Final Insight Every variation of this problem is NOT new. It’s just: Same loops Same structure Different intent 👉 Print 👉 Sum 👉 Count 👉 Filter 👉 Optimize If base logic is weak, every “new problem” feels new. If base is strong, everything feels like a variation. #DataStructures #Java #CodingInterview #ProblemSolving #Arrays #LogicBuilding #LearnByDoing #100DaysOfCode #Java #Programming #Learning #Coding #TAPAcademy #Logics
To view or add a comment, sign in
-
-
Day 19 | Programming Classes at TAP Academy Consecutive Sub Arrays ⚡ 🚀 From “just arrays” to real problem-solving mindset 💡 Problem 1: Find the Missing Element in an Array Given: Array starts from 1, one number is missing. 🔥 Insight: Instead of checking each number → use math. int n = arr.length + 1; int totalSum = n * (n + 1) / 2; int arraySum = 0; for(int i = 0; i < arr.length; i++) { arraySum += arr[i]; } int missing = totalSum - arraySum; System.out.println(missing); ⚡ Clean. Efficient. O(n). No sorting. No extra space. 💡 Problem 2: Print Consecutive Subarrays 👉 Key idea: Two elements are consecutive if: arr[i+1] - arr[i] == 1 for(int i = 0; i < arr.length - 1; i++) { if(arr[i+1] - arr[i] == 1) { System.out.print(arr[i] + " "); } else { System.out.println(arr[i]); } } System.out.println(arr[arr.length - 1]); 💭 Lesson: Don’t blindly use 3 loops just because it’s “subarray”. Think first. Code later. 💡 Problem 3: Length of Consecutive Subarrays int count = 1; for(int i = 0; i < arr.length - 1; i++) { if(arr[i+1] - arr[i] == 1) { count++; } else { System.out.println(count); count = 1; } } System.out.println(count); 💡 Problem 4: Longest Consecutive Subarray int count = 1, max = 0; for(int i = 0; i < arr.length - 1; i++) { if(arr[i+1] - arr[i] == 1) { count++; } else { if(count > max) { max = count; } count = 1; } } if(count > max) { max = count; } System.out.println(max); 💡 Problem 5: Print Longest Consecutive Subarray int count = 1, max = 0; int endIndex = 0; for(int i = 0; i < arr.length - 1; i++) { if(arr[i+1] - arr[i] == 1) { count++; } else { if(count > max) { max = count; endIndex = i; } count = 1; } } if(count > max) { max = count; endIndex = arr.length - 1; } int startIndex = endIndex - max + 1; for(int i = startIndex; i <= endIndex; i++) { System.out.print(arr[i] + " "); } 🧠 Big takeaway from today: Patterns > Memorization Logic > Loops Thinking > Typing 🔥 Next level thinking: What if array doesn’t start from 1? What if we need longest increasing subarray? What about prime-only subarrays? That’s where real DSA begins. #DSA #Java #CodingJourney #ProblemSolving #Placements #LearningInPublic #CodingLogic #DataStructures #ProblemSolving #InterviewPrep #TAPAcademy #Upskilling #Java #Logics #Arrays #Traversal #Learning #Upskilling #Programming
To view or add a comment, sign in
-
-
Day 23 | Programming Classes at TAP Academy 🚨 Mastering String Problems: Logic Over Syntax You’re given a string: 👉 "he#l@lo123" Task? Remove only special characters. Sounds basic But this is where logic—not syntax—gets tested. 💥 The Real Trap Most of us write: if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') { result += ch; } Looks correct. But this removes numbers too ❌ 👉 Problem: remove only special characters 👉 Your code: removes everything except alphabets That’s not solving. That’s assuming. 🧠 Core Concept Every character falls into 3 buckets: Alphabet Numeric Special character So don’t try to detect special characters (it’s messy across Unicode ❌) 👉 Instead, keep what you know is valid if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) { result += ch; } 🔍 Underlying Logic Pattern Strings are immutable. So the real workflow is: ➡️ Traverse (for loop) ➡️ Extract (charAt(i)) ➡️ Validate (conditions) ➡️ Build new string String result = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (Character.isLetterOrDigit(ch)) { result += ch; } } ⚡ Level Up: Rearranging the String New problem: 👉 Uppercase -> Lowercase -> Numbers (remove specials) Two approaches: Approach 1 (naive): 3 loops -> O(3n) Approach 2 (optimized): 1 loop + 3 strings -> O(n) String uc="", lc="", nc=""; for(char ch : s.toCharArray()){ if(ch >= 'A' && ch <= 'Z') uc += ch; else if(ch >= 'a' && ch <= 'z') lc += ch; else if(ch >= '0' && ch <= '9') nc += ch; } return uc + lc + nc; 👉 Same result. Better thinking. 💣 The Dangerous Bug (ASCII Trap) You try to sum numbers: sum += ch; Output? ❌ 155 instead of 11 Why? 👉 '2' = 50, '4' = 52, '5' = 53 👉 You added ASCII, not actual numbers 🔥 Fix: sum += (ch - '0'); 👉 Converts char -> actual digit ⚠️ Operator Precedence Trap This looks harmless: result += ch + 32; But gives ❌ "H32E32..." Why? 👉 String + char + int -> left to right evaluation 🔥 Fix: result += (char)(ch + 32); 🔄 Case Conversion Logic (No Inbuilt Methods) Key insight: 👉 'A' → 'a' difference = 32 So: Upper → Lower → +32 Lower → Upper → -32 if(ch >= 'A' && ch <= 'Z'){ result += (char)(ch + 32); }else{ result += ch; } 🔁 Swap Case Logic if(ch >= 'A' && ch <= 'Z'){ result += (char)(ch + 32); } else if(ch >= 'a' && ch <= 'z'){ result += (char)(ch - 32); } else{ result += ch; } 👉 Clean. No shortcuts. Pure logic. #Java #CodingInterview #ProblemSolving #Developers #LearnToCode #Programming #TechCareers #CodeSmart #CoreJava #Strings #Logics #Thinking #Coding #Upskilling #Problems #Syntax #DSA #TAPAcademy #Software #Development #Learning
To view or add a comment, sign in
-
-
I’ve started writing to improve how I understand and explain concepts. My first blog focuses on a fundamental topic in programming—recursion vs iteration—and how they represent two different ways of thinking while solving problems. This is just the beginning. I’ll be writing on a mix of technical topics and general ideas going forward. Read here: https://lnkd.in/gw98ph77 #Programming #Learning #Algorithms #StudentJourney
To view or add a comment, sign in
-
I Thought I Knew OOP... Until I Tried Explaining Encapsulation in One Sentence! 🚨 Ever had that moment where you think you know something… and then you realize you don’t? That was me with **Encapsulation** in Object-Oriented Programming. I thought I had it down. But when challenged to explain it in a single sentence, my mind went blank! 🤯 Encapsulation isn’t just about keeping data safe. It’s about bundling the data and methods that operate on it, hiding the internal state to protect it from outside interference. Simple, right? But putting that in one concise sentence? A whole different game! Have you ever stumbled on a seemingly simple concept? What’s your experience with explaining complex ideas? 🤔 #OOP #Encapsulation #SoftwareDevelopment #TechTalk #Programming
To view or add a comment, sign in
-
Understanding Event Handling in Programming: How Apps Respond to Users Many beginners think events are small UI features. In reality, events are the mechanism that makes software respond to users....
To view or add a comment, sign in
-
Unravel the secrets of Dynamic Programming! Feeling stuck on complex coding problems? Our latest blog, "Dynamic Programming Demystified", is here to simplify it for you! Dive deep into overlapping subproblems, optimal substructure, and master techniques like memoization and tabulation. Turn confusion into confidence! https://lnkd.in/dfMRsz9B #DynamicProgramming #DP #CodingProblems #BrainBusters #TechEducation
To view or add a comment, sign in
-
💻 Coding is not just about writing code… It’s about creating something from nothing. From simple logic ➝ to complex patterns From lines of code ➝ to real outputs Whether it’s: • Generating patterns • Building visual designs • Solving logical problems Every small program improves your thinking. At first, it looks difficult. But once you understand the logic… It becomes creativity. 🚀 Code is not just syntax. It’s thinking. #Python #Coding #Programming #Developers #Learning #Tech
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development