Question

In: Computer Science

How would you find if numbers in an array element add up to a certain sum...

How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of:

INPUT: 200, 10, 50, 20

and my output asks if these elements add to:

210? YES (200+10)

260? YES (200+10+50)

30? NO

What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist in O(n^2)?

Solutions

Expert Solution

//File handling concept of C++ is used to look throug elements in an array inputted through file.

//run this on latest compiler since unordered_map is used
/*since for 30 it is showing No that means we are searching for subarray
beacuse if asked for subset the for 30 it should give Yes as result because 20 and 10 are there in array
*/

//Time complexity of this program=O(nlogn) which is better than O(n^2)


#include <fstream>
#include <iostream>
#include <sstream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
ifstream fin;
char filename[20];
//the file will have oly elements which are to be present in array
cout<<"Enter The File Name With Extension\n";
cin>>filename;
vector<int>vec;
fin.open(filename);

if (!fin)
{
cerr << "File not found." << endl;
return -1;
}
  
string line;
  
getline(fin, line);

istringstream iss(line);
int sum = 0, next = 0;
while (iss >> next)
vec.push_back(next);
fin.close();

cout<<"Enter required Sum :";
int k;
cin>>k;
int n=vec.size();
//If any subarray exists with sum=k then print Yes else No
unordered_map<int, int> prevSum;

int res = 0;

// Sum of elements so far.
int currsum = 0;

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

// Add current element to sum so far.
currsum += vec[i];

// If currsum is equal to desired sum,
// then a new subarray is found. So
// increase count of subarrays.
if (currsum == sum)
res++;

// currsum exceeds given sum by currsum
// - sum. Find number of subarrays having
// this sum and exclude those subarrays
// from currsum by increasing count by
// same amount.
if (prevSum.find(currsum - sum) != prevSum.end())
res += (prevSum[currsum - sum]);

// Add currsum value to count of
// different values of sum.
prevSum[currsum]++;
}
if(res>0)
cout<<"YES\n";
   else
   cout<<"No\n";
return 0;

}


Related Solutions

In the java programming language. How would you find if numbers in an array element add...
In the java programming language. How would you find if numbers in an array element add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 210? YES (200+10) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums exist...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
IN JAVA Create an array and add random values to it, and then find the sum...
IN JAVA Create an array and add random values to it, and then find the sum of the values using recursion.
In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
4. Two fair dices are rolled. Find the probability that sum of the numbers add to...
4. Two fair dices are rolled. Find the probability that sum of the numbers add to 7. 5. A survey of freshman at Fishtopia University indicated that 70% enjoy playing Oceanic Adventures while only 35% enjoy playing Fishmania. The survey also indicated that 15% enjoy playing both games. a) Complete the Venn Diagram by filling in all four regions with the appropriate percentage values. Make sure that all four regions add up to 100%. b) What is the probability that...
Overflow. Find out what happens when you try to add two numbers whose sum exceeds the...
Overflow. Find out what happens when you try to add two numbers whose sum exceeds the maximum integer value (just over two billion, one hundred million). Test two billion plus two billion. What result did you get? Explain why overflow happens on a computer. Explain inexact results on integer division such as: 2 / 5 = 0 and 51 / 25 = 2
goal of this function will be to add an element to an already existing array in...
goal of this function will be to add an element to an already existing array in C/C++. bool add(structure dir[], int& size, const char nm[], const char ph[]){ //code here } add = function name structure = structure with two fields { char name[20]; char phone[13]; } int& size = # of entries in dir nm = name that's going to be added ph = phone that's going to be added. return true if entry was successfully added, false if...
Problem 6.3 Sum of Numbers - Similar to Problems 6.1 & 6.2, you must sum up...
Problem 6.3 Sum of Numbers - Similar to Problems 6.1 & 6.2, you must sum up 5 numbers. However, you will obtain these numbers from a list box for which you have used the String Collection Editor to insert a number on each of 5 lines in the box. You may use any type of loop. The result is displayed in a text box. The program is initiated with "Go" button. Problem 6.3 does NOT require an Input Dialog Box....
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT