Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case is N and S, where N is the size of array and S is the sum. The second line of each test case contains N space separated integers denoting the array elements.
Output:
For each testcase, in a new line, print the starting and ending positions(1 indexing) of first such occuring subarray from the left if sum equals to subarray, else print -1.
Constraints:
1 <= T <= 100
1 <= N <= 107
1 <= Ai <= 1010
Example:
Input:
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output:
2 4
1 5
My Code:
#include <iostream>
using namespace std;
int main() {
int t, n, s, a[1000], result[1000];
cin >> t;
for (int i = 0; i < t; i++) {
result[i * 2] = -1;
cin >> n >> s;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
int flag = 0;
for (int j = 0; j < n; j++) {
if (flag == 0) {
int sum = 0;
for (int k = j; k < n && sum < s; k++) {
sum += a[k];
if (sum == s) {
result[i * 2] = j + 1;
result[(i * 2) + 1] = k + 1;
flag = 1;
break;
}
}
}
}
}
for (int i = 0; i < t * 2; i += 2) {
if (result[i] != -1) {
cout << result[i] << " " << result[i + 1] << endl;
} else {
cout << result[i];
}
}
}
Result:
Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input:
4 225
9 45 10 190
Its Correct output is:
-1
And Your Code's output is:
-1-1-1-138 42
I've just found this:
https://www.youtube.com/watch?v=G0ocgTgW464
However I believe that the time complexity is O(n*log(n)) given the fact that map::find is O(log(n))
Related
I have a C++ problem:
Input an sequence of digit [ 0 - 9 ] and terminated by three 9 consecutivly, print on standard output the number of subsequences consisting of three consecutive equal digits on standard output.
Example: Given the sequence { 1 2 2 2 2 0 0 3 3 3 7 9 9 9 }, the subsequence are identified:
{ 2 2 2}, { 2 2 2 }, { 3 3 3 } .
Therefore, the program should print on standard output the number 3, equal to sequences present.
I try to use an array. My code ended up like this:
int main(){
int i;
int N = 0, A[100];
while( (A[i] && A[i+1] && A[i+2]) != 9 ){
N++;
for( i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:";
cin >> A[i];
}
for(int i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:" << A[i];
}
}
}
My problem is, I have no idea how to terminate the sequence by three 9's consecutively. So I try to use an array. I hope someone can help me to elaborate the idea.
You can do that by breaking the loop when three consecutive 9 is found.
#include <iostream>
const int ARRAY_SIZE = 100;
int main(){
int i;
int N = ARRAY_SIZE, A[ARRAY_SIZE];
for( i = 0; i < ARRAY_SIZE; i++ ){
std::cout << "A[" << i + 1 << "]:";
std::cin >> A[i];
// stop when three consecutive 9 is found
if (i >= 2 && A[i - 2] == 9 && A[i - 1] == 9 && A[i] == 9){
N = i + 1;
break;
}
}
for(int i = 0; i < N; i++ ){
std::cout << "A[" << i + 1 << "]:" << A[i] << '\n';
}
return 0;
}
Instead of an array with the number of elements equal to the magic number 100 what you need is an array of exactly three elements.
Here is a demonstrative program.
#include <iostream>
int main()
{
const size_t N = 3;
int a[N];
size_t count = 0;
for ( size_t i = 0, j = 0; std::cin >> a[j++]; )
{
j %= N;
if ( i != N - 1 )
{
++i;
}
else
{
size_t k = 1;
while ( k < N && a[k] == a[k-1] ) k++;
if ( k == N )
{
if ( a[0] == 9 ) break;
else ++count;
}
}
}
std::cout << "count = " << count << '\n';
return 0;
}
If to enter the sequence of numbers
1 2 2 2 2 0 0 3 3 3 7 9 9 9
then the program output will be
count = 3
instead of the inner while loop you could use for example the algorithm std::all_of.
The algorithm is supposed to find the minimum cost path in NxN matrix given as an input. The starting cell is always left bottom and the destination is right top.
Each cell of the matrix represents a cost to traverse through that cell.
You can only move up and right.
I have managed to find the cost, however, I still struggle to backtrack the path.
I tried to start from top right cell and use the greedy algorithm to find my "way back", but the output was either completely wrong or skipping random columns/rows. I also tried to keep track of decisions I was making by creating an additional matrix, but I always end up stuck in the loop.
So how do I find the path?
Here's the code that works well (counts the cost and that's it):
#include <iostream>
using namespace std;
int main()
{
int tab[101][101], N, cost[101][101], backtrack[101][101];
cout << "N (size of NxN matrix) :" << endl;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> tab[i][j];
cost[i][j] = 0;
backtrack[i][j] = 0;
}
}
cost[N-1][0] = tab[N-1][0];
int a = N-1;
for(int i = N-2; i >= 0; i--) // column 0 can be chosen only in 1 way
{
cost[i][0] = cost[i+1][0] + tab[i][0];
backtrack[i][0] = 4; // came from down
}
for(int j = 1; j < N; j++) // row N-1 can be chosen only in 1 way
{
cost[a][j] = cost[a][j-1] + tab[a][j];
backtrack[a][j] = 3; // came from right
}
for(int i = N-2; i >= 0; i--)
{
for(int j = 1; j < N; j++)
{
if(cost[i][j-1] <= cost[i+1][j])
{
cost[i][j] = tab[i][j] + cost[i][j-1];
backtrack[i][j] = 3;
}
else
{
cost[i][j] = tab[i][j]+cost[i+1][j];
backtrack[i][j] = 4;
}
}
}
cout << "Cost: " << cost[0][a] << endl;
return 0;
}
Now, here's the function with flawed additional matrix that's supposed to give me the path, but ends up in an infinite loop:
(matrix backtrack from previous code was given as track here)
void path(int track[101][101], int N)
{
int help[101][101];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
help[i][j] = 0;
}
int w = 0, k = N-1;
help[w][k] = 1; // top right argument is included in the output
while(w < N || k >= 0)
{
if(track[w][k] == 3)
{
help[w][k-1] = 1; // 3 means I came from the previous column k-1
k--;
}
else if(track[w][k] == 4)
{
help[w+1][k] = 1; //4 means I came from the previous row w+1
w++;
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(help[i][j] != 0)
cout << i << " " << j << endl;
}
}
}
Example input:
5
2 3 4 2 5
5 2 1 2 2
2 4 2 2 3
1 2 2 4 3
3 2 1 2 3
Expected output:
Cost: 20
4 0
4 1
4 2
3 2
2 2
1 2
1 3
0 3
0 4
Actual output
Cost: 20
And no path at all since it ends up in an infinite loop.
You have written the while loop in path() incorrectly:
while(w < N || k >= 0)
...
You intend this loop to continue until w = N-1 and k=0, which it does, but the loop doesn't terminate there, it just runs in place. (You could see this yourself by adding cout << w << " " << k << endl; to the loop.) The conditional I think you want is:
while(w < N-1 || k > 0)
I'm asking for help with a problem implying an array sorting in the following manner: all even numbers must be in front of the odd ones. I've partially made the problem, but I did the sorting in the opposite manner and I can not manage to fix it.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int v[100], n, i, aux = 0, inv;
cout << "Number of elements: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
do
{
inv = 0;
for (i = 0; i < n; i++)
{
if (v[i] % 2 == 1 && v[i + 1] % 2 == 0)
{
inv = 1;
aux = v[i];
v[i] = v[i + 1];
v[i + 1] = aux;
}
}
} while (inv != 0);
cout << endl;
for (i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
system("pause");
return 0;
}
The output for this would be:
n = 8
1 3 2 4 7 8 4 2
Result: 2 4 8 4 2 -858993460 1 3
In the expression v[i + 1], you access v[n] when i = n - 1, this will result in an out-of-bounds memory access which results in undefined behaviour.
You should change the for loop to this:
for (i = 0; i < n - 1; i++)
The output for the given input is:
a.exe
Number of elements: 8
v[0]=1
v[1]=3
v[2]=2
v[3]=4
v[4]=7
v[5]=8
v[6]=4
v[7]=2
2 4 8 4 2 1 3 7
I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!
So, I have the following problem:
From the file tabl.in a number n will be read (n<=50).
After that a square array with n rows and n columns will be read; all the numbers in the array will be composed by a maximum of 2 digits each.
Shown in the file tabl.out, the modulo between the sum of numbers found on the second diagonal of the array and 10, if the sum is palindrome (true=1, false=0), and the arithmetic mean of elements situated below of the main diagonal.
Will be writing functions for:
reading the array
calculation of the operation sum of secondary diagonal%10
checking if the previous result it is palindrome
calculation of the arithmetic mean below main diagonal
Example:
tabl.in:
4
5 8 2 12
1 0 3 16
1 2 1 11
5 7 2 19
tabl.out:
2 1 3
where
(12+3+2+5)%10 = 22%10 = 2
22 is palindrome = 1
1+2+2+1+7+5 = 18, 18/6=3
My code so far is:
#include <fstream>
using namespace std;
ifstream fin("tabl.in");
ofstream fout("tabl.out");
void readn(int Arr[][51], int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
fin >> Arr[i][j];
}
int sumsec(int Arr[][51], int n) {
int s = 0;
float r;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
r = s % 10;
return r;
}
void pald(int Arr[][51], int n) {
int s = 0, pal = 0;
for (int i = 1; i < n; i++)
s = s + Arr[i][n - i + 1];
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == s)
fout << "1 ";
else
fout << "0 ";
}
int ambmd(int Arr[][51], int n) {
int s = 0, k;
float ame;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
s = s + Arr[i][j];
k++;
}
}
ame = s / k;
return ame;
}
int main() {
int Arr[51][51], n;
float r, ame;
fin >> n;
readn(Arr, n);
r = sumsec(Arr, n);
fout << r << " ";
pald(Arr, n);
ame = ambmd(Arr, n);
fout << ame;
}
But I have an issue with the palindrome() function: my output file will have 2 0 3 written to it for the given array from the example, instead of 2 1 3. What am I doing wrong?
Your pald function would work, if you compute s the same way as you do in sumsec and if s would still contain the sum, after you compute pal.
In your case, while (s != 0) {...}, followed by if (pal == s) {...} could be re-written as if (pal == 0), which is clearly not the intended solution. Just save your sum before computing pal, then compare with the saved sum.
Also, change your loop condition for computing s to for (int i = 1; i <= n; i++).
int s = 0, pal = 0, sum = 0;
for (int i = 1; i <= n; i++)
s = s + Arr[i][n - i + 1];
sum = s;
while (s != 0) {
pal = pal * 10 + s % 10;
s = s / 10;
}
if (pal == sum)
fout << "1 ";
else
fout << "0 ";
You should also consider the various comments for code improvements, like not re-computing the sum in the pald function.