0

Suppose I have two arrays that look like this:

first array : 8 5 6 1 4 11 7
second array: 1 1 1 1 0 0 0

I want to sort the first array in descending order and the order of the elements in the second array should change in the same way as of first array and remove elements in the first array whose corresponding value in the second array is 0. The elements in the first array whose corresponding value is 0 should go into a different array. At last sum of both arrays should be printed.

so the final array should look like this:

first array : 8 6 5 1
second array: 1 1 1 1
sum= 8+6+5+1=20

new arrays with value:

first array : 11 7 4
second array: 0  0 0
sum = 11+7+4=22

Any ideas how to do this in c++

this is what i have done so far... i tried playing with waytoShort() method:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool wayToSort(int a, int b)
{
     return a > b;
}
int main()
{
int n;
int sum;
sum=0;
cout<<"no. of elements in first array: "<<endl;
cin>>n;
//no. of elements in both array should be same.
vector<int> l(n);
vector<int> t(n);

for(int i=0;i<n;i++)
{
    cin>>l[i]>>t[i];
}
sort(l.begin(),l.end(),wayToSort);

for(int i=0;i<n;i++)
{
    cout<<l[i]<<" "<<t[i]<<endl;
}

for(int j= 0; j<n;j++)
{

        sum = sum+l[j];


}
cout<<sum<<endl;
return 0;

}

This only sort the first array.

Yaman Ahlawat
  • 477
  • 6
  • 17
  • 2
    What have you tried so far? Can you show us your attempt and explain specifically where you are stuck? – Cory Kramer Jul 01 '16 at 12:55
  • 3
    I don't see why you need to sort both arrays. Why not simply first remove the elements in the first array, followed by removing all `0` elements in the second array, and finally sort the first array only? – Some programmer dude Jul 01 '16 at 12:55
  • If `second array: 0 0 0` does not that mean that `sum` should be 0? taking none – Khalil Khalaf Jul 01 '16 at 12:56
  • @FirstStep The sum is of the "first array" the "second array" – Jonathan Mee Jul 01 '16 at 12:57
  • @JonathanMee but `0`(s) indexed values should be ignored – Khalil Khalaf Jul 01 '16 at 12:58
  • Seems like you're trying to implement a `multimap` do you really need 2 vectors? – Jonathan Mee Jul 01 '16 at 12:59
  • Do you need to sort the first array? If you don't need to use the sorted array somewhere else in your code, and you are only doing addition, it's not necessary – G. May Jul 01 '16 at 13:00
  • There are endless ways to do this. But this looks like homework; if it is, then the right way is: by playing around with what you just learnt. – Francesco Dondi Jul 01 '16 at 13:00
  • By the way, if it's okay to use the standard library, [there are many functions in it that can help you](http://en.cppreference.com/w/cpp/algorithm). For example you might want to take a look at [`std::accumuate`](http://en.cppreference.com/w/cpp/algorithm/accumulate). – Some programmer dude Jul 01 '16 at 13:34

3 Answers3

1

Notice that you can split the array into two parts before sorting the array, and then sort each array alone.
Example:

8 5 6 1 4 11 7  
1 1 1 1 0  0 0  

Split into:

1) [8 5 6 1],[1,1,1,1]  
2) [4 11 17],[0,0,0] 

then sort each array alone, result:

1) [8 6 5 1],[1,1,1,1]  
2) [17 11 4],[0,0,0]  
joud
  • 13
  • 4
  • I just described the way i saw easiest to understand, and there is no problem no matter what the second array is. – joud Jul 01 '16 at 13:10
0

If I am understanding you correctly, you wish to filter a list of tuples based on the second element, and then you wish to sort a list of tuples based on the first element. I'm not detecting any vital impact on the order of operations, so you should filter first.

Representing the problem as two distinct sets of elements is probably not the way to go (this of course assumes that you are not required to use two separate lists due to some outside constraint).

In keeping with the above, you should use one list, but it should be of pairs (std::pair). Any mutations based in X will drag Y along for the ride implicitly.

As far as removing pairs whose right element == 0. That should be fairly linear. Even if you did it first, the resultant impact on your overall run-time (a single run through an array of pairs, will not be noticed when the sort is where the real heavy lifting happens anyway) will not be noticed.

Sorting pairs is fairly easy:

Sorting a std::vector<std::pair<std::string,bool>> by the string?

Choice of data representation is important. You may want to use an std::vector but since you may be removing a lot of things your performance may suffer. For a large range of datasets this could trigger massive reshuffles. In these cases, it may be better to use std::list.

Community
  • 1
  • 1
  • you are right....there is one outside constraint... in the first array..... after arranging it in descending order i also want to remove the last element in the array...in this case its 1 whose corresponding value in the second array is also 1 and then subtract the last value(i.e 1) from the sum of the both the arrays........sum1+sum2-1(the last element in the first array) ... which gives me the result....so in this case... sum1=8+6+5 sum2= 11+7+4 resultant sum = sum1+sum2-1(the last element in the first array). – Yaman Ahlawat Jul 01 '16 at 13:38
0

You're performing operations that indicate that your container type is wrong.

  1. You're sorting the "second array" based on the values of the "first array", maintaining the arrays as sorted would be preferable
  2. You're splitting the arrays based on the values of "second array" it would be nice if there was a link between them

multimap already accomplishes 1 so you'll never need to sort your keys ("first array".) Given multimap<int, int> arrays 2 can be done like this:

 multimap<int, int> newarrays;
 auto it = begin(arrays);

 while(it != end(arrays)) {
    if(it->second != 0) {
        ++it;
    } else {
        newArrays.insert(*it);
        it = arrays.erase(it);
    }
}

Live Example

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • I am sorting the first array in descending order... according to that the corresponding element in the second array should sort ... think as if the first element in first array has a relationship with the first element of the second array.... so if index of any element in first array changes... it should change the index of the corresponding element in the second array. – Yaman Ahlawat Jul 01 '16 at 14:19
  • @YamanAhlawat Yup, as I said `multimap` handles all that for you. I've updated my answer with an example, so you can see it in action. – Jonathan Mee Jul 01 '16 at 14:34