Find the sum of all possible pairs in an array of N elements | Java implementation of the approach ; Function to return the sum of the elements of all possible pairs from the array ; To store the required sum ; For every element of the array ; It appears ( 2 * n ) times ; Driver code
import java . util . * ; class GFG { static int sumPairs ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] * ( 2 * n ) ) ; } return sum ; } static public void main ( String [ ] arg ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( sumPairs ( arr , n ) ) ; } }