MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

Codeforces_problems/XXXXX/solution.cpp:3–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include<bits/stdc++.h>
2using namespace std;
3int main()
4 {
5 // This problem is basically belongs to number theory category
6 // In this problem we need to basically find the subarray's size from the given array such that sum of the elements of that subarray is not divisible by given number x
7 // More precisely, sum(subarr) % x !=0.
8 // Prerequisites:- One should know about subarray ( It is basically an array made from deletion of certain elements of the array from the front and the end (may be 0) )
9 // Constraints :- The constraints of the given problem is at most 10^5 i.e., this problem can be easily solved with "int" data type
10 // Approach:- Basically what is required in this problem is to delete an element which is not divisible by given number "x"
11 // In that way , we can easily find the subarray size with sum does not divisible by the number "x"
12 // So, to solve this problem we basically iterate from the starting index and count the number of elements which are divisible by "x"
13 // Similarly we do from the end , we itereate from the end index in the reverse to the starting index and count the numbers of elements which are divisible by "x"
14 // Answer will be the minimum of both the counts + 1 ( Why +1? because we also need to delete a number which is not divisible by x in order to make,(whole sum) % x!=0.
15 // Example:- if we subtract 2 from 4 and lets say x="2" (4-2)%2==0 but if we subtract any other number which is not divisible by 2 we will get the sum not divisible by 2
16 // For instance, ((4-1)%2 !=0) (Here , "%" is modulo operator basically gives the remainder when divides a and b)
17 // Lets implement what we have discussed till now.
18
19 int t,n,i,x; // here variable "t" is for test cases , "n" is for length of the array ,i is for iterating throught the array (basically we take vector) ,x is the number for which we need to make the sum of the sequence not divisible by it
20
21 cin>>t; // Storing the value of test cases in variable "t"
22
23 while(t--)
24 {
25 cin>>n>>x; // Taking the values of "n" and "x"
26 vector<int> arr(n);
27 // Taking the the input array elements in vector named "arr"
28
29 for(i=0;i<n;i++) cin>>arr[i];
30 int sum=0; // sum variable for storing the sum of the elements the array has.
31 for(i=0;i<n;i++) sum+=arr[i];
32 if(sum%x) cout<<n<<endl; // if sum is already not divisible by "x" simply we have printed "n".
33 else
34 {
35 int cnt_s=0,cnt_e=0; // taking cnt_s for counting the numbers which are divisible by x from the starting and similarly cnt_e from the end
36 // Taking the counts as mentioned in the top , in the comments.
37 for(i=0;i<n;i++)
38 {
39 if(arr[i]%x==0) cnt_s++;
40 else break;
41 }
42 for(i=n-1;i>=0;i--)
43 {
44 if(arr[i]%x==0) cnt_e++;
45 else break;
46 }
47 int req=min(cnt_s,cnt_e)+1; // Here, +1 is for the number which is not divisible by x ( why? as I mentioned in the top)
48 if(req>=n) cout<<-1<<endl; // if req is greater or equal to n we simply need to output -1 as given in the problem
49 else cout<<n-req<<endl; // otherwise, we need to print the n- (deleted elements from the array stored in req)
50 }
51 }
52 }

Callers

nothing calls this directly

Calls 1

minFunction · 0.85

Tested by

no test coverage detected