Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Arithmetic Slices II – Subsequence problem solution

YASH PAL, 31 July 2024

In this Leetcode Arithmetic Slices II – Subsequence problem solution you have given an integer array nums, return the number of all the arithmetic subsequences of nums.

A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

  1. For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences.
  2. For example, [1, 1, 2, 5, 7] is not an arithmetic sequence.

A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.

  1. For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10].

The test cases are generated so that the answer fits in a 32-bit integer.

Leetcode Arithmetic Slices II - Subsequence problem solution

Problem solution in Python.

class Solution:
    def numberOfArithmeticSlices(self, A: List[int]) -> int:
        dic={}
        for i in range(len(A)):
            dic[i]={}
        ans=0
        for i in range(len(A)):
            for j in range(i):
                d=A[i]-A[j]
                if d not in dic[j]:
                    if d in dic[i]:
                        dic[i][d]+=1
                    else:
                        dic[i][d]=1
                else:
                    if d not in dic[i]:
                        dic[i][d]=dic[j][d]+1
                        ans+=dic[j][d]
                    else:
                        dic[i][d]+=dic[j][d]+1
                        ans+=dic[j][d]
        return ans

Problem solution in Java.

public int numberOfArithmeticSlices(int[] A) {
        int l = A.length;
        if(l <3){
            return 0;
        int[] left = new int[l];
        int[] right = new int[l];
        left[0] = 0;
        right[l-1] = 0;
        for(int i=1;i<l;i++){
            left[i] = A[i] - A[i-1];
        }
        
        for(int i=l-2;i>=0;i--){
            right[i] =  A[i+1] - A[i];
        }

        int prev = 0;
        int[] result = new int[l];
        for(int i=1;i<l-1;i++){
            if(right[i] == left[i]){
                result[i] = 1;
                    if(right[i-1] == left[i-1]){
                        result[i] = result[i]+result[i-1];
                    }
            }
        }
        
        int sum=0;
        for(int i=0;i<l;i++){
            sum+=result[i];
        }
        
        return sum;
    }

Problem solution in C++.

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& nums) {
        int ans = 0, n = size(nums); 
        vector<unordered_map<int, int>> freq(n); 
        for (size_t i = 0; i < n; ++i) {
            for (size_t ii = 0; ii < i; ++ii) {
                long temp = (long) nums[i] - nums[ii]; 
                if (temp > INT_MAX || temp < INT_MIN) continue;
                int diff = nums[i] - nums[ii]; 
                ans += freq[ii][diff]; 
                freq[i][diff] += 1 + freq[ii][diff]; 
            }
        }
        return ans; 
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes