Sum of products of all possible Subarrays | Java program for the above approach ; Function that finds the sum of products of all subarray of arr [ ] ; Stores sum of all subarrays ; Iterate array from behind ; Update the ans ; Update the res ; Print the final sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . io . * ; class GFG { static void sumOfSubarrayProd ( int arr [ ] , int n ) { int ans = 0 ; int res = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int incr = arr [ i ] * ( 1 + res ) ; ans += incr ; res = incr ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int N = arr . length ; sumOfSubarrayProd ( arr , N ) ; } }