logo

Vsota razlik podmnožic

Preizkusite na GfG Practice ' title= #practiceLinkDiv { display: none !important; }

Za množico S, sestavljeno iz n števil, poiščite vsoto razlike med zadnjim in prvim elementom vsake podmnožice. Prvi in ​​zadnji element vsake podmnožice najdemo tako, da ju ohranimo v istem vrstnem redu, kot sta prikazana v vhodnem nizu S. tj. sumSetDiff(S) =? (zadnje(-e) – prve(-e)), kjer vsota zajema vse podmnožice s od S.

Opomba:

ukaz grep v linuxu

Elementi v podmnožici morajo biti v enakem vrstnem redu kot v množici S. Primeri:



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.

Priporočeno: prosimo, rešite na ' VADITE « preden nadaljujete z rešitvijo.

Preprosta rešitev

kajti ta problem je najti razliko med zadnjim in prvim elementom za vsako podmnožico s množice S in izpisati vsoto vseh teh razlik. Time complexity for this approach is O(2

niz v datum

n

).

Učinkovita rešitev

to solve the problem in linear time complexity. Podan nam je niz S, sestavljen iz n števil, in izračunati moramo vsoto razlike med zadnjim in prvim elementom vsake podmnožice S, tj. sumSetDiff(S) =? (zadnje(-e) - prve(-e)), kjer vsota zajema vse podmnožice s od S. Enakovredno je sumSetDiff(S) =? (zadnji(e)) - ? (first(s)) Z drugimi besedami, lahko izračunamo vsoto zadnjega elementa vsake podmnožice in vsoto prvega elementa vsake podmnožice posebej in nato izračunamo njuno razliko. Recimo, da so elementi S {a1 a2 a3... an}. Note the following observation:

meja z uporabo css
  1. a1 kot prvi element lahko dobimo tako, da vzamemo katero koli podmnožico {a2 a3... an} in nato vanjo vključimo a1. Število takih podmnožic bo 2n-1.
  2. Podmnožice, ki vsebujejo element a2 kot prvi element, lahko dobimo tako, da vzamemo katero koli podmnožico {a3 a4... an} in vanjo nato vključimo a2. Število takih podmnožic bo 2n-2.
  3. Podmnožice, ki vsebujejo element ai kot prvi element, lahko dobimo tako, da vzamemo katero koli podmnožico {ai a(i+1)... an} in vanjo nato vključimo ai. Število takih podmnožic bo 2n-i.

  4. Zato bo vsota prvega elementa vseh podmnožic: SumF = a1.2
  5. n-1
  6. + a2.2
  7. n-2
  8. +...+ an.1 Na podoben način lahko izračunamo vsoto zadnjega elementa vseh podmnožic S (pri vsakem koraku vzamemo ai kot zadnji element namesto prvega elementa in nato pridobimo vse podmnožice). SumL = a1.1 + a2.2 +...+ an.2
  9. n-1
  10. Končno bo odgovor na naš problem
  11. SumL – SumF
  12. .
  13. Izvedba:
  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. Izhod:
  16. 21  
  17. Časovna zahtevnost : O(n) Ta članek je prispeval
  18. Akash Aggarwal
  19. . Če vam je GeeksforGeeks všeč in bi radi prispevali, lahko tudi napišete članek z uporabo
  20. prispevati.geeksforgeeks.org
  21. ali pošljite svoj članek na e-poštni naslov [email protected]. Oglejte si, da se vaš članek pojavi na glavni strani GeeksforGeeks in pomagajte drugim Geekom.