Question:

A small puzzle... Plz help me !!!

by  |  earlier

0 LIKES UnLike



Given an array of size 'N' which contains numbers

in some random order;

ur task is to return the sum of (N/2) smaller

elements;assume N is even;

for example:: if the array contains

2,5,7,6,3,4;

ur method should return 9

i.e;(2 3 4);

time constraint O(N);

 Tags:

   Report

2 ANSWERS


  1. first you need to sort the given numbers in ascending order and store it in the array for example a[n] then calculate x=n/2

    now using for loop <=x determine the sum as y=y+a[x]

    i will give you the sample c++ program below try it out

    #include<iostream.h>

    #include<conio.h>

    main()

    { clrscr( );

    int number[100],n,a,b,temp;

    cout<<"\n number ofintegers:";

    cin>>n;

    for (a=1; a<=n; a++ )

    {

    cout<<"\n number:";

    cin>>number[a];

    }

    for (a=1; a<=n; a++ )

    {

    for (b=a+1; b<=n; b++ )

    {

    if (number[b] < number[a])

    {

    temp=number[b];

    number[b]=number[a];

    number[a]=temp;

    }

    }

    }

    cout<<"\n the sorted numbers are:";

    for (a=1; a<=n; a++ )

    {

    cout <<" ";

    cout<<number[a];

    }

    int c=n/2,s=0;

    for(a=1;a<=c;a++)

    {

    s=s+number[a];

    }

    cout<<"the sum of numbers(n/2) :"<<s;

    getch ( );

    }

    replay if my answer is use full to you


  2. kehkehkeh

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.