Header Ads Widget

Possible Combinations | coding ninjas

Possible Combinations

You are given N integers (12<N<20) in sorted order and your task is to print all the possible combinations of the numbers that can be used to choose 12 numbers out of the given N numbers in sorted order.

Input Format:

First line contains N - (Integer)
Second line contains N spaced integers which are player Id of players. Note that, all the N integers are given in sorted order.

Output Format:

Print the space separated combinations in N lines

Constraints

12<= N <= 20
1 <= Ai <=1000

Sample Input :

13
1 2 3 4 5 6 7 8 9 10 11 12 13

Sample Output :

1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 13 
1 2 3 4 5 6 7 8 9 10 12 13 
1 2 3 4 5 6 7 8 9 11 12 13 
1 2 3 4 5 6 7 8 10 11 12 13 
1 2 3 4 5 6 7 9 10 11 12 13 
1 2 3 4 5 6 8 9 10 11 12 13 
1 2 3 4 5 7 8 9 10 11 12 13 
1 2 3 4 6 7 8 9 10 11 12 13 
1 2 3 5 6 7 8 9 10 11 12 13 
1 2 4 5 6 7 8 9 10 11 12 13 
1 3 4 5 6 7 8 9 10 11 12 13 
2 3 4 5 6 7 8 9 10 11 12 13

 Code(Python):-

def printcmb(arr,n,r):
    data=[0]*r 
    combinations(arr,data,0,n-1,0,r)
def  combinations(arr,data,start,end,index,r):
    if index==r:
        for j in range(r):
            print(data[j],end=" ")
        print()
        return ;
    i=start 
    while (i<=end and end-i+1>=r-index):
        data[index]=arr[i]
        combinations(arr,data,i+1,end,index+1,r)
        i+=1 


n=int(input())
l=list(map(int,input().split()))
printcmb(l,n,12)

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments