Advertisement
Zeinab_Hamdy

Untitled

Jun 14th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define newl cout<<"\n";
  5. #define space cout<<" ";
  6. #define sz(x) x.size()
  7. #define NumOfDigit(w) log10(w) + 1
  8. #define MOD 1'000'000'007
  9. #define inf 2'000'000'000
  10. #define PI 3.141592654
  11. #define modulo(a, b, mod) ((((a) % (mod)) + ((b) % (mod))) % (mod))
  12. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  13. #define all(v) v.begin(), v.end()
  14. #define rall(v) v.rbegin(), v.rend()
  15. #define cin(v) for (auto&i:v) cin >> i;
  16. #define cout(v) for (auto&i:v) cout << i << " ";
  17. #define fixed(n) fixed << setprecision(n)
  18. using namespace std;  
  19. void zainab_hamdy(){
  20.   ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  21.  //freopen("collisions.in" , "r" ,stdin);
  22.   #ifndef ONLINE_JUDGE  
  23.      freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  24.  #endif  
  25.  
  26. }
  27. ll gcd(ll a, ll b){
  28.     while(b) {
  29.         a %= b;
  30.         swap(a, b);
  31.     }
  32.     return a;
  33. }
  34. int main(){
  35. zainab_hamdy();
  36. int t; cin>>t;
  37. while(t--){
  38.     int n,sum,sum1=0; cin>>n>>sum;
  39.     vector<int> v(n);
  40.     for(int i=0;i<n;i++) {
  41.         cin>>v[i];
  42.         sum1+=v[i];
  43.     }
  44.     if(sum> sum1){
  45.         cout<<"-1\n";
  46.         continue;
  47.     }
  48.     int pre[n+2]{};
  49. for(int i=1;i<n+1;i++){
  50.     pre[i]=pre[i-1]+v[i-1];
  51.    
  52. }
  53. /*
  54. for(int i=1;i<n+1;i++){
  55.     cout<< pre[i]<<" ";
  56.    
  57. }newl*/
  58. int idx=0;
  59. for(int i=0;i<n/2 +1;i++){
  60.  
  61.     if(pre[n-idx]-pre[idx]== sum){
  62.       //  cout<<pre[n-idx] <<" "<< pre[idx] <<"   ";
  63.    cout<<idx<<"\n";
  64.     break;
  65.    }
  66.    else idx++;
  67. }
  68.    
  69. }
  70.  return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement