fork download
  1. /*Write a program to take an integer array nums which contains only positive integers, and an integer target as
  2. inputs. The goal is to find two distinct indices i and j in the array such that nums[i] + nums[j] equals the
  3. target. Assume exactly one solution exists and return the indices in any order. Print the two indices separated
  4. by a space as output. If no solution exists, print "-1 -1".*/
  5. #include <stdio.h>
  6.  
  7. int main() {
  8. int n, target;
  9. scanf("%d", &n);
  10.  
  11. int nums[n];
  12. for (int i = 0; i < n; i++)
  13. scanf("%d", &nums[i]);
  14.  
  15. scanf("%d", &target);
  16.  
  17. for (int i = 0; i < n; i++) {
  18. for (int j = i + 1; j < n; j++) {
  19. if (nums[i] + nums[j] == target) {
  20. printf("%d %d", i, j);
  21. return 0;
  22. }
  23. }
  24. }
  25.  
  26. printf("-1 -1");
  27. return 0;
  28. }
  29.  
  30.  
Success #stdin #stdout 0s 5316KB
stdin
5
2 7 11 15 3
9
stdout
0 1