├── .vscode └── settings.json ├── GFG TCS Sheet ├── 01]. Problems on Array │ ├── 08_Reverse_an_array_or_string.cpp │ ├── 09_Mean_Median_Mode_of_an_unsorted_array.cpp │ ├── 10_Smallest_and_Second_smallest_element_in_the_array.cpp │ ├── 11_Largest_Element_in_an_array.cpp │ ├── 12_Second_largest_element_in_an_array.cpp │ ├── 14_Program_for_addition_of_two_matrices.cpp │ ├── 16_Sum_of_elements_of_in_a_given_array.cpp │ ├── 17_Remove_duplicate_elements_from_sorted_Array.cpp │ ├── 21_Average_of_an_array.cpp │ ├── 23_Find_duplicates_in_array_in_O(n)_time.cpp │ └── README.md ├── 02]. Problems on String │ ├── !01_Count_vowels_consonents_digits_and_special_characters_in_a_string.cpp │ ├── 02_Given_string_is_palindrome_or_not.cpp │ ├── 03_ASCII_value_of_a_Character.cpp │ ├── 04_Remove_vowels_from_a_string.cpp │ ├── 05_Remove_spaces_from_a_given_string.cpp │ ├── 14_Strings_are_anagram_of_each_other.cpp │ ├── 15_Sort_string_of_characters.cpp │ └── README.md ├── 03]. Problems on Number Series │ ├── 01_Armstrong_numbers.cpp │ ├── 05_Replace_all_o's_with_5.cpp │ ├── 06_Number_is_positive_negative_odd_even_zero.cpp │ ├── 07_Harshad_Number.cpp │ ├── 12_Reverse_digits_of_a_number.cpp │ ├── 14_Sum_of_digits_of_number.cpp │ ├── 19_Perfect_number.cpp │ ├── 20_Palindrome_in_a_given_range.cpp │ ├── 22_Abundant_Number.cpp │ ├── 23_Sum_of_first_N_natural_numbers.cpp │ ├── 24_Sum_of_natural_numbers_in_range_L_to_R.cpp │ ├── 29_Check_if_a_number_is_palondrome_or_not.cpp │ ├── 30_Largest_and_smallest_digit_of_a_Number.cpp │ ├── 34_Area_of_Circle_Reclangle_and_Right_angled_Trinagle.cpp │ └── README.md ├── 04]. Problems on Conversions │ └── README.md ├── 05]. Sorting Algorithms │ ├── 01_Bubble_Sort.cpp │ ├── 03_Insertion_sort.cpp │ └── README.md └── README.md ├── README.md └── Striver TCS Sheet ├── 01]. Arrays ├── 01_Find_the_smallest_number_in_an_array.cpp ├── 02_Find_the_Largest_element_in_an_array.cpp ├── 03_Find_Second_Smallest_and_Second_Largest_Element_in_an_array.cpp ├── 04_Reverse_a_given_Array.cpp ├── 05_Count_frequency_of_each_element_in_the_array.cpp ├── 06_Rearrange_array_in_increasing_decreasing_order.cpp ├── 07_Calculate_Sum_of_the_Elements_of_the_Array.cpp ├── 09_Average_of_all_the_elements_in_the_array.cpp ├── 10_Find_Median_of_the_given_Array.cpp ├── 11_Remove_Duplicates_in-place_from_Sorted_Array.cpp ├── 12_Remove_duplicates_from_unsorted_array.cpp ├── 13_Adding_Element_in_an_Array.cpp ├── 14_Find_all_repeating_elements_in_an_array.cpp ├── 15_Find_all_the_non_repeating_elements_in_an_array.cpp ├── 17_Maximum_Product_Subarray_in_an_Array.cpp ├── 18_Replace_elements_by_its_rank_in_the_array.cpp ├── 19_Sort_Elements_of_an_Array_by_Frequency.cpp ├── 24_Search_an_Element_in_an_Array.cpp ├── README.md └── tempCodeRunnerFile.cpp ├── 02]. Problems_on_Numbers ├── 01_Check_if_a_number_is_Palindrome_or_Not.cpp ├── 03_Check_if_a_number_is_prime_or_not.cpp ├── 04_Prime_Numbers_in_a_given_range.cpp ├── 06_Check_whether_a_number_is_Perfect_Number_or_not.cpp ├── 07_Check_whether_a_given_number_is_even_or_odd.cpp ├── 08_Check_whether_a_number_is_positive_or_negative.cpp ├── 09_Sum_of_first_N_Natural_Numbers.cpp ├── 12_Greatest_of_two_numbers.cpp ├── 13_Greatest_of_three_numbers.cpp ├── 15_Reverse_digits_of_a_number.cpp ├── 16_Maximum_and_Minimum_digit_in_a_number.cpp ├── 17_Print_Fibonacci_Series_up_to_Nth_term.cpp ├── 18_Factorial_of_a_Number.cpp ├── 19_Calculate_the_Power_of_a_Number.cpp ├── 21_Print_all_Prime_Factors_of_the_given_number.cpp ├── 22_Check_if_a_number_is_a_Strong_Number_or_not.cpp ├── 26_Check_if_the_given_number_is_Harsha_Or_Niven_Number.cpp ├── 27_Check_if_the_number_is_an_abundant_number_or_not.cpp ├── 28_Sum_Of_Digits_of_A_Number.cpp ├── 29_Find_the_sum_of_numbers_in_the_given_range.cpp ├── 32_Replace_all_the_0s_with_1_in_a_given_integer.cpp ├── 33_Express_given_number_as_Sum_of_Two_Prime_Numbers.cpp ├── 34_Calculate_the_Area_of_the_Circle.cpp ├── README.md └── tempCodeRunnerFile.cpp ├── 03]. Problems_on_Number_System ├── 01_Convert_Binary_to_Decimal.cpp ├── 02_Binary_Number_to_Octal_Number.cpp ├── 03_Convert_Decimal_to_Binary_Number.cpp ├── 04_Decimal_to_octal_conversion.cpp ├── 05_Octal_to_Binary_numbers.cpp ├── 06_Octal_to_decimal.cpp ├── README.md └── tempCodeRunnerFile.cpp ├── 04]. Problems_on_Sorting ├── 01_Bubble_Sort.cpp ├── 02_Insertion_Sort.cpp ├── 03_Selection_Sort.cpp ├── 04_Quick_Sort.cpp ├── 05_Merge_Sort.cpp └── README.md ├── 05]. Problems_on_String ├── 01_Check_if_the_given_String_is_Palindrome_or_not.cpp ├── 02_Count_number_of_vowels_and_consonants_and_spaces_in_String.cpp ├── 03_Find_the_ASCII_value_of_a_character.cpp ├── 04_Remove_all_vowels_from_the_String.cpp ├── 05_Remove_Spaces_from_a_String.cpp ├── 06_Remove_characters_from_a_string_except_alphabets.cpp ├── 07_Reverse_a_String.cpp ├── 08_Remove_brackets_from_an_algebraic_expression.cpp ├── 10_Capitalize_first_and_last_character_of_each_word_of_a_string.cpp ├── 11_Calculate_Frequency_of_characters_in_a_String.cpp ├── 12_Find_Non_repeating_characters_of_a_String.cpp ├── 13_Check_if_two_Strings_are_anagrams_of_each_other.cpp ├── 16_Maximum_occurring_character_in_a_string.cpp ├── 17_Remove_All_Duplicates_from_a_String.cpp ├── 18_Print_all_the_duplicates_in_the_string.cpp ├── 19_Remove_Characters_from_first_String_present_in_the_Second_String.cpp ├── 20_Change_every_letter_with_next_lexicographic_alphabet.cpp ├── 22_Write_a_program_to_sort_characters_in_a_string.cpp ├── 23_Count_the_number_of_words_in_a_given_string.cpp ├── 25_Change_case_of_each_character_in_a_string.cpp ├── 26_Concatenate_one_string_to_another.cpp └── README.md └── README.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "unordered_map": "cpp", 4 | "ostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /GFG TCS Sheet/01]. Problems on Array/08_Reverse_an_array_or_string.cpp: -------------------------------------------------------------------------------- 1 | //User function Template for C++ 2 | 3 | string reverseWord(string str){ 4 | int l=0; 5 | int r=str.length()-1; 6 | 7 | while(l 20 | using namespace std; 21 | 22 | double getMean(double a[], int n){ 23 | double sum=0; 24 | for(int i=0; i minAnd2ndMin(int a[], int n) { 28 | int s1=INT_MAX, s2=INT_MAX; 29 | sets(a, a+n); 30 | if(s.size()<=1){ 31 | return {-1}; 32 | } 33 | 34 | 35 | for(int i=0; i 16 | #include 17 | using namespace std; 18 | 19 | int getLargest(int arr[], int n){ 20 | int lar=INT_MIN; 21 | for(int i=0; i 25 | using namespace std; 26 | 27 | int getSecondLargest(int arr[], int n){ 28 | sets(arr, arr+n); 29 | if(s.size()==1){ 30 | return -1; 31 | } 32 | vectorv(s.begin(), s.end()); 33 | return v[s.size()-2]; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[]{12, 35, 1, 10, 34, 1}; 39 | cout< >& matrixA, vector>&matrixB) { 7 | // Code here 8 | int m=matrixA.size(); 9 | int n=matrixB.size(); 10 | 11 | for(int i=0; is(a, a+n); 9 | vectorv(s.begin(), s.end()); 10 | for(int i=0; i duplicates(int arr[], int n) { 4 | // code here 5 | vectorv; 6 | sets; 7 | unordered_mapmp; 8 | for(int i=0; i1){ 14 | v.push_back(x.first); 15 | } 16 | } 17 | 18 | if(v.size()==0){ 19 | v.push_back(-1); 20 | } 21 | for(auto x: v){ 22 | s.insert(x); 23 | } 24 | v.clear(); 25 | for(auto x: s){ 26 | v.push_back(x); 27 | } 28 | return v; 29 | } 30 | }; -------------------------------------------------------------------------------- /GFG TCS Sheet/01]. Problems on Array/README.md: -------------------------------------------------------------------------------- 1 | # Problems on Array -------------------------------------------------------------------------------- /GFG TCS Sheet/02]. Problems on String/!01_Count_vowels_consonents_digits_and_special_characters_in_a_string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space. 3 | Examples: 4 | 5 | 6 | Input : str = "geeks for geeks121" 7 | Output : Vowels: 5 8 | Consonant: 8 9 | Digit: 3 10 | Special Character: 2 11 | 12 | Input : str = " A1 B@ d adc" 13 | Output : Vowels: 2 14 | Consonant: 4 15 | Digit: 1 16 | Special Character: 6 17 | */ 18 | 19 | #include 20 | using namespace std; 21 | 22 | void getCount(string s){ 23 | int count_vowels=0; 24 | int count_consonents=0; 25 | int count_digits=0; 26 | 27 | for(int i=0; i='9'){ 29 | count_digits++; 30 | } 31 | else if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){ 32 | count_vowels++; 33 | }else 34 | { 35 | count_consonents++; 36 | } 37 | } 38 | cout<<"digits : "< 16 | using namespace std; 17 | 18 | int get_ASCII_value(char c){ 19 | return c; 20 | } 21 | 22 | int main(){ 23 | cout<v; 10 | 11 | while(temp>0){ 12 | v.push_back(temp%10); 13 | temp/=10; 14 | } 15 | for(int i=0; iv; 8 | while(n>0){ 9 | v.push_back(n%10); 10 | n/=10; 11 | } 12 | reverse(v.begin(), v.end()); 13 | for(int i=0; i 8 | using namespace std; 9 | 10 | // void getOddOrEven(int &num); 11 | // void checkNumber(int &num); 12 | void getOddEven(int &num){ 13 | if(num%2==0){ 14 | cout<<" Even "<0){ 22 | cout<<" Positive "; 23 | }else if(num<0){ 24 | cout<<" Negative "; 25 | } 26 | else{ 27 | cout<<" Zero "; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | int num=-98; 34 | checkNumber(num); 35 | getOddEven(num); 36 | return 0; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /GFG TCS Sheet/03]. Problems on Number Series/07_Harshad_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | An integer number in base 10 which is divisible by the sum of its digits is said to be a Harshad Number. An n-Harshad number is an integer number divisible by the sum of its digit in base n. 3 | Below are the first few Harshad Numbers represented in base 10: 4 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20……… 5 | Given a number in base 10, our task is to check if it is a Harshad Number or not. 6 | 7 | Examples : 8 | 9 | Input: 3 10 | Output: 3 is a Harshad Number 11 | 12 | Input: 18 13 | Output: 18 is a Harshad Number 14 | 15 | Input: 15 16 | Output: 15 is not a Harshad Number 17 | */ 18 | 19 | #include 20 | using namespace std; 21 | 22 | void isHarshadNumber(int n){ 23 | int temp=n; 24 | int sum=0; 25 | while(temp){ 26 | sum+=temp%10; 27 | temp/=10; 28 | } 29 | if(n%sum==0){ 30 | cout<<"YES"; 31 | } 32 | else { 33 | cout<<"NO"; 34 | } 35 | cout< 16 | using namespace std; 17 | 18 | int getReverse(int num){ 19 | int res=0; 20 | while(num>0){ 21 | res=(res*10)+num%10; 22 | num/=10; 23 | } 24 | return res; 25 | } 26 | 27 | int main() 28 | { 29 | int num=12345; 30 | cout<0){ 7 | sum+=N%10; 8 | N/=10; 9 | } 10 | return sum; 11 | } 12 | }; -------------------------------------------------------------------------------- /GFG TCS Sheet/03]. Problems on Number Series/19_Perfect_number.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | 3 | class Solution { 4 | public: 5 | int factorial(int n){ 6 | int fact=1; 7 | while(n>0){ 8 | fact*=n; 9 | n--; 10 | } 11 | return fact; 12 | } 13 | 14 | int isPerfect(int N) { 15 | // code here 16 | int res=0; 17 | vectorv; 18 | int temp=N; 19 | while(temp>0){ 20 | v.push_back(temp%10); 21 | temp/=10; 22 | } 23 | for(int i=0; iv; 8 | int temp=N; 9 | int sum=0; 10 | while(temp>0){ 11 | sum+=temp%10; 12 | temp/=10; 13 | } 14 | 15 | while(sum>0){ 16 | v.push_back(sum%10); 17 | sum/=10; 18 | } 19 | int l=0; 20 | int r=v.size()-1; 21 | 22 | if(v.size()==1){ 23 | return 1; 24 | } 25 | 26 | while(l n 6 | abundance = sum(n) - n 7 | sum(n): aliquot sum - The sum of all proper divisors of n 8 | Given a number n, our task is to find if this number is an Abundant number or not. 9 | The first few Abundant Numbers are: 12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66 ….. 10 | 11 | Abundant Numbers 12 | 13 | Examples: 14 | 15 | Input: 21 16 | Output: NO 17 | 18 | Input: 12 19 | Output: YES 20 | 21 | Input: 17 22 | Output: NO 23 | */ 24 | 25 | #include 26 | using namespace std; 27 | 28 | string isAbundant(int n){ 29 | // vectorv; 30 | int sum=0; 31 | for(int i=1; in){ 38 | return "YES"; 39 | }else{ 40 | return "NO"; 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | int n=21; 47 | cout< 17 | using namespace std; 18 | 19 | int getSum(int n){ 20 | return n*(n+1)/2; 21 | } 22 | 23 | int main(){ 24 | int n=4; 25 | cout< 15 | using namespace std; 16 | 17 | int getSum(int l, int r){ 18 | int sum=0; 19 | for(int i=l; i<=r; i++){ 20 | sum+=i; 21 | } 22 | return sum; 23 | } 24 | 25 | int main() 26 | { 27 | int L=2; 28 | int R=5; 29 | cout<v; 8 | int temp=N; 9 | int sum=0; 10 | while(temp>0){ 11 | sum+=temp%10; 12 | temp/=10; 13 | } 14 | 15 | while(sum>0){ 16 | v.push_back(sum%10); 17 | sum/=10; 18 | } 19 | int l=0; 20 | int r=v.size()-1; 21 | 22 | if(v.size()==1){ 23 | return 1; 24 | } 25 | 26 | while(l 13 | using namespace std; 14 | 15 | void getLargestAndSmallest(int n){ 16 | vectorv; 17 | while(n>0){ 18 | v.push_back(n%10); 19 | n/=10; 20 | } 21 | sort(v.begin(), v.end()); 22 | cout< getAreas(int L , int W , int B , int H , int R) { 4 | // code here 5 | vectorv; 6 | v.push_back(L*W); // Rectangle 7 | v.push_back(0.5*B*H); // Right Angled Triangle 8 | v.push_back(3.14*(R*R)); // Circle 9 | return v; 10 | } 11 | }; -------------------------------------------------------------------------------- /GFG TCS Sheet/03]. Problems on Number Series/README.md: -------------------------------------------------------------------------------- 1 | # Problems on Number Series -------------------------------------------------------------------------------- /GFG TCS Sheet/04]. Problems on Conversions/README.md: -------------------------------------------------------------------------------- 1 | # Problems on Conversions -------------------------------------------------------------------------------- /GFG TCS Sheet/05]. Sorting Algorithms/01_Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Best Case TC: O(n) 5 | // Average Case TC: O(n*n) 6 | // Worst Case TC: O(n*n) 7 | // Bubble Sort is Stable 8 | // Bubble Sort is Adaptive 9 | int BubbleSort(int arr[], int n) 10 | { 11 | int flag; 12 | for(int i=0; iarr[j+1]){ 16 | swap(arr[j], arr[j+1]); 17 | flag=1; 18 | } 19 | } 20 | if(flag==0){ 21 | break; 22 | } 23 | } 24 | cout< 2 | using namespace std; 3 | 4 | // Worst Case TC: O(n*n) 5 | // Average Case TC: O(n*n) 6 | void InsertionSort(int arr[], int n){ 7 | for(int i=1; i=0 && arr[j]>x){ 11 | arr[j+1]=arr[j]; 12 | j--; 13 | } 14 | arr[j+1]=x; 15 | } 16 | cout< 19 | #include 20 | #include 21 | 22 | using namespace std; 23 | 24 | // TC: O(n*log(n)) 25 | // SC: O(n) 26 | int getSmallest(vector arr){ 27 | sort(arr.begin(), arr.end()); 28 | return arr[0]; 29 | } 30 | 31 | int main(){ 32 | vectorarr1 = {23, 24, 53, 53, 14, 456, 755}; 33 | vectorarr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 34 | 35 | cout< 21 | using namespace std; 22 | 23 | // TC: O(n*log(n)) 24 | // SC: O(n) 25 | int getLargest(vector& arr){ 26 | sort(arr.begin(), arr.end()); 27 | return arr[arr.size()-1]; 28 | } 29 | 30 | 31 | int main() { 32 | vector arr1 = {2,5,1,3,0}; 33 | vector arr2 = {8,10,5,7,9}; 34 | 35 | cout<<"The Largest element in the array is: "< 22 | using namespace std; 23 | 24 | // Method 1: 25 | // TC: O(n*log(n)) 26 | // SC: O(1) 27 | void getElements1(int arr[], int n){ 28 | if(n<=1){ 29 | cout<<" 2nd Smallest: -1"< sec_largest && arr[i]!=largest){ 58 | sec_largest=arr[i]; 59 | } 60 | 61 | } 62 | 63 | cout<<"2nd Smallest: "< 19 | using namespace std; 20 | 21 | void reverseArray(int arr[], int n){ 22 | int i=0, j=n-1; 23 | while(i 27 | using namespace std; 28 | 29 | 30 | // Method 1: 31 | // TC: O(n) 32 | // SC: O(n) 33 | void countFreq1(int arr[], int n){ 34 | mapmp; 35 | 36 | for(int i=0; i 18 | using namespace std; 19 | 20 | // TC: O(n*log(n)) 21 | // SC: O(1) 22 | void reArrange(vector& arr, int n){ 23 | sort(arr.begin(), arr.end()); 24 | 25 | for(int i=0; i=n/2; i--){ 29 | cout<arr = {8, 7, 1, 6, 5, 9}; 36 | int n = arr.size(); 37 | reArrange(arr, n); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Striver TCS Sheet/01]. Arrays/07_Calculate_Sum_of_the_Elements_of_the_Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Calculate Sum of the Elements of the Array 3 | Problem Statement: Given an array, we have to find the sum of all the elements in the array. 4 | 5 | Examples: 6 | 7 | Example 1: 8 | Input: N = 5, array[] = {1,2,3,4,5} 9 | Output: 15 10 | Explanation: Sum of all the elements is 1+2+3+4+5 = 15 11 | 12 | Example 2: 13 | Input: N=6, array[] = {1,2,1,1,5,1} 14 | Output: 11 15 | Explanation: Sum of all the elements is 1+2+1+1+5+1 = 11 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | 21 | // Method 1: 22 | // TC: O(n) 23 | // SC: O(1) 24 | int getSum1(vector& arr){ 25 | int n = arr.size(); 26 | int sum = 0; 27 | 28 | for(auto x: arr){ 29 | sum+=x; 30 | } 31 | return sum; 32 | } 33 | 34 | // Method 2: 35 | // TC: O(n) 36 | // SC: O(1) 37 | int getSum2(vector& arr){ 38 | int sum = 0; 39 | return accumulate(arr.begin(), arr.end(), sum); 40 | } 41 | 42 | int main() { 43 | int n = 5; 44 | vectorarr = {1, 2, 3, 4, 5}; 45 | int sum = 0; 46 | cout< 19 | using namespace std; 20 | 21 | // TC: O(n) 22 | // SC: O(1) 23 | double getAverage(vector& arr){ 24 | int n = arr.size(); 25 | double avg=0; 26 | double sum = 0; 27 | return accumulate(arr.begin(), arr.end(), sum)/n; 28 | } 29 | 30 | int main(){ 31 | int n = 5; 32 | vector arr= {1,2,1,1,5,1}; 33 | cout< 17 | using namespace std; 18 | 19 | void getMedian(int arr[], int n){ 20 | double median=0; 21 | sort(arr, arr+n); 22 | 23 | if(n%2==0){ 24 | cout<<(double)(arr[(n/2)]+arr[(n/2)-1])/2; 25 | } 26 | else{ 27 | cout< 29 | using namespace std; 30 | 31 | // Method 1: 32 | // TC: O(n) 33 | // SC: O(1) 34 | void removeSuplicates1(vector& arr){ 35 | int n = arr.size(); 36 | cout<& arr){ 48 | sets(arr.begin(), arr.end()); 49 | for(auto x: s) cout<arr{1,1,1,2,2,3,3,3,3,4,4}; 56 | removeSuplicates1(arr); 57 | cout< 21 | using namespace std; 22 | 23 | // Method 1: 24 | // TC: O(n) 25 | // SC: O(1) 26 | void removeSuplicates1(vector& arr){ 27 | int n = arr.size(); 28 | sort(arr.begin(), arr.end()); 29 | cout<& arr){ 41 | sets(arr.begin(), arr.end()); 42 | for(auto x: s) cout<& arr){ 49 | int n = arr.size(); 50 | mapmp; 51 | for(int i=0; iarr{2,3,1,9,3,1,3,9}; 61 | removeSuplicates1(arr); 62 | cout< 15 | using namespace std; 16 | 17 | // Inserting the element at the BEGINNING 18 | void insertatbegin(int arr[], int n, int value){ 19 | for(int i=n-1; i>=0; i--){ 20 | arr[i+1]=arr[i]; 21 | } 22 | arr[0]=value; 23 | n= n+1; 24 | } 25 | 26 | // Inserting at the END 27 | void insertatEnd(int arr[], int n, int value){ 28 | arr[n]=value; 29 | } 30 | 31 | // Insertion at specific position 32 | void insertatposition(int arr[], int n, int value, int pos){ 33 | for(int i=n-1; i>=pos; i--){ 34 | arr[i]=arr[i-1]; 35 | } 36 | arr[pos-1]=value; 37 | 38 | } 39 | 40 | int main() 41 | { 42 | int arr[9]={10,9,14,8,20,48,16,9}; 43 | int n=8; 44 | int value=40; 45 | cout<<"Before inserting the value at beginning:"< 25 | using namespace std; 26 | 27 | // Method 1; 28 | // TC: O(n*log(n)) 29 | // SC: O(1) 30 | void findRepeatingElements1(int arr[], int n){ 31 | sort(arr, arr+n); 32 | 33 | for(int i=1; imp; 46 | 47 | for(int i=0; i1) 53 | cout< 25 | using namespace std; 26 | 27 | // Method 1: 28 | // TC: O(n) 29 | // SC: O(n) 30 | void findNonRepeatingElement(vector& nums){ 31 | int n = nums.size(); 32 | mapmp; 33 | 34 | for(int i=0; i nums = {1,2,-1,1,3,1}; 49 | cout<<"Non-repeating numbers are: "< 27 | using namespace std; 28 | 29 | // Method 1: 30 | // TC: O(n*n) 31 | // SC: O(1) 32 | int maxProductSubArray(vector& nums){ 33 | int n = nums.size(); 34 | int max_product = 1; 35 | 36 | for(int i=0; i nums = {1,2,3,4,5,0}; 48 | cout<<"The maximum product subarray: "< 21 | using namespace std; 22 | 23 | void ReplaceElement(int arr[], int n){ 24 | vectorv(arr, arr+n); 25 | sort(v.begin(), v.end()); 26 | 27 | for(int i=0; i 22 | using namespace std; 23 | 24 | void sortByFreq(int &arr, int n){ 25 | 26 | } 27 | 28 | int main() 29 | { 30 | int n=6; 31 | int arr[]={1,2,3,2,4,3,1,2}; 32 | sortByFreq(arr, n); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Striver TCS Sheet/01]. Arrays/24_Search_an_Element_in_an_Array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Search an Element in an Array : Program CPP Java 3 | Problem Statement: Search an element in an array and return its position 4 | 5 | Examples: 6 | 7 | Example 1: 8 | Input: array[] = {1,2,3,4,5} k=3 Output: 2 Explanation: The answer is 2 because 3 is present at 2nd index. 9 | 10 | Example 2: 11 | Input: array[]={6,7,9,5,3,10} k=10 12 | Output: 5 13 | Explanation: The answer is 5 because 10 is present at 5th index. 14 | */ 15 | 16 | #include 17 | using namespace std; 18 | 19 | // Method 1: Linear Search 20 | // TC: O(n) 21 | // SC: O(1) 22 | int linearSearch(int arr[], int n, int k){ 23 | for(int i=0; i 32 | // TC: O(log(n)) 33 | // SC: O(1) 34 | int binarySearch(int arr[], int n, int k){ 35 | int left = 0; 36 | int right = n-1; 37 | 38 | while(left<=right){ 39 | int mid = (left+right)/2; 40 | 41 | if(arr[mid]==k){ 42 | return mid; 43 | } 44 | else if(arr[mid] 21 | using namespace std; 22 | 23 | // Method 1: 24 | // TC: O(n) 25 | // SC: O(n) 26 | bool isPalindrome(int x){ 27 | vectorarr; 28 | while(x>0){ 29 | arr.push_back(x%10); 30 | x/=10; 31 | } 32 | 33 | int n = arr.size(); 34 | int i = 0; 35 | int j = n-1; 36 | while(i 21 | using namespace std; 22 | 23 | bool isPrime(int n){ 24 | 25 | for(int i=2; i<=n; i++){ 26 | if(n%i==0){ 27 | return false; 28 | } 29 | } 30 | return true; 31 | } 32 | 33 | int main() { 34 | 35 | int n = 1925; 36 | 37 | bool ans = isPrime(n); 38 | if (n != 1 && ans == true) { 39 | cout << "Prime Number"; 40 | } else { 41 | cout << "Non Prime Number"; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Striver TCS Sheet/02]. Problems_on_Numbers/04_Prime_Numbers_in_a_given_range.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prime Numbers in a given range 3 | Problem Statement: Given a and b, find prime numbers in a given range [a,b], (a and b are included here). 4 | 5 | Examples: 6 | 7 | Examples: 8 | Input: 2 10 9 | Output: 2 3 5 7 10 | Explanation: Prime Numbers b/w 2 and 10 are 2,3,5 and 7. 11 | 12 | Example 2: 13 | Input: 10 16 14 | Output: 11 13 15 | Explanation: Prime Numbers b/w 10 and 16 are 11 and 13. 16 | */ 17 | 18 | #include 19 | using namespace std; 20 | 21 | void getPrimeNumbers(int a, int b){ 22 | for(int n=a; n<=b; n++){ 23 | int flag = true; 24 | for(int j=2; j 24 | using namespace std; 25 | 26 | void isPerfect(int n){ 27 | vectorarr; 28 | for(int i=1; i 18 | using namespace std; 19 | 20 | void isOddOrEven(int n){ 21 | if(n%2==0) 22 | cout< 17 | using namespace std; 18 | 19 | void check(int n){ 20 | if(n<0) cout< 19 | using namespace std; 20 | 21 | // TC: O(1) 22 | // SC: O(1) 23 | void getSum(int n){ 24 | int sum = n*(n+1)/2; 25 | cout<<"The sum of first "< 19 | using namespace std; 20 | 21 | void getLargest(int a, int b){ 22 | cout<<"Greatest number is "< 19 | using namespace std; 20 | 21 | void getLargest(double a, double b, double c){ 22 | double sec_largest = max(a, b); 23 | double largest = max(c, sec_largest); 24 | cout<<"Largest number is "< 18 | using namespace std; 19 | 20 | int reverseDigits(int num){ 21 | vectorarr; 22 | int res=0; 23 | while(num>0){ 24 | arr.push_back(num%10); 25 | num/=10; 26 | } 27 | 28 | for(int i=0; i 25 | using namespace std; 26 | 27 | void get_max_and_min_digit(int num){ 28 | vectorarr; 29 | while(num>0){ 30 | arr.push_back(num%10); 31 | num/=10; 32 | } 33 | sort(arr.begin(), arr.end()); 34 | cout<<"Smallest Digit: "< 20 | using namespace std; 21 | 22 | void getFibonacci(int n){ 23 | 24 | 25 | } 26 | 27 | int main() 28 | { 29 | int n = 6; 30 | getFibonacci(n); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Striver TCS Sheet/02]. Problems_on_Numbers/18_Factorial_of_a_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Factorial of a Number : Iterative and Recursive 3 | Problem Statement: Given a number X, print its factorial. 4 | 5 | To obtain the factorial of a number, it has to be multiplied by all the whole numbers preceding it. More precisely X! = X*(X-1)*(X-2) … 1. 6 | 7 | Note: X is always a positive number. 8 | 9 | Examples: 10 | 11 | Example 1: 12 | Input: X = 5 13 | Output: 120 14 | Explanation: 5! = 5*4*3*2*1 15 | 16 | Example 2: 17 | Input: X = 3 18 | Output: 6 19 | Explanation: 3!=3*2*1 20 | */ 21 | 22 | #include 23 | using namespace std; 24 | 25 | void getFactorial(int n){ 26 | int res=1; 27 | for(int i=1; i<=n; i++){ 28 | res*=i; 29 | } 30 | cout<<"Factoral of "< 19 | using namespace std; 20 | 21 | void powerOf(int n, int k){ 22 | int res=1; 23 | for(int i=0; i 19 | using namespace std; 20 | 21 | void getPrimeFactors(int n){ 22 | vectorarr; 23 | for(int i=1; i 23 | using namespace std; 24 | 25 | int factorial(int n){ 26 | int fact = 1; 27 | for(int i=1; i<=n; i++){ 28 | fact *= i; 29 | } 30 | return fact; 31 | } 32 | 33 | void isStrong(int n){ 34 | int res = 0; 35 | vectorarr; 36 | int temp = n; 37 | while(temp>0){ 38 | arr.push_back(temp%10); 39 | temp/=10; 40 | } 41 | 42 | for(int i=0; i 20 | using namespace std; 21 | 22 | void isNiven(int n){ 23 | vectorarr; 24 | int temp = n; 25 | while(temp>0){ 26 | arr.push_back(temp%10); 27 | temp /= 10; 28 | } 29 | int sum = 0; 30 | sum = accumulate(arr.begin(), arr.end(), sum); 31 | 32 | if(n%sum==0){ 33 | cout< 19 | using namespace std; 20 | 21 | void isAbundant(int n){ 22 | vectorarr; 23 | int temp = n; 24 | for(int i=1; i n){ 33 | cout< 19 | using namespace std; 20 | 21 | void getSumOfDigits(int num){ 22 | vectorarr; 23 | int sum = 0; 24 | while(num>0){ 25 | arr.push_back(num%10); 26 | num/=10; 27 | } 28 | cout< 18 | using namespace std; 19 | 20 | void getSum(int l, int r){ 21 | int sum = 0; 22 | for(int i=l; i<=r; i++){ 23 | sum += i; 24 | } 25 | cout<<"sum : "< 19 | using namespace std; 20 | 21 | void replaceZerosWithOne(int n){ 22 | vectorarr; 23 | int res = 0; 24 | while(n>0){ 25 | arr.push_back(n%10); 26 | n/=10; 27 | } 28 | 29 | reverse(arr.begin(), arr.end()); 30 | 31 | for(int i=0; i 18 | using namespace std; 19 | 20 | bool isSumOfTwoPrime(int n){ 21 | vectorarr; 22 | for(int num=1; num 19 | using namespace std; 20 | 21 | double getAreaOfCircle(double r){ 22 | return 3.14*r*r; 23 | } 24 | 25 | int main() 26 | { 27 | int r = 5; 28 | cout< 18 | #include 19 | #include 20 | #include 21 | using namespace std; 22 | 23 | // Method 1: 24 | // TC: O(n) n = No. of Digits in the number 25 | // SC: O(n) n = No. of Digits in the number 26 | int convert_to_decimal1(int N){ 27 | vectorv; 28 | int res = 0; 29 | while(N>0){ 30 | v.push_back(N%10); 31 | N /= 10; 32 | } 33 | 34 | for(int i=0; i=0; i--){ 47 | if(M[i] == '1') 48 | res += base; 49 | base *= 2; 50 | } 51 | return res; 52 | } 53 | 54 | 55 | int main(){ 56 | int N = 1011; 57 | cout< 19 | #include 20 | using namespace std; 21 | 22 | void convert_binary_to_octal(string s){ 23 | int n = s.length(); 24 | if( n%3 == 1){ 25 | s = "00" + s; 26 | } 27 | else if( n%3 == 2){ 28 | s = "0" + s; 29 | } 30 | n = s.length(); 31 | string res = ""; 32 | for(int i = 0; i < n; i += 3){ 33 | // converting to int by substracting '0' before multiplying 34 | int temp = (s[i]-'0')*4 + (s[i+1]-'0')*2 + (s[i+2]-'0')*1; 35 | // converting to string again by adding '0' and adding it to the res 36 | res += temp + '0'; 37 | } 38 | cout< 19 | using namespace std; 20 | 21 | void decimal_to_binary(int n){ 22 | int binary[32] = {0}; 23 | int i = 0; 24 | while(n){ 25 | binary[i] = n%2; 26 | i++; 27 | n /= 2; 28 | } 29 | 30 | int res = 0; 31 | for(int ind = i-1; ind>=0; ind--){ 32 | cout< 19 | #include 20 | using namespace std; 21 | 22 | int decimal_to_octal(int decimal){ 23 | int octal = 0; 24 | int i = 0; 25 | while(decimal != 0){ 26 | int rem = decimal % 8; 27 | octal += rem * pow(10, i); 28 | i++; 29 | decimal = decimal / 8; 30 | } 31 | return octal; 32 | } 33 | 34 | int main(){ 35 | int decimal = 136; 36 | cout< 19 | #include 20 | using namespace std; 21 | 22 | int convert_octal_to_decimal(int octal){ 23 | int decimal = 0; 24 | int i = 0; 25 | while(octal > 0){ 26 | int rem = octal % 10; 27 | decimal += rem * pow(8, i); 28 | i++; 29 | octal = octal / 10; 30 | } 31 | return decimal; 32 | } 33 | 34 | int convert_octal_to_binary(int octal){ 35 | int decimal = convert_octal_to_decimal(octal); 36 | int binary = 0; 37 | int i = 0; 38 | while(decimal != 0){ 39 | int rem = decimal % 2; 40 | binary += (rem * pow(10, i)); 41 | i++; 42 | decimal = decimal / 2; 43 | } 44 | return binary; 45 | } 46 | 47 | int main(){ 48 | int octal = 345; 49 | cout< 19 | #include 20 | using namespace std; 21 | 22 | int convert_octal_to_decimal(int octal){ 23 | int decimal = 0; 24 | int i = 0; 25 | while(octal != 0){ 26 | int rem = octal % 10; 27 | decimal += rem * pow(8, i); 28 | i++; 29 | octal = octal / 10; 30 | } 31 | return decimal; 32 | } 33 | 34 | int main(){ 35 | int octal = 345; 36 | cout< 19 | using namespace std; 20 | 21 | // Worst Case TC: O(n*n) 22 | // Best case TC: O(n) : if array is already sorted 23 | // Bubble Sort is Adaptive, we can make it adaptive - By nature Bubble sort is not adaptive 24 | // Bubble Sort is Stable 25 | void BubbleSort(int A[], int n){ 26 | for(int i=0; i A[j+1]){ 30 | swap(A[j], A[j+1]); 31 | flag = 1; 32 | } 33 | } 34 | if(flag == 0){ 35 | break; 36 | } 37 | } 38 | } 39 | 40 | int main() 41 | { 42 | int A[]={13,46,24,52,20,9}; 43 | 44 | cout<<"Before Sorting: "< 20 | using namespace std; 21 | 22 | // Worst case TC: O(n*n) 23 | // Best case TC: O(n) 24 | // Insertion sort is Adaptive in nature 25 | // Insertion sort is stable 26 | void InsertionSort(int arr[], int n){ 27 | for(int i=1; i=0 && arr[j]>x){ 31 | arr[j+1]=arr[j]; 32 | j--; 33 | } 34 | arr[j+1]=x; 35 | } 36 | } 37 | 38 | int main() 39 | { 40 | int arr[] = {13,46,24,52,20,9}; 41 | int n = sizeof(arr)/sizeof(arr[0]); 42 | 43 | cout<<"Before Sorting: "< 19 | using namespace std; 20 | 21 | void selectionSort(int arr[], int n){ 22 | int i,j,k; 23 | for(i=0; i 19 | using namespace std; 20 | 21 | int swap(int arr[], int i, int j){ 22 | int temp = arr[i]; 23 | arr[i]=arr[j]; 24 | arr[j]=temp; 25 | } 26 | 27 | int partition(int arr[], int l, int r){ 28 | int pivot=arr[r]; 29 | int i=l-1; 30 | 31 | for(int j=l; j 17 | using namespace std; 18 | 19 | void merge(int arr[], int l, int mid, int r){ 20 | int n1=mid-l+1; 21 | int n2=r-mid; 22 | 23 | int a[n1]; 24 | int b[n2]; 25 | 26 | for(int i=0; i 19 | using namespace std; 20 | 21 | bool isPalindrome(string str){ 22 | int i = 0; 23 | int j = str.length()-1; 24 | 25 | while(i 24 | using namespace std; 25 | 26 | void solve(string str, int n){ 27 | int count_v = 0; 28 | int count_c = 0; 29 | int count_s = 0; 30 | 31 | for(int i=0; i 19 | using namespace std; 20 | 21 | void get_ASCII_value(char c){ 22 | cout<<(int)c< 19 | using namespace std; 20 | 21 | void RemoveVowels(string s){ 22 | string res=""; 23 | for(int i=0; i 20 | using namespace std; 21 | 22 | void removeSpaces(string s){ 23 | for(int i=0; s[i]!='\0'; i++){ 24 | if(s[i]!=' '){ 25 | cout< 23 | using namespace std; 24 | 25 | void removeCharacters(string s){ 26 | for(int i=0; i= 'A' && s[i]<='Z') || (s[i] >= 'a' && s[i] <= 'z') ){ 28 | cout< 13 | using namespace std; 14 | 15 | // Method 1: 16 | // TC: O(n) 17 | // SC: O(1) 18 | void getReverse1(string s){ 19 | for(int i=s.length(); i>=0; i--){ 20 | cout< 27 | using namespace std; 28 | 29 | void removeBrackets(string s){ 30 | for(int i=0; s[i]!='\0'; i++){ 31 | if(s[i]!='(' && s[i]!=')'){ 32 | cout< 18 | using namespace std; 19 | 20 | void capitalize(string s){ 21 | int n = s.length(); 22 | if(s[0] >= 'a' && s[0] <= 'z') 23 | s[0]-=32; 24 | 25 | if(s[n-1] >= 'a' && s[n-1] <= 'z') 26 | s[n-1]-=32; 27 | 28 | for(int i=1; i= 'a' && s[i] <= 'z')){ 31 | s[i]-=32; 32 | } 33 | } 34 | 35 | for(int i=0; i 18 | using namespace std; 19 | 20 | void Printfrequency(string s){ 21 | mapmp; 22 | 23 | for(int i=0; i 20 | using namespace std; 21 | 22 | void getNonRepeating(string s){ 23 | mapmp; 24 | 25 | for(int i=0; i 19 | using namespace std; 20 | 21 | bool isAnagram(string s1, string s2){ 22 | sort(s1.begin(), s1.end()); 23 | sort(s2.begin(), s2.end()); 24 | 25 | if(s1.length()!=s2.length()){ 26 | return false; 27 | } 28 | for(int i=0; i 21 | using namespace std; 22 | 23 | void getMaximumOccuring(string s){ 24 | mapmp; 25 | int n = s.length(); 26 | for(int i=0; i 19 | using namespace std; 20 | 21 | void removeDupicates(string s){ 22 | unordered_mapump; 23 | for(int i=0; i 25 | Explanation: 26 | 27 | In the above example, every character occurs only once(no duplicates), therefore nothing to print. 28 | 29 | */ 30 | 31 | #include 32 | using namespace std; 33 | 34 | // TC: O(n) 35 | // SC: O(n) 36 | void getDuplicates(string s){ 37 | int n = s.length(); 38 | mapmp; 39 | 40 | for(int i=0; i1){ 46 | cout< 23 | using namespace std; 24 | 25 | // TC: O(n*n) 26 | // SC: O(1) 27 | void getCharacters(string s1, string s2){ 28 | for(int i=0; i 20 | using namespace std; 21 | 22 | void changeLetter(string s){ 23 | int n = s.length(); 24 | for(int i=0; i 18 | using namespace std; 19 | 20 | // Method 1: 21 | void getSorted1(string s){ 22 | sort(s.begin(), s.end()); 23 | for(auto x: s) 24 | cout<st(s.begin(), s.end()); 31 | for(auto x: st){ 32 | cout< 14 | using namespace std; 15 | 16 | void countWords(string s){ 17 | int count = 0; 18 | for(int i=0; i 20 | using namespace std; 21 | 22 | void changeTheCase(string s){ 23 | for(int i=0; i 'A' && s[i] < 'Z'){ 25 | s[i]+=32; 26 | } 27 | else if(s[i] > 'a' && s[i] < 'z'){ 28 | s[i]-=32; 29 | } 30 | } 31 | 32 | for(int i=0; i 11 | using namespace std; 12 | 13 | string concatinate(string s1, string s2){ 14 | return s1+s2; 15 | } 16 | 17 | int main() 18 | { 19 | string s1="Hello"; 20 | string s2="World"; 21 | cout< 10 | --------------------------------------------------------------------------------