fork download
  1. //
  2. // main.cpp
  3. // Sort Binary Array
  4. //
  5. // Created by Himanshu on 06/04/22.
  6. //
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int solve (int arr[], int n) {
  12. int ans = 0;
  13. int currOnePos = n-1;
  14.  
  15. for (int index=n-1; index>=0; index--) {
  16. if (arr[index] == 1) {
  17. ans += currOnePos-index;
  18. currOnePos--;
  19. }
  20. }
  21. return ans;
  22. }
  23.  
  24. int main() {
  25.  
  26. int n;
  27. cin>>n;
  28.  
  29. int *arr = new int[n]();
  30.  
  31. for (int i=0; i<n; i++) {
  32. cin>>arr[i];
  33. }
  34.  
  35. cout<<solve(arr, n)<<endl;
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5532KB
stdin
7
1 1 0 0 0 1 0
stdout
9