diff --git a/Arrays/BasicArrayOperations/String example.cpp b/Arrays/BasicArrayOperations/String example.cpp new file mode 100644 index 0000000..17ebb18 --- /dev/null +++ b/Arrays/BasicArrayOperations/String example.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; +int main() +{ + char str[]={'g','f','g'}; + cout< +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; +} diff --git a/Practice Questions/arrays/merge.cpp b/Practice Questions/arrays/merge.cpp new file mode 100644 index 0000000..ccf4b75 --- /dev/null +++ b/Practice Questions/arrays/merge.cpp @@ -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 +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>arr[i]; + } + Segregate(arr,n); + for(int i=0;i +#include +#include +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 S; + for(int i =0;i>expression; + if(AreParanthesesBalanced(expression)) + cout<<"Balanced\n"; + else + cout<<"Not Balanced\n"; +} \ No newline at end of file