给定一个整数数组,找到该数组中四个和的总和等于给定值X的所有组合。
例如,如果给定的数组是{10,2,3,4,5,9,7,8}且X = 23,则您的函数应打印“ 3 5 7 8”(3 + 5 + 7 + 8 = 23)。
一个朴素的解决方案是生成所有可能的四元组,并将每个四元组的和与X进行比较。以下代码使用四个嵌套循环实现此简单方法
C++
// C++ program for naive solution to
// print all combination of 4 elements
// in A[] with sum equal to X
#include
using namespace std;
/* A naive solution to print all combination
of 4 elements in A[]with sum equal to X */
void findFourElements(int A[], int n, int X)
{
// Fix the first element and find other three
for (int i = 0; i < n - 3; i++)
{
// Fix the second element and find other two
for (int j = i + 1; j < n - 2; j++)
{
// Fix the third element and find the fourth
for (int k = j + 1; k < n - 1; k++)
{
// find the fourth
for (int l = k + 1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
cout << A[i] <<", " << A[j]
<< ", " << A[k] << ", " << A[l];
}
}
}
}
// Driver Code
int main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof(A) / sizeof(A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
// This code is contributed
// by Akanksha Rai
C
#include
/* A naive solution to print all combination of 4 elements in A[]
with sum equal to X */
void findFourElements(int A[], int n, int X)
{
// Fix the first element and find other three
for (int i = 0; i < n-3; i++)
{
// Fix the second element and find other two
for (int j = i+1; j < n-2; j++)
{
// Fix the third element and find the fourth
for (int k = j+1; k < n-1; k++)
{
// find the fourth
for (int l = k+1; l < n; l++)
if (A[i] + A[j] + A[k] + A[l] == X)
printf("%d, %d, %d, %d", A[i], A[j], A[k], A[l]);
}
}
}
}
// Driver program to test above function
int main()
{
int A[] = {10, 20, 30, 40, 1, 2};
int n = sizeof(A) / sizeof(A[0]);
int X = 91;
findFourElements (A, n, X);
return 0;
}
Java
class FindFourElements
{
/* A naive solution to print all combination of 4 elements in A[]
with sum equal to X */
void findFourElements(int A[], int n, int X)
{
// Fix the first element and find other three
for (int i = 0; i < n - 3; i++)
{
// Fix the second element and find other two
for (int j = i + 1; j < n - 2; j++)
{
// Fix the third element and find the fourth
for (int k = j + 1; k < n - 1; k++)
{
// find the fourth
for (int l = k + 1; l < n; l++)
{
if (A[i] + A[j] + A[k] + A[l] == X)
System.out.print(A[i]+" "+A[j]+" "+A[k]
+" "+A[l]);
}
}
}
}
}
// Driver program to test above functions
public static void main(String[] args)
{
FindFourElements findfour = new FindFourElements();
int A[] = {10, 20, 30, 40, 1, 2};
int n = A.length;
int X = 91;
findfour.findFourElements(A, n, X);
}
}
Python3
# A naive solution to print all combination
# of 4 elements in A[] with sum equal to X
def findFourElements(A, n, X):
# Fix the first element and find
# other three
for i in range(0,n-3):
# Fix the second element and
# find other two
for j in range(i+1,n-2):
# Fix the third element
# and find the fourth
for k in range(j+1,n-1):
# find the fourth
for l in range(k+1,n):
if A[i] + A[j] + A[k] + A[l] == X:
print ("%d, %d, %d, %d"
%( A[i], A[j], A[k], A[l]))
# Driver program to test above function
A = [10, 2, 3, 4, 5, 9, 7, 8]
n = len(A)
X = 23
findFourElements (A, n, X)
# This code is contributed by shreyanshi_arun
C#
// C# program for naive solution to
// print all combination of 4 elements
// in A[] with sum equal to X
using System;
class FindFourElements
{
void findFourElements(int []A, int n, int X)
{
// Fix the first element and find other three
for (int i = 0; i < n - 3; i++)
{
// Fix the second element and find other two
for (int j = i + 1; j < n - 2; j++)
{
// Fix the third element and find the fourth
for (int k = j + 1; k < n - 1; k++)
{
// find the fourth
for (int l = k + 1; l < n; l++)
{
if (A[i] + A[j] + A[k] + A[l] == X)
Console.Write(A[i] + " " + A[j] +
" " + A[k] + " " + A[l]);
}
}
}
}
}
// Driver program to test above functions
public static void Main()
{
FindFourElements findfour = new FindFourElements();
int []A = {10, 20, 30, 40, 1, 2};
int n = A.Length;
int X = 91;
findfour.findFourElements(A, n, X);
}
}
// This code is contributed by nitin mittal
PHP
Javascript
C++
// C++ program for to print all combination
// of 4 elements in A[] with sum equal to X
#include
using namespace std;
/* Following function is needed
for library function qsort(). */
int compare (const void *a, const void * b)
{
return ( *(int *)a - *(int *)b );
}
/* A sorting based solution to print
all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing
// order, using library function
// for quick sort
qsort (A, n, sizeof(A[0]), compare);
/* Now fix the first 2 elements
one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i+1; j < n - 2; j++)
{
// Initialize two variables as
// indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n-1;
// To find the remaining two
// elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if( A[i] + A[j] + A[l] + A[r] == X)
{
cout << A[i]<<", " << A[j] <<
", " << A[l] << ", " << A[r];
l++; r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
/* Driver code */
int main()
{
int A[] = {1, 4, 45, 6, 10, 12};
int X = 21;
int n = sizeof(A) / sizeof(A[0]);
find4Numbers(A, n, X);
return 0;
}
// This code is contributed by rathbhupendra
C
# include
# include
/* Following function is needed for library function qsort(). Refer
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/ */
int compare (const void *a, const void * b)
{ return ( *(int *)a - *(int *)b ); }
/* A sorting based solution to print all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing order, using library
// function for quick sort
qsort (A, n, sizeof(A[0]), compare);
/* Now fix the first 2 elements one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i+1; j < n - 2; j++)
{
// Initialize two variables as indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n-1;
// To find the remaining two elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if( A[i] + A[j] + A[l] + A[r] == X)
{
printf("%d, %d, %d, %d", A[i], A[j],
A[l], A[r]);
l++; r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
/* Driver program to test above function */
int main()
{
int A[] = {1, 4, 45, 6, 10, 12};
int X = 21;
int n = sizeof(A)/sizeof(A[0]);
find4Numbers(A, n, X);
return 0;
}
Java
import java.util.Arrays;
class FindFourElements
{
/* A sorting based solution to print all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing order, using library
// function for quick sort
Arrays.sort(A);
/* Now fix the first 2 elements one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i + 1; j < n - 2; j++)
{
// Initialize two variables as indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n - 1;
// To find the remaining two elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
System.out.println(A[i]+" "+A[j]+" "+A[l]+" "+A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
// Driver program to test above functions
public static void main(String[] args)
{
FindFourElements findfour = new FindFourElements();
int A[] = {1, 4, 45, 6, 10, 12};
int n = A.length;
int X = 21;
findfour.find4Numbers(A, n, X);
}
}
// This code has been contributed by Mayank Jaiswal
Python 3
# A sorting based solution to print all combination
# of 4 elements in A[] with sum equal to X
def find4Numbers(A, n, X):
# Sort the array in increasing order,
# using library function for quick sort
A.sort()
# Now fix the first 2 elements one by
# one and find the other two elements
for i in range(n - 3):
for j in range(i + 1, n - 2):
# Initialize two variables as indexes
# of the first and last elements in
# the remaining elements
l = j + 1
r = n - 1
# To find the remaining two elements,
# move the index variables (l & r)
# toward each other.
while (l < r):
if(A[i] + A[j] + A[l] + A[r] == X):
print(A[i], ",", A[j], ",",
A[l], ",", A[r])
l += 1
r -= 1
elif (A[i] + A[j] + A[l] + A[r] < X):
l += 1
else: # A[i] + A[j] + A[l] + A[r] > X
r -= 1
# Driver Code
if __name__ == "__main__":
A = [1, 4, 45, 6, 10, 12]
X = 21
n = len(A)
find4Numbers(A, n, X)
# This code is contributed by ita_c
C#
// C# program for to print all combination
// of 4 elements in A[] with sum equal to X
using System;
class FindFourElements
{
// A sorting based solution to print all
// combination of 4 elements in A[] with
// sum equal to X
void find4Numbers(int []A, int n, int X)
{
int l, r;
// Sort the array in increasing order,
// using library function for quick sort
Array.Sort(A);
/* Now fix the first 2 elements one by one
and find the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i + 1; j < n - 2; j++)
{
// Initialize two variables as indexes of
// the first and last elements in the
// remaining elements
l = j + 1;
r = n - 1;
// To find the remaining two elements, move the
// index variables (l & r) toward each other.
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
Console.Write(A[i] + " " + A[j] +
" " + A[l] + " " + A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
// Driver program to test above functions
public static void Main()
{
FindFourElements findfour = new FindFourElements();
int []A = {1, 4, 45, 6, 10, 12};
int n = A.Length;
int X = 21;
findfour.find4Numbers(A, n, X);
}
}
// This code has been contributed by nitin mittal
PHP
X
else
$r--;
}
}
}
}
// Driver Code
$A = array(1, 4, 45, 6, 10, 12);
$n = count($A);
$X = 21;
find4Numbers($A, $n, $X);
// This code is contributed
// by nitin mittal
?>
Javascript
输出:
20, 30, 40, 1
时间复杂度:O(n ^ 4)
的时间复杂度可以通过使用排序作为预处理步骤,并且然后使用此信息的方法1,以减少循环的改进,以O(N ^ 3)。
以下是详细步骤。
1)对输入数组进行排序。
2)将第一个元素固定为A [i],其中i从0到n–3。固定四元组的第一个元素后,将第二个元素固定为A [j],其中j从i + 1到n-2变化。使用本文的方法1查找O(n)时间中剩余的两个元素
以下是O(n ^ 3)解决方案的实现。
C++
// C++ program for to print all combination
// of 4 elements in A[] with sum equal to X
#include
using namespace std;
/* Following function is needed
for library function qsort(). */
int compare (const void *a, const void * b)
{
return ( *(int *)a - *(int *)b );
}
/* A sorting based solution to print
all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing
// order, using library function
// for quick sort
qsort (A, n, sizeof(A[0]), compare);
/* Now fix the first 2 elements
one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i+1; j < n - 2; j++)
{
// Initialize two variables as
// indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n-1;
// To find the remaining two
// elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if( A[i] + A[j] + A[l] + A[r] == X)
{
cout << A[i]<<", " << A[j] <<
", " << A[l] << ", " << A[r];
l++; r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
/* Driver code */
int main()
{
int A[] = {1, 4, 45, 6, 10, 12};
int X = 21;
int n = sizeof(A) / sizeof(A[0]);
find4Numbers(A, n, X);
return 0;
}
// This code is contributed by rathbhupendra
C
# include
# include
/* Following function is needed for library function qsort(). Refer
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/ */
int compare (const void *a, const void * b)
{ return ( *(int *)a - *(int *)b ); }
/* A sorting based solution to print all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing order, using library
// function for quick sort
qsort (A, n, sizeof(A[0]), compare);
/* Now fix the first 2 elements one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i+1; j < n - 2; j++)
{
// Initialize two variables as indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n-1;
// To find the remaining two elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if( A[i] + A[j] + A[l] + A[r] == X)
{
printf("%d, %d, %d, %d", A[i], A[j],
A[l], A[r]);
l++; r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
/* Driver program to test above function */
int main()
{
int A[] = {1, 4, 45, 6, 10, 12};
int X = 21;
int n = sizeof(A)/sizeof(A[0]);
find4Numbers(A, n, X);
return 0;
}
Java
import java.util.Arrays;
class FindFourElements
{
/* A sorting based solution to print all combination of 4 elements in A[]
with sum equal to X */
void find4Numbers(int A[], int n, int X)
{
int l, r;
// Sort the array in increasing order, using library
// function for quick sort
Arrays.sort(A);
/* Now fix the first 2 elements one by one and find
the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i + 1; j < n - 2; j++)
{
// Initialize two variables as indexes of the first and last
// elements in the remaining elements
l = j + 1;
r = n - 1;
// To find the remaining two elements, move the index
// variables (l & r) toward each other.
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
System.out.println(A[i]+" "+A[j]+" "+A[l]+" "+A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
// Driver program to test above functions
public static void main(String[] args)
{
FindFourElements findfour = new FindFourElements();
int A[] = {1, 4, 45, 6, 10, 12};
int n = A.length;
int X = 21;
findfour.find4Numbers(A, n, X);
}
}
// This code has been contributed by Mayank Jaiswal
的Python 3
# A sorting based solution to print all combination
# of 4 elements in A[] with sum equal to X
def find4Numbers(A, n, X):
# Sort the array in increasing order,
# using library function for quick sort
A.sort()
# Now fix the first 2 elements one by
# one and find the other two elements
for i in range(n - 3):
for j in range(i + 1, n - 2):
# Initialize two variables as indexes
# of the first and last elements in
# the remaining elements
l = j + 1
r = n - 1
# To find the remaining two elements,
# move the index variables (l & r)
# toward each other.
while (l < r):
if(A[i] + A[j] + A[l] + A[r] == X):
print(A[i], ",", A[j], ",",
A[l], ",", A[r])
l += 1
r -= 1
elif (A[i] + A[j] + A[l] + A[r] < X):
l += 1
else: # A[i] + A[j] + A[l] + A[r] > X
r -= 1
# Driver Code
if __name__ == "__main__":
A = [1, 4, 45, 6, 10, 12]
X = 21
n = len(A)
find4Numbers(A, n, X)
# This code is contributed by ita_c
C#
// C# program for to print all combination
// of 4 elements in A[] with sum equal to X
using System;
class FindFourElements
{
// A sorting based solution to print all
// combination of 4 elements in A[] with
// sum equal to X
void find4Numbers(int []A, int n, int X)
{
int l, r;
// Sort the array in increasing order,
// using library function for quick sort
Array.Sort(A);
/* Now fix the first 2 elements one by one
and find the other two elements */
for (int i = 0; i < n - 3; i++)
{
for (int j = i + 1; j < n - 2; j++)
{
// Initialize two variables as indexes of
// the first and last elements in the
// remaining elements
l = j + 1;
r = n - 1;
// To find the remaining two elements, move the
// index variables (l & r) toward each other.
while (l < r)
{
if (A[i] + A[j] + A[l] + A[r] == X)
{
Console.Write(A[i] + " " + A[j] +
" " + A[l] + " " + A[r]);
l++;
r--;
}
else if (A[i] + A[j] + A[l] + A[r] < X)
l++;
else // A[i] + A[j] + A[l] + A[r] > X
r--;
} // end of while
} // end of inner for loop
} // end of outer for loop
}
// Driver program to test above functions
public static void Main()
{
FindFourElements findfour = new FindFourElements();
int []A = {1, 4, 45, 6, 10, 12};
int n = A.Length;
int X = 21;
findfour.find4Numbers(A, n, X);
}
}
// This code has been contributed by nitin mittal
的PHP
X
else
$r--;
}
}
}
}
// Driver Code
$A = array(1, 4, 45, 6, 10, 12);
$n = count($A);
$X = 21;
find4Numbers($A, $n, $X);
// This code is contributed
// by nitin mittal
?>
Java脚本
输出 :
1, 4, 6, 10
时间复杂度: O(n ^ 3)