Suma de diferències de subconjunt
#practiceLinkDiv { mostrar: cap !important; } Donat un conjunt S format per n nombres, trobeu la suma de la diferència entre l'últim i el primer element de cada subconjunt. Trobem el primer i l'últim element de cada subconjunt mantenint-los en el mateix ordre que apareixen al conjunt d'entrada S. és a dir, sumSetDiff(S) = ? (últim(s) - primer(s)) on la suma passa per tots els subconjunts s de S.
Nota:
Els elements del subconjunt haurien d'estar en el mateix ordre que en el conjunt S. Exemples:
S = {5 2 9 6} n = 4
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.
{52} last(s)-first(s) = -3.
{59} last(s)-first(s) = 4.
{56} last(s)-first(s) = 1.
{29} last(s)-first(s) = 7.
{26} last(s)-first(s) = 4.
{96} last(s)-first(s) = -3.
{529} last(s)-first(s) = 4.
{526} last(s)-first(s) = 1.
{596} last(s)-first(s) = 1.
{296} last(s)-first(s) = 4.
{5296} last(s)-first(s) = 1.
Output = -3+4+1+7+4-3+4+1+1+4+1
= 21.
Recomanat: si us plau, resol-ho a ' PRÀCTICA primer abans de passar a la solució.
Una solució senzilla
perquè aquest problema és trobar la diferència entre l'últim i el primer element per a cada subconjunt s del conjunt S i donar sortida a la suma de ll aquestes diferències. La complexitat temporal d'aquest enfocament és O(2
n
).
Una solució eficient
per resoldre el problema en complexitat temporal lineal. Se'ns dóna un conjunt S format per n nombres i hem de calcular la suma de la diferència entre l'últim i el primer element de cada subconjunt de S, és a dir, sumSetDiff(S) = ? (últim(s) - primer(s)) on la suma passa per tots els subconjunts s de S. De manera equivalent sumSetDiff(S) = ? (últim(s)) -? (primer(s)) En altres paraules, podem calcular la suma de l'últim element de cada subconjunt i la suma del primer element de cada subconjunt per separat i després calcular la seva diferència. Diguem que els elements de S són {a1 a2 a3... an}. Tingueu en compte la següent observació:
Crea un qüestionari
- Subconjunts que contenen l'element a1 ja que el primer element es pot obtenir agafant qualsevol subconjunt de {a2 a3... an} i després incloent-hi a1. El nombre d'aquests subconjunts serà 2 n-1 .
- Els subconjunts que contenen l'element a2 com a primer element es poden obtenir agafant qualsevol subconjunt de {a3 a4... an} i després incloent-hi a2. El nombre d'aquests subconjunts serà 2 n-2 .
- Els subconjunts que contenen l'element ai com a primer element es poden obtenir agafant qualsevol subconjunt de {ai a(i+1)... an} i després incloent-hi ai. El nombre d'aquests subconjunts serà 2 n-i .
- Per tant, la suma del primer element de tots els subconjunts serà: SumF = a1.2
- n-1
- + a2.2
- n-2
- +...+ an.1 De manera semblant podem calcular la suma de l'últim element de tots els subconjunts de S (Aprenent a cada pas ai com a darrer element en lloc de primer element i obtenint després tots els subconjunts). SumL = a1.1 + a2.2 +...+ an.2
- n-1
- Finalment la resposta al nostre problema serà
- SumL - SumF
- .
- Implementació:
- C++
Java// A C++ program to find sum of difference between // last and first element of each subset #include// Returns the sum of first elements of all subsets int SumF ( int S [] int n ) { int sum = 0 ; // Compute the SumF as given in the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( S [ i ] * pow ( 2 n - i -1 )); return sum ; } // Returns the sum of last elements of all subsets int SumL ( int S [] int n ) { int sum = 0 ; // Compute the SumL as given in the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( S [ i ] * pow ( 2 i )); return sum ; } // Returns the difference between sum of last elements of // each subset and the sum of first elements of each subset int sumSetDiff ( int S [] int n ) { return SumL ( S n ) - SumF ( S n ); } // Driver program to test above function int main () { int n = 4 ; int S [] = { 5 2 9 6 }; printf ( '%d n ' sumSetDiff ( S n )); return 0 ; } Python3// A Java program to find sum of difference // between last and first element of each // subset class GFG { // Returns the sum of first elements // of all subsets static int SumF ( int S [] int n ) { int sum = 0 ; // Compute the SumF as given in // the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int )( S [ i ] * Math . pow ( 2 n - i - 1 )); return sum ; } // Returns the sum of last elements // of all subsets static int SumL ( int S [] int n ) { int sum = 0 ; // Compute the SumL as given in // the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int )( S [ i ] * Math . pow ( 2 i )); return sum ; } // Returns the difference between sum // of last elements of each subset and // the sum of first elements of each // subset static int sumSetDiff ( int S [] int n ) { return SumL ( S n ) - SumF ( S n ); } // Driver program public static void main ( String arg [] ) { int n = 4 ; int S [] = { 5 2 9 6 }; System . out . println ( sumSetDiff ( S n )); } } // This code is contributed by Anant Agarwal.C## Python3 program to find sum of # difference between last and # first element of each subset # Returns the sum of first # elements of all subsets def SumF ( S n ): sum = 0 # Compute the SumF as given # in the above explanation for i in range ( n ): sum = sum + ( S [ i ] * pow ( 2 n - i - 1 )) return sum # Returns the sum of last # elements of all subsets def SumL ( S n ): sum = 0 # Compute the SumL as given # in the above explanation for i in range ( n ): sum = sum + ( S [ i ] * pow ( 2 i )) return sum # Returns the difference between sum # of last elements of each subset and # the sum of first elements of each subset def sumSetDiff ( S n ): return SumL ( S n ) - SumF ( S n ) # Driver program n = 4 S = [ 5 2 9 6 ] print ( sumSetDiff ( S n )) # This code is contributed by Anant Agarwal.JavaScript// A C# program to find sum of difference // between last and first element of each // subset using System ; class GFG { // Returns the sum of first elements // of all subsets static int SumF ( int [] S int n ) { int sum = 0 ; // Compute the SumF as given in // the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int )( S [ i ] * Math . Pow ( 2 n - i - 1 )); return sum ; } // Returns the sum of last elements // of all subsets static int SumL ( int [] S int n ) { int sum = 0 ; // Compute the SumL as given in // the above explanation for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int )( S [ i ] * Math . Pow ( 2 i )); return sum ; } // Returns the difference between sum // of last elements of each subset and // the sum of first elements of each // subset static int sumSetDiff ( int [] S int n ) { return SumL ( S n ) - SumF ( S n ); } // Driver program public static void Main () { int n = 4 ; int [] S = { 5 2 9 6 }; Console . Write ( sumSetDiff ( S n )); } } // This code is contributed by nitin mittal.PHP// Returns the sum of first elements of all subsets function sumF ( S n ) { let sum = 0 ; // Compute the SumF as given in the above explanation for ( let i = 0 ; i < n ; i ++ ) { sum += S [ i ] * Math . pow ( 2 n - i - 1 ); } return sum ; } // Returns the sum of last elements of all subsets function sumL ( S n ) { let sum = 0 ; // Compute the SumL as given in the above explanation for ( let i = 0 ; i < n ; i ++ ) { sum += S [ i ] * Math . pow ( 2 i ); } return sum ; } // Returns the difference between sum of last elements of each subset and the sum of first elements of each subset function sumSetDiff ( S n ) { return sumL ( S n ) - sumF ( S n ); } // Driver program to test the above functions function main () { const n = 4 ; const S = [ 5 2 9 6 ]; console . log ( sumSetDiff ( S n )); } main ();// A PHP program to find sum // of difference between last // and first element of each subset // Returns the sum of first // elements of all subsets function SumF ( $S $n ) { $sum = 0 ; // Compute the SumF as given // in the above explanation for ( $i = 0 ; $i < $n ; $i ++ ) $sum = $sum + ( $S [ $i ] * pow ( 2 $n - $i - 1 )); return $sum ; } // Returns the sum of last // elements of all subsets function SumL ( $S $n ) { $sum = 0 ; // Compute the SumL as given // in the above explanation for ( $i = 0 ; $i < $n ; $i ++ ) $sum = $sum + ( $S [ $i ] * pow ( 2 $i )); return $sum ; } // Returns the difference between // sum of last elements of // each subset and the sum of // first elements of each subset function sumSetDiff ( $S $n ) { return SumL ( $S $n ) - SumF ( $S $n ); } // Driver Code $n = 4 ; $S = array ( 5 2 9 6 ); echo sumSetDiff ( $S $n ); // This code is contributed by anuj_67. ?>- Sortida:
21
- Complexitat temporal: O(n) Aquest article és contribuït per
- Akash Aggarwal
- . Si t'agrada GeeksforGeeks i vols contribuir també pots escriure un article utilitzant
- contribute.geeksforgeeks.org
- o envia el teu article a [email protected]. Vegeu el vostre article que apareix a la pàgina principal de GeeksforGeeks i ajudeu altres Geeks.