I was solving subarray with given sum,Where we have to print the starting and ending index of array if subarray with sum is found , when I tried with two test cases simultaneously i got wrong result
But when I was tried one at a time I got right answer in both.
You please also check in your IDE this is happening in every IDE.
Testcase (Simultaneously)
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output
2 4 (expected 2 4)
2 5 (But expected 1 5)
But when I tried like this for second test cases
1
10 15
1 2 3 4 5 6 7 8 9 10
Output : 1 5(As expected)
I got correct answer ,why my program this kind of weird behaviour ?
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
vector<int>a;
unordered_map<int, int>seen;
int main()
{
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
a.resize(n);
int sum = 0;
seen[0] = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (seen.find(sum - s) != seen.end()) {
int x;
x = seen[sum - s] + 2;
cout << x << " " << i + 1 << endl;
break;
}
else {
seen[sum] = i;
}
}
seen.clear();
a.clear();
//cout<<endl;
}
return 0;
}
Related
This is what the code is for:
https://www.codechef.com/LRNDSA04/problems/STACKS
Here is the code snippet:
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--)
{
int n; cin >> n;
vector<int> a;
while(n--) {
int x; cin >> x;
if(a.empty()) {
a.push_back(x);
} else {
vector<int>::iterator it = upper_bound(a.begin(), a.end(), x);
int pos = it - a.begin();
if(pos == a.size()) {
if(a[pos] > x) {
a[pos] = x;
} else {
a.push_back(x);
}
} else {
a[pos] = x;
}
}
}
cout << a.size();
for(auto e: a) {
cout << " " << e;
}
cout << "\n";
}
return 0;
}
Input to this program is:
2
6
3 4 5 1 1 2
8
14 5 13 19 17 10 18 12
The Unexpected output it generates:
3 1 1 2
3 5 10 12
If the input is changed to:
2
8
14 5 13 19 17 10 18 12
6
3 4 5 1 1 2
It shows up correct output:
4 5 10 12 18
3 1 1 2
The test case with 8 numbers as input if its position is altered in the input file. Then this behavior is observed.
When looking at the run of the code via gdb, it gives expected output for both input files, no problem then.
The output is not justified, what am I missing to see?
In this check:
if(pos == a.size()) {
if(a[pos] > x) { // this is UB
if the condition is true, then you are indexing into an invalid position of a, which invokes undefined behavior.
It seems you want to do
if(pos != a.size()) {
instead. The conventional way to check for this condition is
if(it != a.end()) {
Hi guys i've to calculate the longest sequence of numbers without any repetitions and return the size of the sub-segment.
The point is that im missing something at some point but I don't know where.
int resolverCaso() {
int num;
int cont = 0;
cin >> num;
int var;
TreeMap<int,int> a;
int aux;
int max = 0;
for (int i = 0; i < num; i++) {
cin >> var;
if (!a.contains(var)) {
a[var] = i;
aux = var;
cont++;
}
else {
if (a[aux]==i-1 && var==aux) {
cont = 1;
a = TreeMap<int, int>();
a[var] = i;
}
else {
a.erase(var);
a[var] = i;
}
}
if (cont > max) {
max = cont;
}
}
return max;
}
I've tried the following cases with this outputs and everything seems to be ok.
E:1 2 3 1 2 3 O:3
E:2 2 2 2 O:1
E:4 5 6 7 6 O:4
E:7 8 9 10 7 8 9 11 2 O:6
E:7 8 9 10 10 10 1 2 3 4 O:5
E:3 4 2 3 4 2 8 9 10 11 O:7
E:0 O:0 ( empty vector ).
E:1 O:1
So basically im looking for some sequence that doesn't work with my code.
Thanks.
The problem is with
else {
a.erase(var);
a[var] = i;
}
You need to do more here. Try the sequence 1 3 4 2 3 4 2 8 9 10 11.
The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?
#include <iostream>
using namespace std;
int height(int n) {
int h[61],i;
h[0]=1;
for(i=1;i<61;i++)
{ if (i%2!=0)
h[i]=h[i-1]*2;
else h[i]=h[i-1]+1;
}
cout<<h[n];
return 0;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cout << height(n) << endl;
}
}
Here is a simple solution that avoids too much calculations.
Notice the following:
n: Height: Hint:
0 1 2^0
1 2*1=2 2^1
2 2+1=3 2^2 -1
3 2*3=6 2^3 -2
4 6+1=7 2^3 -1
5 2*7=14 2^4 -2
6 14+1=15 2^4 -1
7 2*15=30 2^5 -2
8 30+1=31 2^5 -1
9 2*31=62 2^6 -2
10 62+1=63 2^6 -1
11 2*63=126 2^7 -2
12 126+1=127 2^7 -1
13 2*127=254 2^8 -2
14 254+1=255 2^8 -1
15 2*255=510 2^9 -2
16 510+1=511 2^9 -1
and so on and so forth...
That means that we can use bit shift and avoid for loops etc. Here is a simple solution:
int main(){
//number of test cases
int t;
cin >> t;
for(int i= 0; i< t; ++i){
//number of cycles for the tree growth
int n;
cin >> n;
if (n == 0)
cout << 1 << endl;
else if (n == 1)
cout << 2 << endl;
else if (n > 1){
cout << ((1 << ( (n & 1) ? ((n+1)/2) : (n/2)) + 1) - ((n & 1) ? 2 : 1)) << endl;
}
}
return 0;
}
The key is to categorize number of cycles in odd and even and process accordingly.
You should return h[n] instead of 0 here:
int height(int n) {
int h[61],i;
h[0]=1;
for(i=1;i<61;i++)
{ if (i%2!=0)
h[i]=h[i-1]*2;
else h[i]=h[i-1]+1;
}
//cout<<h[n]<<endl;
return h[n];
}
Also, note that it was printing 10 and 20 because the value of h[n] was actually 1 and 2 itself but the function was returning 0 itself.
Due to the statement:
cout << h[n];
It was printing 1(and 2 for next call) and then due to statement:
cout<<height(n);
it was printing the 0(and 0 for next call). Thus, it was printing 1 and 0 for height[0] and then printing 2 and 0 for height[0] .
Summary:
Return h[n] from function instead of 0.
Also, use tab or newline with cout to minimize the confusion.
Why to print the value in both height() and main(). Simply do it in main()
The code should produce 10 and 20 based on your program if you enter 2 0 1. You might want to have a look at what you output and what you return from your function plus what you do with the result.
Note that you should also always check that your inputs were actually successful e.g. using if (std::cin >> T) { ... }. However, that's not the problem in your code.
The function int height(int ) should return h[n]; instead of printing h[n] (cout << h[n];) and then return 0 (which adds 0 next to your desired output of 1,2). Using your code, you should modify it as follows in order to make it work:
#include <iostream>
using namespace std;
int height(int n) {
int h[61],i;
h[0]=1;
for(i=1;i<61;i++)
{ if (i%2!=0)
h[i]=h[i-1]*2;
else h[i]=h[i-1]+1;
}
//cout<<h[n];
return h[n];
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cout << height(n) << endl;
}
}
How about array of functions?
typedef int(*FunctionPrt)(int);
int utopianTree(int n) {
int initialHeight = 1;
FunctionPrt functions[] = {
[](int initialHeight) { return initialHeight * 2; },
[](int initialHeight) { return initialHeight + 1; }
};
for (int cycles = 0, i = 0; cycles < n; i = (i + 1) % 2, ++cycles) {
initialHeight = functions[i](initialHeight);
}
return initialHeight;
}
I want to write a code that print a table like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
I wrote a code to print a table as said above, but it just print 5's.
I know that I have to use a condition to print such a table. What's the condition to print it ?
int main () {
int number = 5;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (condition)
...
else
cout << number << " ";
}
cout << endl;
}
return 0;
}
As I mentioned in comments, what you want to print is the Chebyshev distance to the center +1. I dont know what condition can make your code work, but instead I would use a simple formula to calculate each value:
#include <iostream>
using namespace std;
int main() {
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
cout << std::max(abs(i-4),abs(j-4)) +1 << " " ;
}
cout << endl;
}
}
/*To make boxes and inside box and so on
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[100][100],n,i,j,k=0,l;
clrscr();
cout<<"Enter the outside No. n \n";
//like for your answer it is 5;
//i and j are loop element for 2D array
//k is used for making n boxes
cin>>n;
l=n;
n=2*n-1;
while(k<n)
{
if(k%2==0)
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
else
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout << a[i][j];
if(a[i][j]>9)
cout<<" ";
else
cout<<" ";
}
cout<<endl;
}
getch();
}
I tried solving this interview question. My code runs for test cases but fails for all the real input test cases. I tried hard to find the mistake but unable to do so. Please find my code below the question
Bob loves sorting very much. He is always thinking of new ways to sort an array.His friend Ram gives him a challenging task. He gives Bob an array and an integer K. The challenge is to produce the lexicographical minimal array after at most K-swaps. Only consecutive pairs of elements can be swapped. Help Bob in returning the lexicographical minimal array possible after at most K-swaps.
Input: The first line contains an integer T i.e. the number of Test cases. T test cases follow. Each test case has 2 lines. The first line contains N(number of elements in array) and K(number of swaps). The second line contains n integers of the array.
Output: Print the lexicographical minimal array.
Constraints:
1 <= T <= l0
1 <= N,K <= 1000
1 <= A[i] <= 1000000
Sample Input (Plaintext Link)
2
3 2
5 3 1
5 3
8 9 11 2 1
Sample Output (Plaintext Link)
1 5 3
2 8 9 11 1
Explanation
After swap 1:
5 1 3
After swap 2:
1 5 3
{1,5,3} is lexicographically minimal than {5,1,3}
Example 2:
Swap 1: 8 9 2 11 1
Swap 2: 8 2 9 11 1
Swap 3: 2 8 9 11 1
#include <iostream>
using namespace std;
void trySwap(int a[], int start, int end, int *rem)
{
//cout << start << " " << end << " " << *rem << endl;
while (*rem != 0 && end > start)
{
if (a[end - 1] > a[end])
{
swap(a[end - 1], a[end]);
(*rem)--;
end--;
}
else end--;
}
}
void getMinimalLexicographicArray(int a[], int size, int k)
{
int start , rem = k, window = k;
for (start = 0; start < size; start++)
{
window = rem;
if (rem == 0)
return;
else
{
//cout << start << " " << rem << endl;
int end = start + window;
if (end >= size)
{
end = size - 1;
}
trySwap(a, start, end, &rem);
}
}
}
int main()
{
int T, N, K;
int a[1000];
int i, j;
cin >> T;
for (i = 0; i < T; i++)
{
cin >> N;
cin >> K;
for (j = 0; j < N; j++)
{
cin >> a[j];
}
getMinimalLexicographicArray(a, N, K);
for (j = 0; j < N; j++)
cout << a[j] << " ";
cout << endl;
}
return 0;
}
Python solution can be easily translated to C++:
def findMinArray(arr, k):
i = 0
n = len(arr)
while k > 0 and i < n:
min_idx = i
hi = min(n, i + k + 1)
for j in range(i, hi):
if arr[j] < arr[min_idx]:
min_idx = j
for j in range(min_idx, i, -1):
arr[j - 1], arr[j] = arr[j], arr[j - 1]
k -= min_idx - i
i += 1
return arr
Here are two failed test cases.
2
2 2
2 1
5 3
3 2 1 5 4
In the first, your code makes no swaps, because K >= N. In the second, your code swaps 5 and 4 when it should spend its third swap on 3 and 2.
EDIT: the new version is still too greedy. The correct output for
1
10 10
5 4 3 2 1 10 9 8 7 6
is
1 2 3 4 5 10 9 8 7 6
.