Codeforces Round 653 (Div. 3) 1374D - Zero Remainder Array Solution



 Problem Link  :  https://codeforces.com/problemset/problem/1374/D

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. using ll = long long ;
  10. int main() {
  11. ios_base::sync_with_stdio(0);
  12. cin.tie(0);
  13. ll t ;
  14. cin >> t ;
  15. while ( t-- ) {
  16. ll n , k ;
  17. cin >> n >> k ;
  18. ll a[n+9] , b[n+9] ;
  19. for ( ll i = 1 ; i <= n ; i++ ) {
  20. cin >> a[i] ;
  21. a[i] = a[i] % k ;
  22. if ( a[i] != 0 ) a[i] = k - a[i] ;
  23. }
  24. sort( a + 1 , a + n + 1 ) ;
  25. ll ans ;
  26. ans = a[1] , b[1] = a[1] ;
  27. for ( ll i = 2 ; i <= n ; i++ ) {
  28. if (a[i] == 0 ) continue ;
  29. if ( a[i-1] == a[i] ) b[i] = b[i-1] + k ;
  30. else b[i] = a[i] ;
  31. ans = max ( ans , b[i]) ;
  32. }
  33. if ( ans != 0 ) cout << ans + 1 << endl ;
  34. else cout << ans << endl;
  35. }
  36. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.