Somma delle differenze di sottoinsieme

Somma delle differenze di sottoinsieme
Provalo su GfG Practice #practiceLinkDiv { display: none! importante; }

Dato un insieme S composto da n numeri trovare la somma della differenza tra l'ultimo e il primo elemento di ciascun sottoinsieme. Troviamo il primo e l'ultimo elemento di ogni sottoinsieme mantenendoli nello stesso ordine in cui appaiono nell'insieme di input S. ovvero sumSetDiff(S) = ? (ultimo(i) - primo(i)) dove la somma copre tutti i sottoinsiemi s di S.

Nota:

Gli elementi nel sottoinsieme dovrebbero essere nello stesso ordine dell'insieme S. Esempi:

 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.

Consigliato: risolverlo su ' PRATICA ' prima di passare alla soluzione.

Una soluzione semplice

poiché questo problema consiste nel trovare la differenza tra l'ultimo e il primo elemento per ciascun sottoinsieme s dell'insieme S e restituire la somma di tutte queste differenze. La complessità temporale per questo approccio è O(2

N

).

Una soluzione efficiente

risolvere il problema in complessità temporale lineare. Ci viene dato un insieme S composto da n numeri e dobbiamo calcolare la somma della differenza tra l'ultimo e il primo elemento di ciascun sottoinsieme di S, ovvero sumSetDiff(S) = ? (ultimo(i) - primo(i)) dove la somma copre tutti i sottoinsiemi s di S. Equivalentemente sumSetDiff(S) = ? (ultimo(i)) - ? (first(s)) In altre parole possiamo calcolare la somma dell'ultimo elemento di ciascun sottoinsieme e la somma del primo elemento di ciascun sottoinsieme separatamente e quindi calcolare la loro differenza. Diciamo che gli elementi di S sono {a1 a2 a3... an}. Si noti la seguente osservazione:

  1. Sottoinsiemi contenenti elemento a1 poiché il primo elemento può essere ottenuto prendendo qualsiasi sottoinsieme di {a2 a3... an} e includendovi a1. Il numero di tali sottoinsiemi sarà 2 n-1 .
  2. I sottoinsiemi contenenti l'elemento a2 come primo elemento possono essere ottenuti prendendo qualsiasi sottoinsieme di {a3 a4... an} e quindi includendovi a2. Il numero di tali sottoinsiemi sarà 2 n-2 .
  3. I sottoinsiemi contenenti l'elemento ai come primo elemento possono essere ottenuti prendendo qualsiasi sottoinsieme di {ai a(i+1)... an} e quindi includendovi ai. Il numero di tali sottoinsiemi sarà 2 n-i .

  4. Pertanto la somma del primo elemento di tutti i sottoinsiemi sarà: SumF = a1.2
  5. n-1
  6. +a2.2
  7. n-2
  8. +...+ an.1 In modo simile possiamo calcolare la somma dell'ultimo elemento di tutti i sottoinsiemi di S (prendendo ad ogni passo ai come ultimo elemento anziché primo elemento e quindi ottenendo tutti i sottoinsiemi). Somma L = a1.1 + a2.2 +...+ an.2
  9. n-1
  10. Finalmente la risposta al nostro problema sarà
  11. SommaL - SommaF
  12. .
  13. Attuazione:
  14. C++
       // 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  ;   }   
    Java
       // 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.   
    Python3
       # 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.   
    C#
          // 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.   
    JavaScript
       // 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  ();   
    PHP
          // 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.   ?>   
  15. Produzione:
  16.  21   
  17. Complessità temporale: O(n) Questo articolo è stato fornito da
  18. Akash Aggarwal
  19. . Se ti piace GeeksforGeeks e desideri contribuire puoi anche scrivere un articolo utilizzando
  20. contribuire.geeksforgeeks.org
  21. o invia il tuo articolo per posta a [email protected]. Guarda il tuo articolo apparire sulla pagina principale di GeeksforGeeks e aiuta altri Geeks.
Crea quiz