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 af1f6eb

Browse files
program to find equivalent subarrays
1 parent b95d156 commit af1f6eb

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

‎Data Structures/LinkedLists/Merge2LLSorted.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,32 @@ Node* MergeList(Node *headA, Node *headB) {
7575
if(headA->data <= headB->data) {
7676
temp->next=headA;
7777
temp=headA;
78-
headA=temp->next;
78+
headA=headA->next;
7979
}
8080
else {
8181
temp->next=headB; //temp links to headB
8282
temp=headB;//temp becomes headB node
83-
headB=temp->next;//headB moves to next to temp
83+
headB=headB->next;//headB moves to next to temp
8484
}
8585

8686
}
8787

8888
if(headA==NULL) temp->next=headB; //when List A is finished
8989
if(headB==NULL) temp->next=headA; //when list B is finished
9090

91-
while(new_head!=NULL) {
92-
93-
cout<<new_head->data<<endl;
94-
new_head = new_head->next;
95-
}
9691

9792
return(new_head); //will return the new header of the sorted merged LL
9893
}
9994

10095

96+
void traverse(Node *head)
97+
{
98+
while(head)
99+
{
100+
cout<<head->data<<" ";
101+
head = head->next;
102+
}
103+
}
101104

102105
int main()
103106
{
@@ -139,7 +142,9 @@ int main()
139142
m4->next = NULL;
140143

141144

142-
cout<<MergeList(n1,m2);
145+
Node *new_head = MergeListsRec(n1,m1);
146+
147+
traverse(new_head);
143148

144149

145150

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include<iostream>
2+
#include<set>
3+
4+
//program to find equivalent subarrays
5+
6+
using namespace std;
7+
8+
int maxCount(int arr[],int size)
9+
{
10+
int count = 0 ;
11+
set<int>s;
12+
set<int>sub;//to store subarrays
13+
for(int i = 0 ; i < size ; i++)
14+
{
15+
s.insert(arr[i]);
16+
}
17+
18+
int dis = s.size();
19+
20+
for(int i = 0 ; i < size; i++)
21+
{
22+
23+
for(int j = i ; j < size;j++)
24+
{
25+
sub.insert(arr[j]);
26+
if(sub.size()==dis)
27+
count++;
28+
}
29+
30+
//clearing the subarray after one complete check
31+
sub.erase(sub.begin(),sub.end());
32+
33+
}
34+
cout<<count<<endl;
35+
36+
}
37+
38+
int main()
39+
{
40+
int arr[]= {2,3,2,1,3};
41+
42+
int size = sizeof(arr)/sizeof(arr[0]);
43+
44+
maxCount(arr,size);
45+
}
46+

‎Dynamic Programming/maxSumSubarrayWhoseSizeisK.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ using namespace std;
88
int maxSum(int arr[],int size,int k)
99
{
1010
int Max = 0,curMax;
11-
for(int i = 0 ; i <= (size-k) ; i++)
11+
for(int i = 0 ; i < (size-k) ; i++)
1212
{
1313

1414
curMax = 0;
15-
for(int j = i ; j < i+k ; j++)
15+
for(int j = i ; j <= k ; j++)
1616
{
1717
curMax += arr[j];
1818

0 commit comments

Comments
(0)

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