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. Sample input3 2 3 3 4Sample output3
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. Sample input3 2 3 3 4Sample output3
Solution
Sure, here is a step-by-step solution to the problem:
- Read the value of N from the input. This represents the number of elements in the array A.
- Read the value of K from the input. This represents the integer by which the sum of subarrays should be divisible.
- Read the elements of the array A from the input and store them in a list or array.
- Initialize a variable count to 0. This variable will keep track of the number of subarrays whose sum is divisible by K.
- Initialize a dictionary or array called prefix_sum with a single key-value pair: (0, 1). This will be used to store the prefix sums and their frequencies.
- Initialize a variable curr_sum to 0. This variable will keep track of the current prefix sum.
- Iterate over each element in the array A. a. Add the current element to curr_sum. b. Calculate the remainder of curr_sum divided by K and store it in a variable called mod. c. If mod is negative, add K to it to make it positive. d. If mod exists as a key in the prefix_sum dictionary or array, increment count by the value associated with mod in prefix_sum. e. If mod does not exist as a key in the prefix_sum dictionary or array, add it as a new key with a value of 1. f. Increment the value associated with mod in the prefix_sum dictionary or array by 1.
- Print the value of count as the output.
Here is the solution in Python:
N, K = map(int, input().split())
A = list(map(int, input().split()))
count = 0
prefix_sum = {0: 1}
curr_sum = 0
for num in A:
curr_sum += num
mod = curr_sum % K
if mod < 0:
mod += K
if mod in prefix_sum:
count += prefix_sum[mod]
if mod in prefix_sum:
prefix_sum[mod] += 1
else:
prefix_sum[mod] = 1
print(count)
This program takes the input values N, K, and the array A, and calculates the number of subarrays whose sum is divisible by K. The output is then printed as the final result.
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.
Implement a Java program to Find the subarray of size 3 with the greatest sum.Input FormatThe input consists of a single line where the user is prompted to enter 30 integers, separated by spaces.ConstraintsThe array contains exactly 30 integers.Output FormatThe output consists of two lines: The first line contains the subarray with the greatest sum separated by spaces. The second line contains the sum of the subarray.Sample Input 010 20 30 40 50 60 70 80 90 100 11 22 33 44 55 66 77 88 99 9 8 7 6 5 4 3 2 1 99 88Sample Output 0Subarray with the Greatest Sum: 80 90 100Sum of the Subarray: 270Contest ends in an hourSubmissions: 0Max Score: 10Difficulty: MediumRate This Challenge: More Java 151import java.io.*;2import java.util.*;34public class Solution {56 public static void main(String[] args) {7 /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8 }9}
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.
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
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.