Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19c559c

Browse files
program to find subarray sum whose size is K
1 parent 7ea6e9a commit 19c559c

File tree

2 files changed

+85
-13
lines changed

2 files changed

+85
-13
lines changed

‎Data Structures/Trees/BST/BSTorNot.cpp‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include<iostream>
22
#include<iomanip>
3+
#include<vector>
34

45
using namespace std;
56

@@ -122,29 +123,67 @@ BSTnode *newNode(int data)
122123
return temp;
123124
}
124125

126+
void StoreInorder(BSTnode *root,vector<int>&v)
127+
{
128+
if(!root)
129+
return;
130+
131+
132+
StoreInorder(root->left,v);
133+
134+
v.push_back(root->data);
135+
136+
137+
StoreInorder(root->right,v);
138+
}
139+
140+
bool isBSTSimple(BSTnode* root) {
141+
// Your code here
142+
143+
vector<int>v; //to store the inorder traversal
144+
145+
StoreInorder(root,v);
146+
147+
int prev = v[0];
148+
for(int i = 1 ; i < v.size(); i++)
149+
{
150+
151+
if(v[i] > prev) prev = v[i];
152+
153+
else {
154+
return false;
155+
break;
156+
}
157+
}
158+
159+
return true;
160+
}
161+
125162

126163

127164
int main()
128165
{
129166

130-
BSTnode *root=newNode(7);
167+
BSTnode *root=newNode(1);
131168

132-
root->left = newNode(5);
133-
root->right = newNode(9);
134-
root->right->left = newNode(8);
135-
root->left->left = newNode(1);
136-
root->left->right = newNode(6);
137-
root->right->right = newNode(11);
138-
169+
root->left = newNode(3);
170+
root->right = newNode(2);
171+
// root->right->left = newNode(8);
172+
// root->left->left = newNode(1);
173+
// root->left->right = newNode(6);
174+
// root->right->right = newNode(11);
175+
//
139176

140177

141-
cout<<FindMin(root)->data;
142-
cout<<endl;
143-
cout<<isBST(root);
144-
cout<<endl;
178+
// cout<<FindMin(root)->data;
179+
// cout<<endl;
180+
// cout<<isBST(root);
181+
// cout<<endl;
145182

146183
//initializing prev as INT_MIN
147184
int a = INT_MIN;
148185
int *prev = &a;
149-
cout<<isBSTInorder(root,prev);
186+
// cout<<isBSTInorder(root,prev);
187+
188+
cout<<isBSTSimple(root);
150189
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
6+
//program to find maximum subarray sum whose size equals k
7+
8+
int maxSum(int arr[],int size,int k,int i=0)
9+
{
10+
int Max = 0,curMax;
11+
for(int i = 0 ; i <= (size-k) ; i++)
12+
{
13+
14+
curMax = 0;
15+
for(int j = i ; j < i+k ; j++)
16+
{
17+
curMax += arr[j];
18+
19+
if(curMax > Max)
20+
Max = curMax;
21+
}
22+
23+
}
24+
25+
return Max;
26+
}
27+
28+
29+
int main()
30+
{
31+
int arr[] =
32+
return 0;
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /