write a Program to find index of two array elements whose sum is equal to given value

Description

To find two elements from an array, whose sum is equal to given value,
use two loops, first loop start from index 0 and second loop start next
to first loop. We can check sum of elements from first loop and second
loop, if it equal to given value then we store the both index and break
the loop.
For more details follow below code.

C/C++

/* C Program to find index of two array elements whose sum is equal to given value */
//Save it as SumTwoEqualsTarget.c

#include<stdio.h>
int main(){

    int i, j, n, val;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the array elements : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Enter the target value : ");
    scanf("%d",&val);

    int index1=-1, index2=-1;

    for(i=0;i<(n-1);i++) {
        for(j=(i+1);j<n;j++) {
            if((arr[i]+arr[j]) == val) {
                index1 = i;
                index2 = j;
            }
        }
    }
    if(index1==-1 && index2==-1) {
        printf("Elements not found");
    }else {
        printf("The element at %d and %d is equal to %d",index1,index2,val);
    }

    return 0;
}
Input:
Enter the size of array : 
5
Enter the array elements : 
2
6
4
5
7
Enter the target value : 
11

Output:
The element at 2 and 4 is equal to 11

Java

/* Java Program to find index of two array elements whose sum is equal to given value */
//Save it as SumTwoEqualsTarget.java

import java.io.*;
import java.util.Scanner;

public class SumTwoEqualsTarget {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        System.out.println("Enter the array elements : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Enter the target value : ");
        int val = scanner.nextInt();
        
        int index1=-1, index2=-1;
        
        for(int i=0;i<(n-1);i++) {
            for(int j=(i+1);j<n;j++) {
                if((arr[i]+arr[j]) == val) {
                    index1 = i;
                    index2 = j;
                    break;
                }
            }
            
        }
        if(index1==-1 && index2==-1) {
            System.out.println("Elements not found");
        }else {
            System.out.println("The element at "+index1+" and "+index2+" is equal to "+val);
        }
    }
}
Input:
Enter the size of array : 
5
Enter the array elements : 
2
6
4
5
7
Enter the target value : 
11

Output:
The element at 2 and 4 is equal to 11

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to insert a number at given position of an Array
4) Program to delete an element from given location in an array
5) Program to delete an element from an array sorted in ascending order
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find the smallest missing number
8) Program to Find Value equal to index value
9) Program to make pair of elements alternatively
10) Remove Duplicate Elements From Array
Share Me

Leave a Reply