Introduction:
After a brief pause from Java and DSA while working on other tech stacks, I’ve dedicated myself to revisiting core concepts. This blog documents my first week of solving 10 basic problems in Java, along with my learnings. Below are the questions I solved, explanations, and solutions.
Problem 1: Benjamin Bulb Problem
Question: Given n
bulbs, which bulbs will be on after all operations, where each bulb is toggled for every divisor it has?
Explanation: The bulbs that remain on are the ones toggled an odd number of times. Only bulbs at positions that are perfect squares have an odd number of divisors. So, the answer is the perfect squares less than or equal to n
.
Learning: This problem helped me understand how perfect squares have an odd number of divisors, and how that relates to the bulb toggle operation.
import java.util.Scanner;
public class Benjamin_Bulb {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
for (int i = 1; i * i <= n; i++) {
System.out.println(i * i);
}
}
}
Problem 2: Counting Digits of a Number
Question: How can we count the number of digits in a given number n
?
Explanation: We can repeatedly divide the number by 10 until the number becomes 0, counting the number of divisions. This gives the total number of digits in the number.
Learning: This simple approach to counting digits using integer division was an effective refresher on basic arithmetic operations.
import java.util.Scanner;
public class Count_digit_of_numbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int counter = 0;
while (a > 0) {
a = a / 10;
counter++;
}
System.out.println(counter);
}
}
Problem 3: Digits of a Number in Order
Question: How do you print the digits of a number in the same order?
Explanation: To print each digit in order, you first count the total number of digits, then use division to isolate each digit from the leftmost position by dividing by powers of 10.
Learning: This problem refreshed my understanding of iterative sequences and how to use simple loops to generate patterns.
import java.util.Scanner;
public class digits_of_number {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int nod = 0;
int temp = n;
while (temp != 0) {
temp = temp / 10;
nod++;
}
int div = (int)Math.pow(10, nod - 1);
while (div != 0) {
int q = n / div;
System.out.println(q);
n = n % div;
div = div / 10;
}
}
}
Problem 5: GCD and LCM
Question: How do you calculate the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of two numbers?
Explanation: The GCD can be calculated using the Euclidean algorithm, where you repeatedly take the remainder of the two numbers until one becomes 0. LCM can then be derived using the relation LCM * GCD = a * b
.
Learning: This problem helped me practice using the Euclidean algorithm and deepened my understanding of the relationship between GCD and LCM.
import java.util.Scanner;
public class GCD_AND_LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int a1 = a;
int b1 = b;
while (a % b != 0) {
int rem = a % b;
a = rem;
b = a;
}
int gcd = b;
int lcm = (a1 * b1) / gcd;
System.out.println(gcd);
System.out.println(lcm);
}
}
I'll continue this format for the remaining problems (6–10) in the blog, giving a clear question for each problem, explaining the approach, and including code solutions with key learnings.