fork download
  1. //{ Driver Code Starts
  2. // Initial Template for C++
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7. // } Driver Code Ends
  8.  
  9. class Solution {
  10. public:
  11. // Function to find equilibrium point in the array.
  12. int findEquilibrium(vector<int> &arr) {
  13. // code here
  14. int sum0=0,sum1=0;
  15. int start=0,end=arr.size()-1;
  16. while(start<=end)
  17. {
  18. if(sum0==sum1)
  19. {
  20. if(start+2==end)
  21. return start+1;
  22. }
  23. else if(sum0<sum1)
  24. {
  25. ++start;
  26. sum0+=arr[start];
  27. }
  28. else
  29. {
  30. --end;
  31. sum1+=arr[end];
  32. }
  33. }
  34. return -1;
  35. }
  36. };
  37.  
  38.  
  39. //{ Driver Code Starts.
  40.  
  41. int main() {
  42. int t;
  43. cin >> t;
  44. cin.ignore(); // To discard any leftover newline characters
  45. while (t--) // while testcases exist
  46. {
  47. vector<int> arr;
  48. string input;
  49. getline(cin, input); // Read the entire line for the array elements
  50. stringstream ss(input);
  51. int number;
  52. while (ss >> number) {
  53. arr.push_back(number);
  54. }
  55.  
  56. Solution ob;
  57. cout << ob.findEquilibrium(arr) << endl;
  58. cout << "~" << endl;
  59. }
  60. }
  61. // } Driver Code Ends
Success #stdin #stdout 0s 5280KB
stdin
1 2 0 3
stdout
1
~