logo

Zbroj razlika podskupa

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

Zadan je skup S koji se sastoji od n brojeva. Pronađite zbroj razlike između posljednjeg i prvog elementa svakog podskupa. Pronalazimo prvi i zadnji element svakog podskupa držeći ih istim redoslijedom kako se pojavljuju u ulaznom skupu S. tj. sumSetDiff(S) = ? (zadnji(i) - prvi(i)) gdje zbroj ide preko svih podskupova s ​​od S.

Bilješka:

Glumica Rakul Preet Singh

Elementi u podskupu trebaju biti u istom redoslijedu kao u skupu S. Primjeri:



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.

Preporučeno: riješite ga na ' PRAKSA ' prije nego prijeđete na rješenje.

Jednostavno rješenje

jer ovaj problem je pronaći razliku između posljednjeg i prvog elementa za svaki podskup s skupa S i ispisati zbroj svih tih razlika. Vremenska složenost za ovaj pristup je O(2

n

nedostaci vezani uz internet

).

Učinkovito rješenje

Glumica Rakul Preet Singh

riješiti problem u linearnoj vremenskoj složenosti. Dan nam je skup S koji se sastoji od n brojeva i trebamo izračunati zbroj razlike između posljednjeg i prvog elementa svakog podskupa od S, tj. sumSetDiff(S) = ? (zadnji(i) - prvi(i)) gdje zbroj ide preko svih podskupova s ​​od S. Ekvivalentno sumSetDiff(S) =? (zadnji(i)) - ? (prvi(i)) Drugim riječima, možemo izračunati zbroj zadnjeg elementa svakog podskupa i zbroj prvog elementa svakog podskupa zasebno i zatim izračunati njihovu razliku. Recimo da su elementi od S {a1 a2 a3... an}. Imajte na umu sljedeće:

  1. Podskupovi koji sadrže element a1 jer se prvi element može dobiti uzimanjem bilo kojeg podskupa od {a2 a3... an} i zatim uključivanjem a1 u njega. Broj takvih podskupova bit će 2n-1.
  2. Podskupovi koji sadrže element a2 kao prvi element mogu se dobiti uzimanjem bilo kojeg podskupa od {a3 a4... an} i zatim uključivanjem a2 u njega. Broj takvih podskupova bit će 2n-2.
  3. Podskupovi koji sadrže element ai kao prvi element mogu se dobiti uzimanjem bilo kojeg podskupa od {ai a(i+1)... an} i zatim uključivanjem ai u njega. Broj takvih podskupova bit će 2n-i.

  4. Stoga će zbroj prvog elementa svih podskupova biti: SumF = a1.2
  5. n-1
  6. + a2.2
  7. n-2
  8. +...+ an.1 Na sličan način možemo izračunati zbroj posljednjeg elementa svih podskupova od S (uzimajući u svakom koraku ai kao posljednji element umjesto prvog elementa i zatim dobivajući sve podskupove). Zbroj L = a1.1 + a2.2 +...+ an.2
  9. n-1
  10. Konačno će odgovor na naš problem biti
  11. SumL - SumF
  12. .
  13. Implementacija:
  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. Izlaz:
  16. 21  
  17. Vremenska složenost: O(n) Ovaj je članak napisao
  18. Akash Aggarwal
  19. . Ako vam se sviđa GeeksforGeeks i želite doprinijeti, možete napisati članak koristeći
  20. doprinos.geeksforgeeks.org
  21. ili pošaljite svoj članak e-poštom na e-mail: [email protected]. Pogledajte kako se vaš članak pojavljuje na glavnoj stranici GeeksforGeeksa i pomozite drugim Geekovima.