Given a special set of numbers and a special sum. Find all those combinations from the given set, equaling the given sum
Input :
First Input : Set of numbers
Second Input : Special Sum
Output :
Combinations satisfying criteria
Sample Testcases :
I/P 1:
-1, 1, 0, 0, 2, -2
0
O/P 1
3
Explanation : Following combinations are satisfying (-1,1,2,-2), (0, 0, 1, -1), (0, 0, -2, 2)
Note : Make combinations with 4 integers only.
Solution :
import itertools
l=list(map(int,input().split(",")))
s=int(input())
res=list(itertools.combinations(l,4))
c=0
for i in res:
u=sum(i)
if u == s:
c+=1
print(c)

/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
String sstr=”-1,1,0,0,2,-2″;
int sum=0;
String[] str= sstr.split(“,”);
int[] arr=new int[str.length];
for(int k=0;k<str.length;k++)
arr[k]=Integer.parseInt(str[k]);
int count=0;
for(int i=0;i<arr.length;i++){
for(int j=i+1;jsum)
break;
}
}
System.out.println(count);
}
}
import java. io.*;
import java. util.*;
import java. lang.*;
public class Main
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
String t = Sc.nextLine();
String k[] = t.split(“,”);
//System.out.println(k.length);
int c=0;
int sum = Sc.nextInt();
int i,j,kk,l;
for(i=0;i<k.length-3;i++)
{
for(j=i+1;j<k.length-2;j++)
{
for(kk=j+1;kk<k.length-1;kk++)
{
for(l=kk+1;l<k.length;l++)
{ int r = Integer.parseInt(k[i]);
int r1 = Integer.parseInt(k[j]);
int r2 = Integer.parseInt(k[kk]);
int r3 = Integer.parseInt(k[l]);
//System.out.println((r+r1+r2+r3));
if((r+r1+r2+r3)==sum)
c++;
}}}}
System.out.println(c);}
}