LeetCode (Sum of two indices equal to Target value)

·

1 min read

#include <iostream>
#include <stdbool.h>
using namespace std;
int main() {

    bool isFound=false;
    int arr[7]={
4,6,7,8,10,12,14       
    };
    int target=20;
    int num1=0,num2=0;
    for(int i=0;i<6;i++){
      if(arr[i]+arr[i+1]==target){
          isFound=true;
          num1=i;
          num2=i+1;
          break;
      }
    }
    if(isFound)
    cout<<"Element at index "<<num1<<" and "<<num2<<" add up to "<<target<<endl;
    else cout<<"No two items add up to "<<target<<endl;

    return 0;
}