+ Reply to Thread
Results 1 to 3 of 3

Thread: Find combination of numbers

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Find combination of numbers

    Given an array of integers and a unique number. Find all different combination of numbers from the array that add up to the unique number. Print all possible combination.

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Hint: Use Dynamic Programming

  3. #3
    chithreshazad is offline Junior Member
    Join Date
    Mar 2012
    Posts
    2
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    const int size=30;
    void main()
    {
    clrscr();
    randomize();
    int a[size],unique,k=0;
    cout<<"Elements of array are:\n";
    for(int i=0;i<size;i++)
    {
    a[i]=10+random(90); //random numbers between 10-99//
    cout<<a[i]<<" ";
    }
    unique=300+random(500); //unique number between 300-799//
    cout<<"\nUnique number is: "<<unique;
    cout<<"\nDifferent combinations are:\n";
    for(int i=0;i<size;i++)
    {
    for(int j=i+1;j<size;j++)
    {
    if((a[i]+a[j])==unique)
    {
    cout<<a[i]<<" + "<<a[j]<<" = "<<unique<<"\n";
    }
    else
    {
    k=j+1;
    int temp=0;
    while(k<size)
    {
    temp+=a[k];
    if((a[i]+a[j]+temp)==unique)
    {
    int l=k;
    cout<<a[i]<<" + "<<a[j];
    while(l>=j+1)
    {
    cout<<" + "<<a[l];
    l--;
    }
    cout<<" = "<<unique<<"\n";
    }
    k++;
    }
    }
    }
    }
    getch();
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. find pair of numbers
    By Krazy in forum Google
    Replies: 3
    Last Post: 7th November 2010, 21:24
  2. Find three numbers in an array
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 16th August 2010, 15:16
  3. find largest even and odd numbers
    By admin in forum Epic Systems
    Replies: 0
    Last Post: 25th July 2010, 18:43
  4. find 2 numbers in an array
    By TopGun in forum Amazon
    Replies: 1
    Last Post: 19th June 2008, 14:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts