fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[] = {1,1,1,1,1,2,3,4,4,5};
  6. int target = 1;
  7. int first=0, second=0;
  8. int n = sizeof(arr)/sizeof(arr[0]);
  9. for(int i = 0;i<n;i++){
  10. if(target == arr[i]){
  11. first =i;
  12. break;
  13. }
  14. }
  15. for(int j =n-1; j>=0;j--){
  16. if(target == arr[j]){
  17. second =j;
  18. break;
  19. }
  20. }
  21. cout<<"First index of "<< target << " is: "<<first<<endl;
  22. cout<<"Second index of "<< target << " is: "<<second<<endl;
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
First index of 1 is: 0
Second index of 1 is: 4