Number of substrings divisible by 4 in a string of integers

Given a string consisting of integers 0 to 9.The task is to count the number of substrings which when convert into integer are divisible by 4. Substring may contain leading zeroes.

Examples:

Input : "124"
Output : 4
Substrings divisible by 4 are  "12", "4", "24", "124" .

Input : "04"
Output : 3
Substring divisible by 4 are "0", "4", "04" .



Method 1: (Brute Force) The idea is to find all the substrings of the given string and check if substring is divisible by 4 or not.
Time Complexity: O(n^2 ).

Efficient solution : A number is divisible by 4 if its last two digits are divisible by 4 and single digit numbers divisible by 4 are 4, 8 and 0 . So, to calculate number of substrings divisible by 4 we first count number of 0’s, 4’s and 8’s in the string .Then, we make all pairs of two consecutive characters and convert it into integer. After converting it into integer we check that whether it is divisible by 4 or not . If it is divisible by 4 then all such substring ending with this last two charcters are divisible by 4 . Now, number of such substrings are basically the index of 1st character of pair. To make it more clear, consider string “14532465” then possible pairs are “14”, “45”, “53”, “32”, “24”, “46”, “65” . In these pairs only “32” and “24” when converted into integer are divisible by 4 . Then, substrings ( length >= 2 ) divisible by 4 must end with either “32” or “24” So, number of substrings ending with “32” are “14532”, “4532”, “532”, “32” i.e 4 and index of ‘3’ is also 4 . Similarly, number of substrings ending with “24” are 5 .

Thus we get an O(n) solution. Below is the implementation of this approach .

C/C++

// C++ program to count number of substrings
// divisible by 4.
#include <bits/stdc++.h>
using namespace std;
  
int countDivisbleby4(char s[])
{
    int n = strlen(s);
      
    // In the first loop we will count number of 
    // 0's, 4's and 8's present in the string
    int count = 0;
    for (int i = 0; i < n; ++i) 
       if (s[i] == '4' || s[i] == '8' || s[i] == '0')
            count++ ;
      
    // In second loop we will convert pairs
    // of two consecutive characters into
    // integer and store it in variable h .
    // Then we check whether h is divisible by 4
    // or not . If h is divisible we increases
    // the count with ( i + 1 ) as index of
    // first character of pair
    for (int i = 0; i < n - 1; ++i) {
       int h = ( s[i] - '0' ) * 10 + ( s[i+1] - '0' ); 
       if (h % 4 == 0)  
           count = count + i + 1 ;
    }
  
    return count;
}
  
// Driver code to test above function
int main()
{
    char s[] = "124";
    cout << countDivisbleby4(s);
    return 0;
}

Java

// Java program to count number of substrings
// divisible by 4
import java.io.*;
  
class GFG 
{
    // Function to count number of substrings
    // divisible by 4
    static int countDivisbleby4(String s)
    {
        int n = s.length();
       
        // In the first loop we will count number of 
        // 0's, 4's and 8's present in the string
        int count = 0;
        for (int i = 0; i < n; ++i) 
            if (s.charAt(i) == '4' || s.charAt(i) == '8' || s.charAt(i) == '0')
                count++ ;
       
        // In second loop we will convert pairs
        // of two consecutive characters into
        // integer and store it in variable h .
        // Then we check whether h is divisible by 4
        // or not . If h is divisible we increases
        // the count with ( i + 1 ) as index of
        // first character of pair
        for (int i = 0; i < n - 1; ++i) 
        {
            int h = ( s.charAt(i) - '0' ) * 10 + ( s.charAt(i+1) - '0' ); 
            if (h % 4 == 0)  
                count = count + i + 1 ;
        }
   
        return count;
    }
      
    // driver program
    public static void main (String[] args) 
    {
        String s = "124";
        System.out.println(countDivisbleby4(s));
    }
}
  
// Contributed by Pramod Kumar

C#

// C# program to count number of 
// substrings divisible by 4
using System;
      
public class GFG 
      
    // Function to count number of 
    // substrings divisible by 4
    static int countDivisbleby4(string s)
    {
        int n = s.Length;
      
        // In the first loop we will count 
        // number of 0's, 4's and 8's present  
        // in the string
        int count = 0;
        for (int i = 0; i < n; ++i) 
          
            if (s[i] == '4' || s[i] == '8'
                            || s[i] == '0')
                count++ ;
      
        // In second loop we will convert pairs
        // of two consecutive characters into
        // integer and store it in variable h .
        // Then we check whether h is divisible 
        // by 4 or not . If h is divisible, we
        // increases the count with ( i + 1 )
        // as index of first character of pair
        for (int i = 0; i < n - 1; ++i) 
        {
            int h = (s[i] - '0' ) * 10 + 
                    (s[i + 1] - '0' ); 
            if (h % 4 == 0) 
                count = count + i + 1 ;
        }
  
        return count;
    }
      
    // Driver Code
    public static void Main () 
    {
        string s = "124";
        Console.WriteLine(countDivisbleby4(s));
    }
}
  
// This code is contributed by Sam007

PHP

<?php
// PHP program to count number 
// of substrings divisible by 4.
  
function countDivisbleby4( $s)
{
    $n = strlen($s);
      
    // In the first loop we
    // will count number of 
    // 0's, 4's and 8's present
    // in the string
    $count = 0;
    for($i = 0; $i < $n; ++$i
    if ($s[$i] == '4' or $s[$i] == '8' 
                    or $s[$i] == '0')
            $count++ ;
      
    // In second loop we will convert pairs
    // of two consecutive characters into
    // integer and store it in variable h .
    // Then we check whether h is divisible by 4
    // or not . If h is divisible we increases
    // the count with ( i + 1 ) as index of
    // first character of pair
    for ( $i = 0; $i < $n - 1; ++$i
    {
        $h = ( $s[$i] - '0' ) * 10 +
                 ( $s[$i+1] - '0' ); 
        if ($h % 4 == 0) 
            $count = $count + $i + 1 ;
    }
  
    return $count;
}
  
    // Driver Code
    $s = "124";
    echo countDivisbleby4($s);
      
// This code is contributed by anuj_67.
?>


Output:

4

This article is contributed by Surya Priy. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007, vt_m



Article Tags :
Practice Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.