Java solution for numerically balanced numbers challenge

🚀 Day 76: Numerically Balanced Brute Force – Java Edition Today’s challenge was deceptively simple yet oddly satisfying: 🔢 Find the next numerically balanced number greater than a given integer. A number is numerically balanced if every digit d appears exactly d times. Examples: 22 → two 2s ✅ 1333 → one 1, three 3s ✅ 122 → one 1, two 2s ❌ (2 appears only once) I went full brute-force in Java, and it worked like a charm: java public int nextBeautifulNumber(int n) { for (int i = n + 1; i <= 1224444; i++) { if (isBalanced(i)) return i; } return -1; } private boolean isBalanced(int num) { int[] count = new int[10]; int temp = num; while (temp > 0) { count[temp % 10]++; temp /= 10; } for (int i = 0; i < 10; i++) { if (count[i] != 0 && count[i] != i) return false; } return true; } 🧠 Clean logic, no fancy tricks. Just a solid loop and a digit frequency check. 📌 Lesson: Sometimes brute force is enough—if you know your bounds and keep it clean. Let me know if you’ve tackled this one differently or optimized it further. #100DaysOfCode #Java #LeetCode #ProblemSolving #NumericallyBalanced #CodingJourney #Day76

  • graphical user interface, website

To view or add a comment, sign in

Explore content categories