Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Arrays/BasicArrayOperations/String example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include<iostream>
using namespace std;
int main()
{
char str[]={'g','f','g'};
cout<<str;
return 0;
}
60 changes: 60 additions & 0 deletions Arrays/Juggling_Algo.(Array_Rotation).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// C++ program to rotate an array by
// d elements
#include <bits/stdc++.h>
using namespace std;

/*Fuction to get gcd of a and b*/
int gcd(int a, int b)
{
if (b == 0)
return a;

else
return gcd(b, a % b);
}

/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
/* To handle if d >= n */
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;

while (1) {
int k = j + d;
if (k >= n)
k = k - n;

if (k == i)
break;

arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}

// Function to print an array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}

/* Driver program to test above functions */
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);

// Function calling
leftRotate(arr, 2, n);
printArray(arr, n);

return 0;
}
35 changes: 35 additions & 0 deletions Practice Questions/arrays/merge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//sort an array of three types of element 0's, 1's and 2's with linear time and const extra space

#include <iostream>
using namespace std;

void Segregate(int arr[], int n){
int low = 0, mid = 0, high = n-1;
while(mid<=high){
switch (arr[mid]){
case 0: swap(arr[low], arr[mid]);
low++;mid++;
break;
case 1: mid++;
break;
case 2: swap(arr[mid],arr[high]);
high--; mid++;
break;
}
}
}

int main(){
int T;
cin>>T;
while(T--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
Segregate(arr,n);
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
}
}
48 changes: 48 additions & 0 deletions Queue/checkBalanceParanthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
C++ Program to check for balanced parentheses in an expression using stack.
Given an expression as string comprising of opening and closing characters
of parentheses - (), curly braces - {} and square brackets - [], we need to
check whether symbols are balanced or not.
*/
#include<iostream>
#include<stack>
#include<string>
using namespace std;
// Function to check whether two characters are opening
// and closing of same type.
bool ArePair(char opening,char closing)
{
if(opening == '(' && closing == ')') return true;
else if(opening == '{' && closing == '}') return true;
else if(opening == '[' && closing == ']') return true;
return false;
}
bool AreParanthesesBalanced(string exp)
{
stack<char> S;
for(int i =0;i<exp.length();i++)
{
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
S.push(exp[i]);
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(S.empty() || !ArePair(S.top(),exp[i]))
return false;
else
S.pop();
}
}
return S.empty() ? true:false;
}

int main()
{
/*Code to test the function AreParanthesesBalanced*/
string expression;
cout<<"Enter an expression: ";
cin>>expression;
if(AreParanthesesBalanced(expression))
cout<<"Balanced\n";
else
cout<<"Not Balanced\n";
}