This program is written in C++
The purpose of this program is to read set of numbers that is <= 100,000 into an array and then sum up consecutive numbers and test if they are a multiple of N. Once sum % N == 0 is true then the program outputs the indices of i and j showing exactly from which positions i through j sum to be a multiple of N. There could be multiple solutions, but only one is required so once a solution is found the bool variable is set to true to exit the loop.
My code does correctly calculate a single solution, however, it doesn't do it efficiently enough. Furthermore, I am told that the use of an unsigned sum variable is not necessary as a standard int sum variable is enough to satisfy the requirements of this program. Without the unsigned version of sum I encountered overflow that seemed to ruin my calculations as each test case does contain 100,000 combinations of integers.
Any help or direction is appreciated. Thank you!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int testCases = 0, n = 0;
unsigned int sum = 0;
const int arraySize = 100000;
int array[arraySize];
ofstream outFile ("output.txt");
ifstream inFile ("input.txt");
inFile >> testCases;
while(testCases > 0)
{
bool found = false;
inFile >> n;
for(int a = 0; a < n; a++)
inFile >> array[a];
for(int i = 0; i < n && !found; ++i)
for(int j = i; j < n && !found; ++j)
{
sum = 0;
for(int d = i; d <= j; d++)
sum += array[d];
if (sum % n == 0)
{
outFile << i << ' ' << j << endl;
found = true;
}
}
testCases--;
}
outFile.close();
inFile.close();
return 0;
}