Skip to content
Programming101
Programmingoneonone

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
Programmingoneonone

Learn everything about programming

Leetcode Arithmetic Slices problem solution

YASH PAL, 31 July 2024

In this Leetcode Arithmetic Slices problem solution, 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.

Leetcode Arithmetic Slices problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

class Solution:
    def numberOfArithmeticSlices(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        if len(A) < 3:
            return 0
        diff = A[1] - A[0]
        element_count = 2
        result_count = 1
        total = 0
        for i in range(2,len(A)):
            if diff == A[i] - A[i-1]:
                element_count += 1
                if element_count >= 3:
                    total += result_count
                    result_count += 1
            else:
                diff = A[i] - A[i-1]
                element_count = 2
                result_count = 1
        return total

Problem solution in Java.

public int numberOfArithmeticSlices(int[] A) {
        int n = A.length;
        if (n < 3) return 0;
        HashMap<Integer, Integer> map = new HashMap();
        int start = A[0];
        int diff = A[1] - A[0];
        int len = 2;
        for (int i = 2; i < n; i++) {
            if (A[i] - A[i-1] == diff) {
                len++;
                map.put(start, len);
            } else {
                start = A[i-1];
                diff = A[i] - A[i-1];
                len = 2;
            }
        }
        
        int res = 0;
        for (int key: map.keySet()) {
            int length = map.get(key);
            int num_seq = length-2;
            res += (1 + num_seq) * num_seq / 2;
        }
        return res;
    }

Problem solution in C++.

class Solution {
public:
    int numberOfArithmeticSlices(vector<int>& A) {
        if(A.size()<3) return 0;
        int diff=A[1]-A[0];
        int len=2;
        int res=0;
        for(int i = 2 ; i < A.size(); ++i){
            len++;
            if(!(len>=3&&A[i]-A[i-1]==diff))
                len = 2;
            diff = A[i]-A[i-1];
            res += len -2 ;
        }
        return res;
    }
};

Problem solution in C.

int numberOfArithmeticSlices(int* A, int ASize) {
    int prev = 0, curr, ans = 0;
    for(int i = 2; i < ASize; i++){
        curr = 0;
        if(A[i] - A[i-1] == A[i-1] - A[i-2]){
            curr = prev + 1;
        }
        prev = curr;
        ans = ans + curr;
    }
    return ans;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • 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
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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes