Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

673. Number of Longest Increasing Subsequence

Medium49.0% Acceptance
ArrayDynamic ProgrammingBinary Indexed Tree
Asked by:
Amazon
ProblemSolutions (6)VideosCompanies (7)Notes

Problem Statement

Given an integer array nums, return the number of longest increasing subsequences.

Notice that the sequence has to be strictly increasing.

Example 1:

Input: nums = [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].

Example 2:

Input: nums = [2,2,2,2,2]
Output: 5
Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.

Constraints:

  • 1 <= nums.length <= 2000
  • -106 <= nums[i] <= 106
  • The answer is guaranteed to fit inside a 32-bit integer.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Z
Zoho
S
Samsung
t
tcs
A
Apollo 247
+2

Approach

The goal of #673 Number of Longest Increasing Subsequence is not only to find the length of the longest increasing subsequence (LIS) but also to count how many such subsequences exist. A common approach uses Dynamic Programming where two arrays track the length of the LIS ending at each index and the count of subsequences achieving that length. By iterating through previous elements, you update these arrays whenever a valid increasing extension is found.

This DP approach runs in O(n²) time and is often sufficient for moderate input sizes. For optimization, advanced solutions use data structures like a Binary Indexed Tree (Fenwick Tree) or Segment Tree combined with coordinate compression. These structures efficiently store pairs of (length, count) and query the best subsequence ending with smaller values. This reduces the time complexity to O(n log n) while maintaining correct counting logic.

The key idea is to track both the maximum subsequence length and the number of ways to achieve it.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic ProgrammingO(n²)O(n)
Binary Indexed Tree / Segment Tree with CompressionO(n log n)O(n)

Video Solution Available

NeetCode

View all video solutions

Solutions (6)

Dynamic Programming with Two Arrays

This approach utilizes two arrays to track the length of the longest increasing subsequence ending at each index and the count of such subsequences. The first array, lengths, will store the length of L.I.S. ending at each position, and the second array, counts, will store how many times such a subsequence appears. We iterate through each possible pair of indices to update these arrays accordingly.

Time Complexity: O(n^2) where n is the number of elements in the input array.
Space Complexity: O(n) used for the lengths and counts arrays.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4
5int findNumberOfLIS(int* nums, int numsSize)




Explanation

This C function initializes two arrays, lengths and counts, with size corresponding to the input nums. It uses nested loops to compare elements, updating these arrays based on the conditions discussed. The overall length and count logic is surrounded by careful checks to decide whether to extend, begin, or merge subsequences.

Video Solutions

Watch expert explanations and walkthroughs

Longest Increasing Subsequence - Dynamic Programming - Leetcode 300

NeetCode
18:13432,482 views

Asked By Companies

7 companies
A
Amazon
Z
Zoho
S
Samsung
t
tcs
A
Apollo 247
G
Google
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Trapping Rain WaterHard
Jump Game IIMedium
Maximum SubarrayMedium
Jump GameMedium
More similar problems

Related Topics

ArrayDynamic ProgrammingBinary Indexed TreeSegment Tree

Problem Stats

Acceptance Rate49.0%
DifficultyMedium
Companies7

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why do we track both length and count in this problem?

Tracking only the LIS length is not enough because multiple subsequences may achieve that same maximum length. Maintaining both the length and the count allows the algorithm to correctly accumulate the number of distinct longest subsequences.

Is Number of Longest Increasing Subsequence asked in FAANG interviews?

Yes, variations of LIS counting problems appear in technical interviews at large tech companies. Interviewers use it to test understanding of dynamic programming, state transitions, and optimization using advanced data structures.

What is the optimal approach for Number of Longest Increasing Subsequence?

The typical solution uses dynamic programming with two arrays to track the LIS length and the count of sequences ending at each index. For better performance, Binary Indexed Trees or Segment Trees with coordinate compression can reduce the time complexity to O(n log n).

What data structures are useful for solving Number of Longest Increasing Subsequence?

Dynamic programming arrays are the most common structure for this problem. For optimized solutions, a Fenwick Tree (Binary Indexed Tree) or Segment Tree can efficiently maintain the best subsequence length and the number of ways for smaller values.

{
6
if
(
numsSize
==
0
)
return
0
;
7
8
int
*
lengths
=
(
int
*
)
calloc
(
numsSize
,
sizeof
(
int
)
)
;
9
int
*
counts
=
(
int
*
)
calloc
(
numsSize
,
sizeof
(
int
)
)
;
10
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
11
lengths
[
i
]
=
1
;
12
counts
[
i
]
=
1
;
13
}
14
15
int
maxLen
=
0
,
result
=
0
;
16
17
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
18
for
(
int
j
=
0
;
j
<
i
;
j
++
)
{
19
if
(
nums
[
i
]
>
nums
[
j
]
)
{
20
if
(
lengths
[
j
]
+
1
>
lengths
[
i
]
)
{
21
lengths
[
i
]
=
lengths
[
j
]
+
1
;
22
counts
[
i
]
=
counts
[
j
]
;
23
}
else
if
(
lengths
[
j
]
+
1
==
lengths
[
i
]
)
{
24
counts
[
i
]
+=
counts
[
j
]
;
25
}
26
}
27
}
28
if
(
lengths
[
i
]
>
maxLen
)
{
29
maxLen
=
lengths
[
i
]
;
30
result
=
counts
[
i
]
;
31
}
else
if
(
lengths
[
i
]
==
maxLen
)
{
32
result
+=
counts
[
i
]
;
33
}
34
}
35
36
free
(
lengths
)
;
37
free
(
counts
)
;
38
return
result
;
39
}
40
41
int
main
(
)
{
42
int
nums
[
]
=
{
1
,
3
,
5
,
4
,
7
}
;
43
int
numsSize
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
44
printf
(
"%d"
,
findNumberOfLIS
(
nums
,
numsSize
)
)
;
45
return
0
;
46
}