Skip to content

Commit 2182f49

Browse files
committed
modified first_missing_number and balanced paranthesis
1 parent 14f2b0a commit 2182f49

File tree

2 files changed

+29
-49
lines changed

2 files changed

+29
-49
lines changed

Arrays/first_missing_positive.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words,
2+
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words,
33
find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
44
55
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
@@ -26,7 +26,7 @@ int firstMissingNumber(int arr[], int n)
2626
int index, temp;
2727
for(index=0;index<n;index++)
2828
{
29-
while(arr[index]!=arr[arr[index]-1] && arr[index]>=1 && arr[index]<=n)
29+
while(arr[index]>=1 && arr[index]<=n && arr[index]!=arr[arr[index]-1])
3030
{
3131
temp = arr[arr[index]-1];
3232
arr[arr[index]-1] = arr[index];
Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
1-
// Given a string of parantheses, braces and square brackets. We need to check whether they are balanced.
2-
3-
#include<iostream>
4-
#include<stack>
5-
#include<algorithm>
6-
1+
#include <bits/stdc++.h>
72
using namespace std;
3+
bool isBalanced(string);
84

9-
bool isbalanced(string s)
10-
{ stack<char> st;
11-
char x;
5+
bool isBalanced(string expr)
6+
{
7+
stack<int> st;
8+
int size = expr.length();
129

13-
for(int i=0;i<s.length();i++)
10+
for(int i=0;i<size;i++)
1411
{
15-
if(s[i]=='(' || s[i]=='{' || s[i]=='[')
16-
{
17-
st.push(s[i]);
18-
continue;
12+
if(expr[i] == '(' || expr[i] == '[' || expr[i] == '{'){
13+
st.push(expr[i]);
1914
}
20-
if (s.empty())
21-
{
22-
return false;
23-
}
24-
25-
switch (s[i])
26-
{
27-
case ')':
28-
x = st.top();
29-
st.pop();
30-
if(s[i]=='{' || s[i]=='[')
31-
return false;
32-
break;
33-
case '}':
34-
x = st.top();
35-
st.pop();
36-
if(s[i]=='(' || s[i]=='[')
37-
return false;
38-
break;
39-
case ']':
40-
x = st.top();
15+
else if(!st.empty() && ((st.top()=='(' && expr[i]==')') ||
16+
(st.top()=='[' && expr[i]==']') || (st.top()=='{' && expr[i]=='}'))){
17+
4118
st.pop();
42-
if(s[i]=='(' || s[i]=='{')
43-
return false;
44-
break;
4519
}
20+
else return false;
4621
}
47-
return st.empty();
4822

23+
return st.empty();
4924
}
5025

51-
int main(int argc, char const *argv[])
52-
{
53-
string s;
54-
cin>>s;
55-
56-
cout<<(isbalanced(s) ? "Yes" : "No");
57-
58-
return 0;
26+
int main() {
27+
//code
28+
int t;
29+
string expr;
30+
cin>>t;
31+
while(t--)
32+
{
33+
cin>>expr;
34+
if(isBalanced(expr))
35+
cout<<"balanced\n";
36+
else cout<<"not balanced\n";
37+
}
38+
return 0;
5939
}

0 commit comments

Comments
 (0)