Binary TreesBinary Trees36

  1. 1Preorder Traversal of a Binary Tree using Recursion
  2. 2Preorder Traversal of a Binary Tree using Iteration
  3. 3Postorder Traversal of a Binary Tree Using Recursion
  4. 4Postorder Traversal of a Binary Tree using Iteration
  5. 5Level Order Traversal of a Binary Tree using Recursion
  6. 6Level Order Traversal of a Binary Tree using Iteration
  7. 7Reverse Level Order Traversal of a Binary Tree using Iteration
  8. 8Reverse Level Order Traversal of a Binary Tree using Recursion
  9. 9Find Height of a Binary Tree
  10. 10Find Diameter of a Binary Tree
  11. 11Find Mirror of a Binary Tree - Todo
  12. 12Inorder Traversal of a Binary Tree using Recursion
  13. 13Inorder Traversal of a Binary Tree using Iteration
  14. 14Left View of a Binary Tree
  15. 15Right View of a Binary Tree
  16. 16Top View of a Binary Tree
  17. 17Bottom View of a Binary Tree
  18. 18Zigzag Traversal of a Binary Tree
  19. 19Check if a Binary Tree is Balanced
  20. 20Diagonal Traversal of a Binary Tree
  21. 21Boundary Traversal of a Binary Tree
  22. 22Construct a Binary Tree from a String with Bracket Representation
  23. 23Convert a Binary Tree into a Doubly Linked List
  24. 24Convert a Binary Tree into a Sum Tree
  25. 25Find Minimum Swaps Required to Convert a Binary Tree into a BST
  26. 26Check if a Binary Tree is a Sum Tree
  27. 27Check if All Leaf Nodes are at the Same Level in a Binary Tree
  28. 28Lowest Common Ancestor (LCA) in a Binary Tree
  29. 29Solve the Tree Isomorphism Problem
  30. 30Check if a Binary Tree Contains Duplicate Subtrees of Size 2 or More
  31. 31Check if Two Binary Trees are Mirror Images
  32. 32Calculate the Sum of Nodes on the Longest Path from Root to Leaf in a Binary Tree
  33. 33Print All Paths in a Binary Tree with a Given Sum
  34. 34Find the Distance Between Two Nodes in a Binary Tree
  35. 35Find the kth Ancestor of a Node in a Binary Tree
  36. 36Find All Duplicate Subtrees in a Binary Tree

Largest Odd Number in Numeric String



Problem Statement

Given a numeric string (i.e., a string that contains only digits), your task is to extract the largest-valued odd number that can be formed by removing some digits from the end of the string.

Remember, a number is odd if it ends with 1, 3, 5, 7, or 9.

Examples

InputOutputDescription
3542735427Entire number is odd; last digit is 7 (odd)
4206""No odd digits in the string
12540125Last odd digit is 5 at index 2; return prefix up to index 2
8080""All digits are even; return empty string
10011001Last digit is 1 (odd); entire string is valid
10001Last odd digit is 1 at index 0
11Single-digit odd number
4""Single-digit even number
""""Empty string input returns empty string

Solution

To solve this problem, we need to find the largest odd number that exists as a prefix of the given numeric string. Let's break it down in simple terms.

We are only allowed to remove digits from the end of the string. That means, we can keep shortening the number from the right side until we find a version that ends with an odd digit.

Case 1: Last digit is odd

If the last digit of the string is odd (like 1, 3, 5, 7, or 9), then the entire string is already an odd number. We simply return the whole string.

Case 2: Last digit is even, but there is an odd digit earlier

If the string ends in an even digit (like 0, 2, 4, 6, 8), we move leftward to find the last occurrence of an odd digit. The moment we find it, we return the substring from the beginning up to that digit (inclusive). This is the largest odd number that can be formed as a prefix.

Case 3: No odd digit at all

If the string doesn't contain any odd digits, there is no way to form an odd number. In this case, we return an empty string "".

Case 4: Edge case - Empty string

If the input is an empty string, we also return "" since there's nothing to check.

This approach is simple, efficient, and beginner-friendly because we are just scanning the string from the end until we find what we need. There’s no need to convert anything to an actual number or use complex operations.

Visualization

Algorithm Steps

  1. Start from the end of the string and move backward.
  2. Check each character:
  3. → If it's an odd digit (1, 3, 5, 7, 9), take the substring from the beginning to that character (inclusive).
  4. → Return this substring immediately as it's the largest odd number possible.
  5. If no odd digit is found, return an empty string "".

Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
def largest_odd_number(num):
    for i in range(len(num) - 1, -1, -1):
        if int(num[i]) % 2 == 1:
            return num[:i + 1]  # Return the substring ending at the last odd digit
    return ""  # No odd digit found

# Sample Input
print(largest_odd_number("4206"))  # Output: ""

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(1)If the last character is odd, we return immediately.
Average CaseO(n)In the average case, we might scan most of the string to find the last odd digit.
Average CaseO(n)If no odd digit is present, the entire string is scanned once.

Space Complexity

O(1)

Explanation: We use a constant number of variables, and only return a substring from the original input.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M