Friday, July 11, 2025

Leetcode 2894. Divisible and Non-divisible Sums Difference

 

Problem Statement:

 leetcode problem 2894. Divisible and Non-divisible Sums Difference


You are given positive integers n and m.

Define two integers as follows:num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.
num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.

Return the integer num1 - num2.


Initial Code

class Solution {
    public int differenceOfSums(int n, int m) {
        int suma = 0;sumb=0; // ERROR: sumb not declared properly
        for (int i=1; i==n; i++){ // ERROR: i==n (this never iterates)
            if(i%m==0){
                sumb+=i;
            }else{
                suma+=i;
            }
        }
        return suma-sumb;
    }
}


Issues in the code:

  • Variable declaration: sumb is undeclared.
  • Loop condition: for (int i = 1; i == n; i++) executes only when i equals n, so it never loops. It must be i <= n.
  • Minor style: combining declarations on one line makes it harder to read

Corrected code

class Solution {
    public int differenceOfSums(int n, int m) {
        int suma = 0;
        int sumb = 0;
        for (int i = 1; i <= n; i++) {
            if (i % m == 0) sumb += i;
            else suma += i;
        }
        return suma - sumb;
    }
}

Leetcode 3110 - Score of a string problem analysis

 Hi Everyone, This was the first attempt answer for the Leetcode problem on score of a string. 

https://leetcode.com/problems/score-of-a-string

class Solution {
    public int scoreOfString(String s) {
        sum = 0;
        char[] ssize = s.toCharArray();
        for (int i=0;i<ssize.length-1; i++){
            sum = sum + abs(ssize(i).asciicode-ssize(i+1).asciicode)
        }
            return sum
        }

Issues with the code - 

Issue Description
sum not declared   - sum is used without type declaration.
Incorrect syntax: abs() - Java requires Math.abs() for absolute value.
Incorrect character access: ssize(i) - Use ssize[i], not parentheses.
Non-existent property: .asciicode Characters in Java already act as ASCII values when cast to int.
Missing semicolons and braces Several semicolons and } missing.
Overall formatting and structure Needs improvement for clarity and compilation.


Learning from the code: 

1. Declare the variables. 
2. For each Math method, use Math.<method>
3. Array access using [].
4. asciiCode. each character has a ascii value, which can be used while converting to int.
5. colons are missing


Corrected code for this 


class Solution {
    public int scoreOfString(String s) {
        int sum = 0;
        char[] ssize = s.toCharArray();
        for (int i = 0; i < ssize.length - 1; i++) {
            sum += Math.abs(ssize[i] - ssize[i + 1]);
        }
        return sum;
    }
}

Sunday, March 2, 2025

Collections - Working With Stream




What is a Java Stream?

A Stream is a sequence of elements supporting functional-style operations like filtering, mapping, and reducing.
Streams do not modify the original data structure.
They operate lazily, meaning operations are executed only when a terminal operation is applied.
Can be sequential or parallel


Stream Pipeline Structure

A Java Stream Pipeline consists of:

Source - Where the stream comes from (e.g., a List, Set, or Array)
Intermediate Operations - Process and transform elements
Terminal Operation - Produces the final result, closing the stream.


Example of Stream PipelineList<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); List<Integer> result = numbers.stream() // Step 1: Create Stream .filter(n -> n % 2 == 0) // Step 2: Intermediate Operation .map(n -> n * n) // Step 3: Intermediate Operation .collect(Collectors.toList()); // Step 4: Terminal Operation System.out.println(result);




Flow Diagram to Understand Filters

Understanding Streams
Stream Sources

data structure from which the stream originates. This could be a collection, array, file, or range.List<String> list = Arrays.asList("Apple", "Banana", "Cherry"); Stream<String> stream1 = list.stream(); ---------------------------------------------------------------------- String[] array = {"Red", "Green", "Blue"}; Stream<String> stream2 = Arrays.stream(array); ---------------------------------------------------------------------- Stream<String> stream3 = Stream.of("One", "Two", "Three"); ----------------------------------------------------------------------

Intermediate Operation -

Intermediate operations transform a stream without consuming it.

They return another Stream, allowing further operations to be chained. Stream<Integer> filtered = numbers.stream().filter(n -> n > 5); ----------------------------------------------------------------------- Stream<Integer> squared = numbers.stream().map(n -> n * n); ----------------------------------------------------------------------- Stream<String> sorted = list.stream().sorted(); ----------------------------------------------------------------------- Stream<String> sortedDesc = list.stream().sorted(Comparator.reverseOrder()); ----------------------------------------------------------------------- Stream<Integer> uniqueNumbers = numbers.stream().distinct(); ----------------------------------------------------------------------- Stream<String> limited = list.stream().limit(2); ----------------------------------------------------------------------- Stream<String> skipped = list.stream().skip(2); ----------------------------------------------------------------------- List<List<Integer>> listOfLists = Arrays.asList( Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6) ); List<Integer> flattened = listOfLists.stream() .flatMap(List::stream) .collect(Collectors.toList()); // Output: [1, 2, 3, 4, 5, 6] ------------------------------------------------------------------------

Terminal Operation -

Terminal expressions (or terminal operations) consume the stream and produce a result or side effect. Unlike intermediate operations (which return a stream for further processing), terminal operations close the stream, meaning it cannot be used again.

✅ Consumes the stream – Once a terminal operation is applied, the stream cannot be reused.

✅ Returns a result – Can return a single value, a collection, or perform an action like printing.

✅ Forces execution – Since streams are lazy, terminal operations trigger actual processing.List<String> collected = list.stream().collect(Collectors.toList()); ---------------------------------------------------------------------------------- Set<String> collectedSet = list.stream().collect(Collectors.toSet()); ---------------------------------------------------------------------------------- Map<String, Integer> collectedMap = list.stream().collect(Collectors.toMap(s -> s, String::length)); ---------------------------------------------------------------------------------- list.stream().forEach(System.out::println); ---------------------------------------------------------------------------------- long count = list.stream().filter(s -> s.startsWith("A")).count(); ---------------------------------------------------------------------------------- Optional<String> first = list.stream().findFirst(); Optional<String> any = list.stream().findAny(); ---------------------------------------------------------------------------------- boolean anyMatch = list.stream().anyMatch(s -> s.contains("e")); boolean allMatch = list.stream().allMatch(s -> s.length() > 3); boolean noneMatch = list.stream().noneMatch(s -> s.isEmpty()); --------------------------------------------------------------------------------- Optional<Integer> sum = numbers.stream().reduce((a, b) -> a + b); ---------------------------------------------------------------------------------- int sum = numbers.stream().reduce(0, Integer::sum); ---------------------------------------------------------------------------------- Optional<Integer> min = numbers.stream().min(Integer::compare); Optional<Integer> max = numbers.stream().max(Integer::compare); ----------------------------------------------------------------------------------

Common Implementation

filter() for condition-based filtering.
map() to transform elements.
reduce() for aggregation (sum, max, min).
groupingBy() to classify data.
partitioningBy() for true/false categorization.
sorted() to order elements.
flatMap() to merge multiple collections.

List<String> uppercased = list.stream() .map(String::toUpperCase) .collect(Collectors.toList()); ---------------------------------------------------------------- List<Integer> evens = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); ----------------------------------------------------------------- // Use groupingBy() when you need multiple groups List<Employee> employees = Arrays.asList( new Employee("Alice", "HR"), new Employee("Bob", "IT"), new Employee("Charlie", "HR"), new Employee("David", "IT"), new Employee("Eve", "Finance") ); // Group employees by department Map<String, List<Employee>> groupedByDepartment = employees.stream() .collect(Collectors.groupingBy(emp -> emp.department)); System.out.println(groupedByDepartment); ------------------------------------------------------------------ // Use of partitioning to group based on condition List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30); // Partition numbers into even and odd Map<Boolean, List<Integer>> partitionedNumbers = numbers.stream() .collect(Collectors.partitioningBy(num -> num % 2 == 0)); System.out.println(partitionedNumbers); -------------------------------------------------------------------- List<Integer> numbers = Arrays.asList(10, 15, 20, 25, 30); int sum = numbers.stream() .filter(n -> n % 2 == 0) .reduce(0, Integer::sum); ---------------------------------------------------------------------- List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); String result = names.stream() .collect(Collectors.joining(", ")); ----------------------------------------------------------------------------- //Use of flatmap to create a list from list of list List<List<String>> nestedList = Arrays.asList( Arrays.asList("a", "b"), Arrays.asList("c", "d"), Arrays.asList("e", "f") ); List<String> flatList = nestedList.stream() .flatMap(List::stream) .collect(Collectors.toList());Benefits of Using Streams:


Improves Readability – Eliminates verbose loops and enhances code clarity.
Encourages Functional Programming – Uses lambda expressions for concise processing logic.
Optimized Performance – Lazy evaluation and parallel processing reduce execution time for large datasets.


Official Documentation & References

Oracle Java Streams Documentation 🔗 https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
Java Tutorials by Oracle 🔗 https://docs.oracle.com/javase/tutorial/collections/streams/index.html
Baeldung - Guide to Java Streams 🔗 https://www.baeldung.com/java-8-streams
GeeksforGeeks - Java Streams Guide 🔗 https://www.geeksforgeeks.org/stream-in-java/