Skip to content

Instantly share code, notes, and snippets.

@DhruvaG2000
Created September 1, 2020 17:45
Show Gist options
  • Save DhruvaG2000/19826c732ad2ccaf7abb3fa8c7ffd823 to your computer and use it in GitHub Desktop.
Save DhruvaG2000/19826c732ad2ccaf7abb3fa8c7ffd823 to your computer and use it in GitHub Desktop.
cpp code
#include<iostream>
// using namespace std;
int main(){
// declarations
int nums[]={2,7,11,15}, target = 9;
/*
return the indices of the two numbers such that they add up to target
*/
// my code
int first_num=nums[0], second_num=nums[1], index_of_first_num=0, index_of_second_num=1;
for (int i = 0; i < sizeof(nums); i++)
{
first_num=nums[i];
for (int j = 0; j < sizeof(nums); j++)
{
second_num=nums[j];
/* code */
if ((first_num+second_num)==9)
{
index_of_first_num=i;
index_of_second_num=j;
}
}
}
std::cout << "the indices are:" << index_of_first_num << " and "<< index_of_second_num <<std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment