Knowee
Questions
Features
Study Tools

Given an array A of N integers and an integer K, write a program to print the number of subarrays in A whose sum is divisible by K.

Question

Given an array A of N integers and an integer K, write a program to print the number of subarrays in A whose sum is divisible by K.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step approach to solve the problem:

  1. Initialize a variable count to 0, which will keep track of the number of subarrays whose sum is divisible by K.

  2. Create a dictionary remainder_count to store the count of remainders obtained when the cumulative sum of elements in the array is divided by K. Initialize remainder_count with a single key-value pair: (0, 1), as the cumulative sum of an empty subarray is 0.

  3. Initialize a variable cumulative_sum to 0, which will store the cumulative sum of elements in the array.

  4. Iterate through each element num in the array A.

  5. Update the cumulative_sum by adding num to it.

  6. Calculate the remainder remainder when cumulative_sum is divided by K.

  7. If remainder is negative, add K to it to make it positive. This is done to handle negative remainders.

  8. If remainder exists in remainder_count, increment count by the value associated with remainder in remainder_count.

  9. If remainder does not exist in remainder_count, add it to remainder_count with a value of 1.

  10. Increment the value associated with remainder in remainder_count by 1.

  11. After iterating through all the elements in the array, print the value of count, which represents the number of subarrays whose sum is divisible by K.

Here is a sample implementation in Python:

def count_subarrays(A, K):
    count = 0
    remainder_count = {0: 1}
    cumulative_sum = 0

    for num in A:
        cumulative_sum += num
        remainder = cumulative_sum % K

        if remainder < 0:
            remainder += K

        if remainder in remainder_count:
            count += remainder_count[remainder]

        remainder_count[remainder] = remainder_count.get(remainder, 0) + 1

    return count

# Example usage
A = [4, 5, 0, -2, -3, 1]
K = 5
result = count_subarrays(A, K)
print(result)  # Output: 7

This implementation has a time complexity of O(N), where N is the length of the array A.

This problem has been solved

Similar Questions

You are given an array of integers of size N and another integer K. Find the size of the largest subset of the array in which the sum of any two numbers is not perfectly divisible by K.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array and K, separated by a space. The second line contains the elements of the array.Output FormatFor each test case, print the size of the largest subset in which the sum of any two numbers is not perfectly divisible by K, separated by a newline.Constraints10 points1 <= T <= 1001 <= N <= 101 <= K <= 500 <= arr[i] <= 10220 points1 <= T <= 1001 <= N <= 1021 <= K <= 1020 <= arr[i] <= 10570 points1 <= T <= 1001 <= N <= 1041 <= K <= 1050 <= arr[i] <= 105ExampleInput24 31 7 2 4 5 21 2 3 4 5Output32ExplanationTest Case 1:The subset {1, 7, 4} is the largest subset with the sum of any two numbers { {1,7}=8, {1,4}=5, {7,4}=11 } not perfectly divisible by K=3.Test Case 2:The subset {1, 2} or {2, 3} or {3, 4} or {4, 5} can be the largest subset with the sum of any two numbers not perfectly divisible by K=2.

Write a program that takes an array of integers as input and prints all its subarrays, ensuring that they are sorted based on the forward direction and the count of elements.Constraints:The length of the array is at least 1 and at most 100.Each element of the array is an integer between -1000 and 1000.Input:The input consists of two lines. The first line contains an integer, n, which is the length of the array. The second line contains n space-separated integers, representing the elements of the array.Output:The program should print all the subarrays of the given array in increasing order, one subarray per line.

ou are given an array of integers of length n. Determine the number of pairs of adjacent elements whose sum is divisible by a given integer k. If such pairs exist, output the count of pairs; otherwise, indicate that no pairs were found.Example 1Input:3 // number of elements (n)10 2 30 // elements2 // kOutput: Number of pairs: 2Explanation: In the given array [10, 2, 30], the pairs with sums divisible by 2 are (10, 2) and (2, 30). Hence, the output is 2, indicating the number of such pairs found.Input format :The first line consists of an integer n, representing the size of the array.The second line consists of n space-separated integers, representing the elements of the array.The third line consists of an integer k, representing the divisor.Output format :If there exist pairs of adjacent elements in the array whose sum is divisible by k, output "Number of pairs: " followed by the count of such pairs.If no such pairs are found, output "No pairs found".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:3 ≤ n ≤ 251 ≤ Each element ≤ 1002 ≤ k ≤ 25Sample test cases :Input 1 :310 2 302Output 1 :Number of pairs: 2Input 2 :610 20 30 40 50 6010

def divisibleSumPairs(n, k, ar): count1=0 for i in range(n): for j in range(i+1, n): if ar[i]+ar[j] % k == 0: count1+=1 return count1

Given an array of integers A, print true if we can partition the array into three non-empty subarrays with equal sums.Input FormatThe first line of the input contains an integer N. Second line of input contains an array of size N.Output FormatPrint true if we can partition the array, otherwise false.Constraints3 ≤ N ≤ 104-104 ≤ Ai ≤ 104ExampleInput103 3 6 5 -2 2 5 1 -9 4OutputtrueExplanation(3 + 3) = (6) = (5 - 2 + 2 + 5 + 1 - 9 + 4) = 6.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.