Arithmetic Slices – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
- For example,
[1,3,5,7,9]
,[7,7,7,7]
, and[3,-1,-5,-9]
are arithmetic sequences.
Given an integer array nums
, return the number of arithmetic subarrays of nums
.
A subarray is a contiguous subsequence of the array.
Example 1:
Input: nums = [1,2,3,4] Output: 3 Explanation: We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
Example 2:
Input: nums = [1] Output: 0
Constraints:
1 <= nums.length <= 5000
-1000 <= nums[i] <= 1000
C++ Arithmetic Slices LeetCode Solution
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
// if nums size is less than 3 return false
if(nums.size() < 3)
return 0;
int cnt = 0, diff;
for(int i = 0; i<nums.size()-2; ++i)
{
// storing diff of first 2 elements
diff = nums[i+1] - nums[i];
// checking for consecutive elements with same difference.
for(int j = i+2; j<nums.size(); ++j)
{
// if we find the same diff of next 2 elements
// this means we find consecutive elements
// increase the Count
if(nums[j] - nums[j-1] == diff)
++cnt;
else
// break as we need to cnt for consecutive diff elements
break;
}
}
// return cnt
return cnt;
}
};
Java Arithmetic Slices LeetCode Solution
public int numberOfArithmeticSlices(int[] nums) {
var slices = 0;
for (int i = 2, prev = 0; i < nums.length; i++)
slices += (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2])
? ++prev
: (prev = 0);
return slices;
}
Python 3 Arithmetic Slices LeetCode Solution
class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
dp = [0] * n
ans = 0
for i in range(2, n):
if nums[i-1] - nums[i-2] == nums[i] - nums[i-1]:
dp[i] = dp[i-1] + 1
ans += dp[i]
return ans
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1