fork download
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // } Driver Code Ends
  7.  
  8. class Solution {
  9. public:
  10. int firstIndex(vector<int> &arr) {
  11. // Your code goes here
  12. int low=0;
  13. int n=arr.size();
  14. int high=n-1;
  15.  
  16. int ans=-1;
  17. while(low<=high){
  18. int mid=low+(high-low)/2;
  19. if(arr[mid]==0){
  20. low=mid+1;
  21. }
  22. else if(arr[mid]==1){
  23. ans=mid;
  24. high=mid-1;
  25. }
  26. }
  27. return ans;
  28. }
  29. };
  30.  
  31.  
  32. //{ Driver Code Starts.
  33. int main() {
  34.  
  35. int t;
  36. cin >> t;
  37. cin.ignore();
  38. while (t--) {
  39.  
  40. vector<int> arr;
  41. string input;
  42. getline(cin, input);
  43. stringstream s1(input);
  44. int num;
  45. while (s1 >> num) {
  46. arr.push_back(num);
  47. }
  48. Solution ob;
  49. cout << ob.firstIndex(arr) << endl;
  50. cout << "~" << endl;
  51. }
  52. }
  53. // } Driver Code Ends
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty