Sum of subset differences | Practice | GeeksforGeeks

Given an array S consisting of n numbers, find the sum of difference between last and first element of each subset.
For example for set 5,8 there are 3 subsets possible
5    (difference is 5-5 = 0 as 5 is the only last and first element)
8    (difference is 8-8 = 0 as 8 is the only last and first element)
5 8 (difference is 8-5 = 3 as 8 is the last element  and 5 is the first element)
thus difference sum is 0+0+3 = 3
You are required to complete the function which returns an integer denoting the sum of difference between last and first element for each subset of array S.

Note: Elements in the subset should be in the same order as in the set S.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 2 lines the first line of input contains an integer N denoting the size of the array A. In the next line are N space separated values of the array A.

Output:
For each test case in a new line output will be the sum of difference between last and first element of each subset of array S.

Constraints:
1<=T<=100
1<=N<=20
1<=A[]<=1000

Example(To be used only for expected output) :
Input:
2
4
5 2 9 6
3
1 2 3 
Output
21
6

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
0
_luffy_2 months ago
int ans=0;   for(int i=0;i<n;i++)   {       for(int j=i+1;j<n;j++)       {           ans+= (S[j] - S[i])*pow(2,(j- i - 1));       }   }   return ans;
0
imaniket2 months ago
int sumDiff(int S[], int n)
{
    int sum=0;
    for(int i=0; i<n; i++){
        for(int j=i; j<n; j++){
            sum+= (S[j]-S[i])*(pow(2, j-i-1));
        }
    }
    
    return sum;
}
0
ayerajkumar2 months ago
class GfG
{
    int sumDiff(int S[], int n)
    {
	    int ans=0;
	    for(int i=n-1; i>0; i--){
	        for(int j=0; j<i; j++){
	            ans+=(S[i]-S[j])*Math.pow(2,i-j-1);
	        }
	    }return ans;
    }
}
        //       Subsets are:       //       {5}, last(s)-first(s) = 0.       //       {2}, last(s)-first(s) = 0.       //       {9}, last(s)-first(s) = 0.       //       {6}, last(s)-first(s) = 0.       //       {5,2}, last(s)-first(s) = -3.       //       {5,9}, last(s)-first(s) = 4.         //       {5,2,9}, last(s)-first(s) = 4.       //       {5,6}, last(s)-first(s) = 1.       //       {5,2,9,6}, last(s)-first(s) = 1.       //       {5,2,6}, last(s)-first(s) = 1.       //       {5,9,6}, last(s)-first(s) = 1.       //       {2,9}, last(s)-first(s) = 7.       //       {2,6}, last(s)-first(s) = 4.       //       {2,9,6}, last(s)-first(s) = 4.       //       {9,6}, last(s)-first(s) = -3.
+1
chessnoobdj4 months ago
C++
int sumDiff(int S[], int n)
{
    int ans = 0;
    for(int i=n-1; i>0; i--){
        for(int j=0; j<i; j++)
            ans += (S[i]-S[j])*pow(2, i-j-1);
    }
    return ans;
}
0
D Sathwik9 months ago
D Sathwik
https://uploads.disquscdn.c...
0
Tushar Sharma2 years ago
Tushar Sharma
Simplest O(n) solution || One Traversal || No Auxiliary Space || 0.01s
//By Tushar Sharma
https://ide.geeksforgeeks.o...
int sumDiff(int S[], int n){    int sum=0;    for(int i=0;i<n;i++) {sum+="((pow(2,i)-1)*S[i])-((pow(2,n-i-1)-1)*S[i]);}" return="" sum;="" }="">
0
Prateek Gole2 years ago
Prateek Gole
0.01 simplest java solnhttps://uploads.disquscdn.c...
0
Prateek Gole
This comment was deleted.
0
TUSHAR RAINA5 years ago
TUSHAR RAINA
please explain any one of given test case, i am unable to understand it.
0
ASHISH KUMAR SINGH
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.