logo

Súčet rozdielov podmnožín

Vyskúšajte to na GfG Practice ' title= #practiceLinkDiv { display: none !important; }

Daná množina S pozostávajúca z n čísel nájdite súčet rozdielov medzi posledným a prvým prvkom každej podmnožiny. Prvý a posledný prvok každej podmnožiny nájdeme tak, že ich ponecháme v rovnakom poradí, v akom sa objavujú vo vstupnej množine S. t.j. sumSetDiff(S) = ? (posledné (posledné) - prvé (prvé)), kde súčet presahuje všetky podmnožiny S.

Poznámka:

Sharwanand

Prvky v podmnožine by mali byť v rovnakom poradí ako v množine S. Príklady:



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.

Odporúčané: Vyriešte to na ' PRAXE “ predtým, ako prejdete k riešeniu.

Jednoduché riešenie

pre tento problém je nájsť rozdiel medzi posledným a prvým prvkom pre každú podmnožinu s množiny S a vygenerovať súčet všetkých týchto rozdielov. Časová zložitosť pre tento prístup je O(2

n

menná konvencia java

).

Efektívne riešenie

algoritmus rýchleho triedenia

vyriešiť problém v lineárnej časovej zložitosti. Dostali sme množinu S pozostávajúcu z n čísel a musíme vypočítať súčet rozdielu medzi posledným a prvým prvkom každej podmnožiny S, t.j. sumSetDiff(S) = ? (last(s) - first(s)), kde súčet presahuje všetky podmnožiny s S. Ekvivalentne sumSetDiff(S) = ? (posledné) - ? (first(s)) Inými slovami, môžeme vypočítať súčet posledného prvku každej podmnožiny a súčet prvého prvku každej podmnožiny samostatne a potom vypočítať ich rozdiel. Povedzme, že prvky S sú {a1 a2 a3... an}. Všimnite si nasledujúce pozorovanie:

  1. Podmnožiny obsahujúce prvok a1 ako prvý prvok možno získať tak, že vezmeme akúkoľvek podmnožinu {a2 a3... an} a potom do nej zahrnieme a1. Počet takýchto podskupín bude 2n-1.
  2. Podmnožiny obsahujúce prvok a2 ako prvý prvok možno získať tak, že vezmeme akúkoľvek podmnožinu {a3 a4... an} a potom do nej zahrnieme a2. Počet takýchto podskupín bude 2n-2.
  3. Podmnožiny obsahujúce prvok ai ako prvý prvok možno získať tak, že vezmeme akúkoľvek podmnožinu {ai a(i+1)... an} a potom do nej zahrnieme ai. Počet takýchto podskupín bude 2n-i.

  4. Preto súčet prvého prvku všetkých podmnožín bude: SumF = a1,2
  5. n-1
  6. + a2.2
  7. n-2
  8. +...+ an.1 Podobným spôsobom môžeme vypočítať súčet posledného prvku všetkých podmnožín S (pri každom kroku brať ai ako posledný prvok namiesto prvého prvku a potom získať všetky podmnožiny). SumL = a1,1 + a2,2 +...+ an.2
  9. n-1
  10. Nakoniec bude odpoveď na náš problém
  11. SumL - SumF
  12. .
  13. Implementácia:
  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('%dn' 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. výstup:
  16. 21  
  17. Časová zložitosť: O(n) Autor tohto článku
  18. Akash Aggarwal
  19. . Ak máte radi GeeksforGeeks a chceli by ste prispieť, môžete tiež napísať článok pomocou
  20. prispieť.geeksforgeeks.org
  21. alebo pošlite svoj článok na prispievateľ@geeksforgeeks.org. Pozrite si, ako sa váš článok zobrazuje na hlavnej stránke GeeksforGeeks a pomôžte ostatným Geeks.