-2

I tried but I am not able to correctly implement it. This is the code I wrote which is wrong as I am not counting some of the subarrays. Is it possible to write an algorithm which runs is O(n)

    int even=0 , count =0 ;
    for( int i=0 ; i<n ; ++i ) {
        if(arr[i]%4==0) {
            count = count + n - i ;
            if(even==1) { 
               count = count + n - i ; 
               --even; 
            }
        }   
        else if(a[i]%2==0) {
            ++even;
            if(even==2) {
                count = count + n - i ; 
                --even; 
            }
        }
    }

Sample I/O - if arr[] = {1,4,9} then answer should be 4 as {1,4} , {1,4,9} , {4,9} , {4} have their products divisible by 4 .

newbie
  • 5
  • 3
  • 1
    Can you please provide some sample input/output for this problem? – Manish Sundriyal Apr 05 '20 at 08:01
  • @ManishSundriyal if a[3] = {1,4,9}. answer should be 3 as {1,4} , {4} , {4,9} have their products divisible by 4. Edit - {1,4,9} is also valid – newbie Apr 05 '20 at 08:03
  • 1
    @newbie but wouldn't `{1,4,9}` itself also count as a subarray? It's product is also divisible by 4. – selbie Apr 05 '20 at 08:07
  • @newbie What is a subarray? For instance given {1,4,9} is {1,9} a subarray? Or do subarray elements have to be contiguous in the parent array? In other words, is a subarray a subset or a subsequence? – john Apr 05 '20 at 08:07
  • @selbie yeah, sorry I forgot that one. – newbie Apr 05 '20 at 08:09
  • @john subarray elements have to be contiguous in the original array. – newbie Apr 05 '20 at 08:10

3 Answers3

2

please refrain from answering the questions

its related to an ongoing problem in a constest

I hope u guys understand.

here's the contest and problem link https://www.codechef.com/APRIL20B/problems/SQRDSUB

Please abide by it as a token of appreciation for those who do solve it on there own

EDIT: NOW SINCE THE CONTEST IS OVER I AM PROVIDING AN ANSWER ON APRIL 14 2020 NOTE: The code is written in python3

def ALL_SUBS_PRO_DIV_BY_4(arr, n):
    even_ind = [i for i,it in enumerate(arr) if it%2==0]
    TOTAL_COUNT = 0

    last = -1
    while even_ind:
        ind = even_ind.pop(0)
        if arr[ind]%4==0:
            TOTAL_COUNT += (ind-last) * (n-ind)
            last = ind
        else:
            if even_ind:
                ind2 = even_ind[0]
                TOTAL_COUNT += (ind - last)*(n-ind2)
            else:
                return TOTAL_COUNT
            last = ind
    return ans            

I know a lot of you guys may need an explanation but i guess u can simply run the code and print the values to see what is happening

In any case if someone needs an explanation please leave a comment below. I might upload a google doc explaining the algo

2

This is codechef online currently going April challenge related query.as per online contest rules abd regulations. don't spoil the contest... You can check it out after april 13 3 :00 pm.

All is one
  • 11
  • 4
mr x
  • 21
  • 5
0

I am not sure if this can be solved in linear time complexity. However, the below link has a few approaches to this problem. I hope it helps.

https://www.geeksforgeeks.org/count-sub-arrays-whose-product-is-divisible-by-k/

Manish Sundriyal
  • 611
  • 6
  • 16
  • That link just gets redirected by malicious embedded ads that try to install malware. – selbie Apr 05 '20 at 08:16
  • @selbie Yes, it has ads (so?). Tries to install malware? No way. That's one of the most popular sites for algorithms & data structures. – Manish Sundriyal Apr 05 '20 at 08:18
  • The first 3x times it redirected me to a malware page. 4th time was better. – selbie Apr 05 '20 at 08:19
  • 1
    That's strange, but I am pretty confident that it's not from that site. – Manish Sundriyal Apr 05 '20 at 08:23
  • I tried this but it seems to have a higher time complexity. – newbie Apr 05 '20 at 08:28
  • 1
    From "how to answer": "Provide context for links Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Paul Hankin Apr 05 '20 at 14:34
  • yeah if k is a complicated number then the best u can do is arround O(n**2 log n). But for k=4 you can have a linear solution – Roushan Singh Apr 14 '20 at 11:48