Codeforces Round 653 (Div. 3) 1374D - Zero Remainder Array Solution
Problem Link : https://codeforces.com/problemset/problem/1374/D
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long ;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- ll t ;
- cin >> t ;
- while ( t-- ) {
- ll n , k ;
- cin >> n >> k ;
- ll a[n+9] , b[n+9] ;
- for ( ll i = 1 ; i <= n ; i++ ) {
- cin >> a[i] ;
- a[i] = a[i] % k ;
- if ( a[i] != 0 ) a[i] = k - a[i] ;
- }
- sort( a + 1 , a + n + 1 ) ;
- ll ans ;
- ans = a[1] , b[1] = a[1] ;
- for ( ll i = 2 ; i <= n ; i++ ) {
- if (a[i] == 0 ) continue ;
- if ( a[i-1] == a[i] ) b[i] = b[i-1] + k ;
- else b[i] = a[i] ;
- ans = max ( ans , b[i]) ;
- }
- if ( ans != 0 ) cout << ans + 1 << endl ;
- else cout << ans << endl;
- }
- }
No comments