• Skip to primary navigation
  • Skip to content
  • Skip to footer
Xuanken's Space Xuanken's Space
  • About
  • Blog
  • Coding Exercises
  • Projects
    1. Home
    2. /
    3. Exercises
    4. /
    5. Sum of First N Odd Integers
    Xuanken Tay

    Xuanken Tay

    Software engineer who likes statistics, machine learning, DevOps and the cloud

    • Sydney, AU
    • GitHub
    • LinkedIn
    • Email

    Sum of First N Odd Integers

    January 14, 2022 less than 1 minute read

    Given an integer n, return the sum of the first n positive odd integers.

    Constraints

    • n ≤ 1,000

    https://binarysearch.com/problems/Sum-of-First-N-Odd-Integers

    Examples

    Example 1

    Input

    • n = 5

    Output

    • answer = 25

    Explanation

    The first 5 odd integers are [1, 3, 5, 7, 9] and its sum is 25.

    Solution

    Tags: math

    Categories: exercises

    Updated: January 14, 2022

    Share on

    Twitter Facebook LinkedIn
    Previous Next

    Leave a comment

    Read more

    Longest Arithmetic Subsequence with Difference Constraint

    October 8, 2022 1 minute read

    Given a list of integers nums and an integer diff, return the length of the longest arithmetic subsequence where the difference between each consecutive numb...

    Airplane Seat Shuffling

    May 21, 2022 1 minute read

    You are given an integer n representing the number of seats in an airplane. The first person has lost their ticket, so they pick a random seat. Everyone else...

    Count of Sublists with Same First and Last Values

    March 22, 2022 less than 1 minute read

    Given a list of integers nums, return the number of sublists where the first element and the last element have the same value.

    Count Substrings With All 1s

    March 13, 2022 less than 1 minute read

    You are given a string s containing "1"s and "0"s. Return the number of substrings that contain only "1"s. Mod the result by 10 ** 9 + 7.

    • Follow:
    • GitHub
    • LinkedIn
    • Feed
    © 2024 Xuanken Tay. Powered by Jekyll & Minimal Mistakes.