logo

Poaibių skirtumų suma

Išbandykite GfG praktikoje ' title= #practiceLinkDiv { display: none !important; }

Duota aibė S, susidedanti iš n skaičių, raskite skirtumo tarp paskutinio ir pirmojo kiekvieno poaibio elementų sumą. Pirmąjį ir paskutinį kiekvieno poaibio elementus randame išlaikydami juos tokia tvarka, kokia jie yra įvesties rinkinyje S. ty sumSetDiff(S) = ? (paskutinė(i) – pirma(i)), kur suma eina per visus S poaibius.

Pastaba:

java len of masyvo

Elementai poaibyje turi būti tokia pačia tvarka kaip ir rinkinyje S. Pavyzdžiai:



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.

Rekomenduojama: išspręskite tai „ PRAKTIKA “, prieš pereinant prie sprendimo.

Paprastas sprendimas

tinklo topologija

nes ši problema yra rasti skirtumą tarp paskutinio ir pirmojo elemento kiekvienam aibės S poaibiui s ir išvesti visų šių skirtumų sumą. Šio metodo laiko sudėtingumas yra O (2

n

).

char + int java

Efektyvus sprendimas

išspręsti problemą tiesiniu laiko sudėtingumu. Mums duota aibė S, susidedanti iš n skaičių, ir turime apskaičiuoti skirtumo tarp paskutinio ir pirmojo kiekvieno S poaibio elemento sumą, ty sumSetDiff(S) = ? (paskutinis(iai) – pirmas(-iai)), kur suma eina per visus S poaibius s. Lygiai sumSetDiff(S) = ? (paskutinis (-iai)) - ? (pirmas(s)) Kitaip tariant, galime atskirai apskaičiuoti kiekvieno poaibio paskutinio elemento sumą ir kiekvieno poaibio pirmojo elemento sumą ir tada apskaičiuoti jų skirtumą. Sakykime, kad S elementai yra {a1 a2 a3... an}. Atkreipkite dėmesį į tokį pastebėjimą:

  1. Poaibiai, kuriuose yra elementas a1 kaip pirmąjį elementą galima gauti paėmus bet kurį {a2 a3... an} poaibį ir į jį įtraukus a1. Tokių pogrupių skaičius bus 2n-1.
  2. Poaibius, kurių pirmasis elementas yra elementas a2, galima gauti paėmus bet kurį {a3 a4...an} poaibį ir į jį įtraukus a2. Tokių pogrupių skaičius bus 2n-2.
  3. Poaibius, kurių pirmasis elementas yra elementas ai, galima gauti paėmus bet kurį {ai a(i+1)...an} poaibį ir į jį įtraukus ai. Tokių pogrupių skaičius bus 2n-i.

  4. Todėl visų poaibių pirmojo elemento suma bus tokia: SumF = a1.2
  5. n-1
  6. + a2.2
  7. n-2
  8. +...+ an.1 Panašiu būdu galime apskaičiuoti visų S poaibių paskutinio elemento sumą (kiekviename žingsnyje ai imame paskutinį elementą vietoj pirmojo elemento ir tada gauname visus poaibius). SumaL = a1.1 + a2.2 +...+ an.2
  9. n-1
  10. Pagaliau atsakymas į mūsų problemą bus
  11. SumL – SumF
  12. .
  13. Įgyvendinimas:
  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. Išvestis:
  16. 21  
  17. Laiko sudėtingumas: O(n) Šio straipsnio autorius
  18. Akash Aggarwal
  19. . Jei jums patinka GeeksforGeeks ir norėtumėte prisidėti, taip pat galite parašyti straipsnį naudodami
  20. prisidėti.geeksforgeeks.org
  21. arba atsiųskite savo straipsnį adresu közreműkö[email protected]. Peržiūrėkite savo straipsnį „GeeksforGeeks“ pagrindiniame puslapyje ir padėkite kitiems „Geeks“.