diff --git a/BinarySearch_Recursive.c b/BinarySearch_Recursive.c new file mode 100644 index 0000000..9e2af0e --- /dev/null +++ b/BinarySearch_Recursive.c @@ -0,0 +1,32 @@ +//Recursive Binary Search Implementation in C. +#include +int BinarySearch(long long int arr[],long long int low,long long int high,long long int key){ + if(high < low){ + return -1; + } + long long int mid = low + (high-low)/2; + if(arr[mid]==key) + return mid; + else if(key < arr[mid]){ + return BinarySearch(arr,low,mid-1,key); + } + else + return BinarySearch(arr,mid+1,high,key); +} +int main(){ + long long int n,k,i; + scanf("%lld",&n); + long long int a[n]; + for(i=0;i +using namespace std; + +int main(){ + +}//{ Driver Code Starts +#include +using namespace std; + +// } Driver Code Ends +class Solution{ + public: + //Function to count the frequency of all elements from 1 to N in the array. + void frequencyCount(vector& arr,int N, int P) + { + // code here + int hash[N]={0}; + int i=0; + while(i& arr,int N, int P) + { + // code here + int i=0; + while(i=N){ + arr[i]=0; + i++; + } + //If element at correct index/position of arr[i] is greater than 0 that means its not the occurrence frequency count. Its the element for which we have to count occurrence. + else if(arr[elementIndex] >0){ + //If the elementIndex has an element that is not + // processed yet, then first store that element + // to arr[i] so that we don't lose anything. + arr[i] = arr[elementIndex];//There can be some other element at the correct index/position of arr[i] so putting the element arr[elementIndex] at arr[i] + // After storing arr[elementIndex], change it + // to store initial count of 'arr[i]' + arr[elementIndex] = -1; + } + else{ + // If this is NOT first occurrence of arr[i], + // then decrement its count. + arr[elementIndex]--; + // And initialize arr[i] as 0 means the element + // is not seen so far + arr[i] = 0; + //Increment the pointer i only when arr[i] is <=0 that means at i th position occurrence of element is calculated. + i++; + } + } + //To make all the frequency count to positive numbers as required in output. + for(int i=0;i> t; + + while(t--){ + + int N, P; + //size of array + cin >> N; + + vector arr(N); + + //adding elements to the vector + for(int i = 0; i < N ; i++){ + cin >> arr[i]; + } + cin >> P; + Solution ob; + //calling frequncycount() function + ob.frequencyCount(arr, N, P); + + //printing array elements + for (int i = 0; i < N ; i++) + cout << arr[i] << " "; + cout << endl; + } + return 0; +} + +// } Driver Code Ends diff --git a/Valid_Palindrome.cpp b/Valid_Palindrome.cpp new file mode 100644 index 0000000..e6a4561 --- /dev/null +++ b/Valid_Palindrome.cpp @@ -0,0 +1,81 @@ +/* +LeetCode Problem link : https://leetcode.com/problems/valid-palindrome/ +Valid Palindrome + +A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. + +Given a string s, return true if it is a palindrome, or false otherwise. + + + +Example 1: + +Input: s = "A man, a plan, a canal: Panama" +Output: true +Explanation: "amanaplanacanalpanama" is a palindrome. +Example 2: + +Input: s = "race a car" +Output: false +Explanation: "raceacar" is not a palindrome. +Example 3: + +Input: s = " " +Output: true +Explanation: s is an empty string "" after removing non-alphanumeric characters. +Since an empty string reads the same forward and backward, it is a palindrome. + + +Constraints: + +1 <= s.length <= 2 * 105 +s consists only of printable ASCII characters. +*/ +#include +using namespace std; + +class Solution { +public: + bool isPalindrome(string s) { + int l=0,r = s.size()-1; + while(l which is a c style library function. +isalnum() +isblank() +isspace() +ispunct() +tolower() +toupper() + +are some functions in this library. +*/ diff --git a/gcd.c b/gcd.c new file mode 100644 index 0000000..f7727f4 --- /dev/null +++ b/gcd.c @@ -0,0 +1,15 @@ +#include +int gcd(long int a,long int b){ + long int c = a%b; + if(c!=0){ + gcd(b,c); + } + else{ + printf("%ld",b); + } +} +int main(){ + long int a,b; + scanf("%ld %ld",&a,&b); + gcd(a,b); +}