logo

サブセットの差の合計

GfG Practice で試してみる ' title= #practiceLinkDiv { 表示: なし !重要; }

n 個の数値で構成される集合 S が与えられた場合、各サブセットの最後の要素と最初の要素の差の合計を求めます。すべてのサブセットの最初と最後の要素を、入力セット S に現れるのと同じ順序に保つことによって見つけます。つまり、 sumSetDiff(S) = ? (last(s) - first(s)) ここで、合計は S のすべてのサブセット s に及びます。

注記:

git pull オリジンマスター

サブセット内の要素は、セット S 内と同じ順序である必要があります。例:



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.

推奨: で解決してください。 練習する 解決策に進む前に、まず最初に確認してください。

シンプルな解決策

この問題は、集合 S の各部分集合 s の最後の要素と最初の要素の差を見つけて、これらすべての差の合計を出力することです。このアプローチの時間計算量は O(2

n

iskcon の完全な形式

)。

効率的なソリューション

Javaで配列を返す

線形時間計算量で問題を解決します。 n 個の数値で構成される集合 S が与えられ、S の各サブセットの最後の要素と最初の要素の差の合計を計算する必要があります。つまり、 sumSetDiff(S) = ? (last(s) - first(s)) ここで、合計は S のすべてのサブセット s に及びます。同様に sumSetDiff(S) = ? (最後) - ? (first(s)) つまり、各サブセットの最後の要素の合計と各サブセットの最初の要素の合計を個別に計算し、それらの差を計算できます。 S の要素が {a1 a2 a3... an} であるとします。次の点に注意してください。

  1. 要素を含むサブセット a1 最初の要素は、{a2 a3... an} の任意のサブセットを取得し、それに a1 を含めることによって取得できます。このようなサブセットの数は 2 になります。n-1
  2. 最初の要素として要素 a2 を含むサブセットは、{a3 a4... an} の任意のサブセットを取得し、それに a2 を含めることによって取得できます。このようなサブセットの数は 2 になります。n-2
  3. 最初の要素として要素 ai を含むサブセットは、{ai a(i+1)... an} の任意のサブセットを取得し、それに ai を含めることによって取得できます。このようなサブセットの数は 2 になります。に、い

  4. したがって、すべてのサブセットの最初の要素の合計は次のようになります: SumF = a1.2
  5. n-1
  6. +a2.2
  7. n-2
  8. +...+ an.1 同様の方法で、S のすべての部分集合の最後の要素の合計を計算できます (各ステップで ai を最初の要素ではなく最後の要素として取り、すべての部分集合を取得します)。 SumL = a1.1 + a2.2 +...+ an.2
  9. n-1
  10. 最終的に私たちの問題の答えは次のようになります
  11. SumL - SumF
  12. 実装:
  13. 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. ?> 
  14. 出力:
  15. 21  
  16. 時間計算量 : O(n) この記事は次の寄稿者によるものです
  17. アカシュ・アガルワル
  18. 。 GeeksforGeeks が好きで貢献したい場合は、次を使用して記事を書くこともできます。
  19. 貢献する.geeksforgeeks.org
  20. または記事を[email protected]にメールしてください。 GeeksforGeeks のメイン ページにあなたの記事が掲載されているのを見て、他のオタクを助けてください。