├── .vscode └── settings.json ├── Blogs └── Disjoint Set Union.txt ├── C++ ├── 132-pattern.cpp ├── AdditionofComplexNumber.cpp ├── Ak9.cpp ├── Ambiguity.cpp ├── AmbiguityHybridInheritance.cpp ├── AnagramCheck.cpp ├── Any.cpp ├── AreaOfCircle.cpp ├── AreaOfRectangleUsingConstructor.cpp ├── ArrayOfObjects.cpp ├── ArrayOfObjectsUsingPOINTER-Approach2.cpp ├── ArrayOfObjectsUsingpPOINTER.cpp ├── ArrayOfStructure.cpp ├── ArrayOfUnion.cpp ├── Array_operation.cpp ├── BSTimplementation.cpp ├── Bubble sort.cpp ├── Calculator.cpp ├── Check Armstrong number.cpp ├── ClassesContainingPointer.cpp ├── ConstructorListInitialization.cpp ├── ConstructorOverloading.cpp ├── ConstructorWithDefaultValues.cpp ├── Constructors.cpp ├── Copy String.cpp ├── CopyConstructor.cpp ├── CountSort.cpp ├── Count_letter_frequency.cpp ├── Cpp_pattern │ ├── C++_invert_V-shape.cpp │ ├── C++_invert_V-shape.exe │ ├── C++_invert_pyramid.cpp │ ├── C++_invert_pyramid.exe │ ├── C++_lower_full_pyramid.cpp │ ├── C++_lower_full_pyramid.exe │ ├── C++_lower_half_triangle.cpp │ └── C++_lower_half_triangle.exe ├── DeclareMember.cpp ├── DeleteLastof_SinglyLinkedList.cpp ├── Destructor.cpp ├── Display.cpp ├── Display1to10.cpp ├── Employee.txt ├── Enumeration.cpp ├── Factorial.cpp ├── FileHandling-Create.cpp ├── FileHandling-OpenFunction.cpp ├── FileHandling-Read.cpp ├── FileHandling.cpp ├── FileModes.cpp ├── FilePointer-seekg.cpp ├── FilePointerFunction-Advance.cpp ├── FilePointerSpecialFunction.cpp ├── FileStreamFunction.cpp ├── FindMAXNum.cpp ├── FriendClass.cpp ├── FunctionOverloading.cpp ├── Functions.cpp ├── HollowPyramid.cpp ├── HybridInheritance.cpp ├── InlineMemberFunc.cpp ├── Kth Smallest Element.cpp ├── Largest subarray 0 sum.cpp ├── Lcs.cpp ├── Leap Year.cpp ├── Linear_Search.cpp ├── MaxNumber.cpp ├── MeanOfData.cpp ├── MultiLevelInheritance.cpp ├── Multiplication.cpp ├── NonInlineMemberFunc.cpp ├── Oops.cpp ├── OperatorOverloading.cpp ├── OrderOfExecution-ParameterizedConstructor.cpp ├── OrderOfExecution.cpp ├── OverloadAdditionOperator.cpp ├── OverloadingBinaryOperatorUsingFriendFunction.cpp ├── Palindrome_Number_Check.cpp ├── PassingByReference.cpp ├── Permutations.cpp ├── PointerinObjects.cpp ├── PointersToObject.cpp ├── Quick Sort.cpp ├── RadixSort.cpp ├── Recursion.cpp ├── Remove_outermost_parentheses.cpp ├── Reverse_Queue.cpp ├── Stack_implementation.cpp ├── StringFindFunc.cpp ├── StringFunction.cpp ├── StringFunctions.cpp ├── StringOperations.cpp ├── Strings.cpp ├── StringsOperation.cpp ├── StructuresBasic.cpp ├── ThisPointer.cpp ├── ThisPointerAddressProblem.cpp ├── ThisPointerClassProblem.cpp ├── Triangle_pattern.cpp ├── TypeConversion.cpp ├── UnaryOperator.cpp ├── Unionof_twosorted arrays.cpp ├── Virtual_function.cpp ├── binary search.cpp ├── binarysearch.cpp ├── bubble_sort.cpp ├── circularlinkedlist.cpp ├── cocktailSort.cpp ├── cs50.cpp ├── e4t7.cpp ├── factorial1.cpp ├── fibonacci.cpp ├── find-bad-version.cpp ├── finding_roots.cpp ├── goodpairs.cpp ├── hibin3.cpp ├── insertion_sort.cpp ├── knapsack.cpp ├── lowerbound.cpp ├── matrixADT.cpp ├── merge_sort.cpp ├── mult 2 array.cpp ├── overlapping-intervals.cpp ├── palindrome.cpp ├── printnnumbers.cpp ├── removeDuplicatesInArray.cpp ├── result.txt ├── reverse linked list.cpp ├── reverse_string_using_stack.cpp ├── right_trianglepattern.cpp ├── rotate_linkedlist_Ktimes.cpp ├── selectionsort.cpp ├── stack.cpp ├── star pattern.cpp ├── tree.cpp ├── vector.cpp ├── warshall.cpp └── zig_zag_traversal.cpp ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Codechef ├── 4a_Watermelon_cf.cpp ├── ALTSTR.cpp ├── ARRAYBREAK.cpp ├── AUDIBLE.cpp ├── BALLON.cpp ├── BINARYSUB.cpp ├── BROKPHON.cpp ├── CP_solution(Chef and Wire Frames).py ├── DiceNumber.cpp ├── Distinct_permutation.cpp ├── EID.cpp ├── ERROR.cpp ├── EUREKA.cpp ├── FLIPCARDS.PY ├── HAPPYSTR.cpp ├── HOW_MANY_DIGITS_DO_I_HAVE.c ├── KNIGHT2.cpp ├── Kadanes_Algorithm.cpp ├── MATCHES.cpp ├── MAXFUN.cpp ├── MAXSUBARR.cpp ├── MEANMEDIAN.cpp ├── ODDPAIRS.cpp ├── PAIREQ.cpp ├── PATHPAR.cpp ├── PERMCLEAR.cpp ├── Problems in your to-do list ├── REMOVECARDS.cpp ├── SKMP.cpp ├── SUBARRAY-221D.cpp ├── SUBSEQDIST.cpp ├── SUSSTR.cpp ├── TODOLIST.cpp ├── WORDLE.cpp ├── XORGAND.cpp ├── _BINBASBASIC.cpp ├── addtwonumbers.py ├── aug4(5).c ├── credcoins.py ├── expertsetter.py ├── howmanycodechefquestionc++.cpp ├── minimumpizza.py ├── saleseason.py ├── test.cpp └── testaverage.py ├── Codeforces ├── 140-B.cpp ├── 1674D.cpp ├── 1738-A.cpp ├── 1846C.cpp ├── 1862C.cpp ├── 263_A_Beautiful_matrix.cpp ├── 405A_Gravity_Flip.cpp ├── 794-c.cpp ├── 820-B.cpp ├── 825_B_Playing_with_GCD.cpp ├── 826_D_Masha_and_a-beautiful_tree.cpp ├── 827_C_Stripes.cpp ├── A._Football.cpp ├── Beautiful_matrix(Codeforces).cpp ├── Binary String Sorting_Edu145D.cpp ├── Borze(Codeforces).cpp ├── D_Tree_Construction.cpp ├── DivisorChain.cpp ├── GameOnPermutation.cpp ├── Garland_Edu145A.cpp ├── Mex Master_Cf858B.cpp ├── Points on Plane_Edu145B.cpp ├── Sequence Master_Cf858C.cpp ├── Sum on Subarrays_Edu145C.cpp ├── Walking Master_Cf858A.cpp ├── max_subarray_sum.cpp └── xor_palindrome.cpp ├── DSA └── HeapSort.c ├── Hackerearth └── test.cpp ├── Java Programs ├── 3sumcloset.java ├── ATM.java ├── BinaryTree.class ├── CheckSorted.java ├── FirstMissingPositive.java ├── GOLDEN RATIO (VA) NEW 22.java ├── Inorder_Traversing.class ├── Inorder_Traversing.java ├── LargestRectangleInHistogram ├── Length.java ├── MaxPointsOnALine.java ├── MergeSort.java ├── Merge_KSortedLists.java ├── N-Queen(FSS) New22.java ├── Node.class ├── NumberGuessingGame.java ├── Postorder_Traversing.java ├── Preorder_Traversing.class ├── Preorder_Traversing.java ├── QuickSort.java ├── SlidingWindowMaximum.java ├── Sudoku Solver │ ├── Sudoku.class │ └── Sudoku.java ├── TowerOfHanoi.java ├── TrappingRainwater.java ├── area_of_circle.java ├── binsearch21.java ├── factorial.java ├── heapsort.java ├── java.java ├── main2.java └── volume_of_cylinder.java ├── JavaScript ├── Javascript five courses for free.docx ├── The Best Podcasts for JavaScript.doc └── hello.js ├── Javascript five courses for free.docx ├── LICENSE ├── Leetcode ├── 1020_Number_of_Enclaves.cpp ├── 1029_Two_City_Scheduling.cpp ├── 1061_Lexicographically_Smallest_Equivalent_String.cpp ├── 1095.Find_in_mountain_array.cpp ├── 10_delete-the-middle-node-of-linkedlist.cpp ├── 1104_Path_In_Zigzag_Labelled_Binary_Tree.cpp ├── 136_single_number.cpp ├── 1463_Cherry_Pickup_2.cpp ├── 1752_check_if_array_is_rotated_and_sorted.cpp ├── 189_Rotate_array.cpp ├── 1_Set_Matrix_Zeroes.cpp ├── 2009.Minimun_number_of_operations_to_make_the_array_continuous.cpp ├── 209. Minimum_Size_Subarray_Sum.cpp ├── 2_Pascal_Triangle.cpp ├── 37Sudoku_Solver.cpp ├── 3_next_permutation.cpp ├── 42Trapping Rain Water.cpp ├── 42_Trapping_Rainwater_Problem.cpp ├── 4_maximum_subarray.cpp ├── 4sum.py ├── 515._Find_Largest_Value_in_Each_Tree_Row.cpp ├── 51_N-Queens.cpp ├── 5_Sort_Colors.cpp ├── 66_plus_one.cpp ├── 6_stock_buy_and_sell.cpp ├── 7_rotate_image.cpp ├── 84LargestRectangleinHistogram.cpp ├── 8_Merge_intervals.cpp ├── 9_Merge_sorted_array.cpp ├── All_A's_Before_B's.cpp ├── Binary_Tree_Max_Path.cpp ├── BoundaryTraversalBinaryTree.cpp ├── Build_Array.cpp ├── Combination-Sum-II.cpp ├── Count number of nice subaarrays.py ├── Count-Number-of-Distinct-Integers-After-Reverse-Operations.cpp ├── Find-duplicate-number.cpp ├── Find_Duplicate_File_in_System.java ├── Find_Minimum_in_Rotated_Sorted_Array.cpp ├── Heeba-Khan │ └── 1095.Find_in_the_mountain_array.cpp ├── Intersection_of_Arrays-II.cpp ├── Intersection_of_Arrays.cpp ├── LinkedListCycle.cpp ├── Longest_Palindromic_Substring.cpp ├── Longest_substring_without_repeating_characters.cpp ├── Median_of_2_sorted_array.cpp ├── NQueenProblem.cpp ├── NaryTreeTraversal.cpp ├── PalindromeNumber.py ├── PowerOfThree.py ├── PowerOfTwo.py ├── Q50_Pow(x,n).cpp ├── Q56_Merge_Intervals.cpp ├── Q58_Length_of_last_word.cpp ├── Regular_Expression_Matching.cpp ├── Remove _Duplicates_from_Sorted_Array.cpp ├── Remove_Duplicates_from_Sorted_Array.cpp ├── Reverse_integer.c ├── Rotate_Array.cpp ├── Subsets.py ├── ValidAnagrams.cpp ├── addstring.py ├── binaryvalid.cpp ├── flood_fill.cpp ├── mergelink.cpp ├── palindrome_number.py ├── poweroffour.py ├── sudoku_solver.cpp ├── sum-of-number-and-its-reverse.cpp ├── sumoftwonumber.py ├── test.cpp ├── two sum └── uniquepaths.cpp ├── Python programs ├── 3D Graphs.py ├── 3sum.py ├── 4sum.py ├── Alarm_Clock_Code.py ├── BMI Calculator.py ├── Buy and sell stocks IV.py ├── C.P_Solution.py ├── CP-SOLUTION21.py ├── Count Good Nodes in Binary Tree.py ├── Count Subarrays with fixed bound.py ├── Delete Operation for Two Strings.py ├── Dictionary_in_python.py ├── Dynamic_Programming_making_two_strings_equal..py ├── Game_of_life.py ├── Guess The number.py ├── Image Filtering.py ├── Largest Rectangle in Histogram.py ├── Leetcode solution - Subsets II.py ├── Longest Common Subsequence.py ├── Longest Substring Without Repeating Characters.py ├── Max Chunks To Make Sorted II.py ├── Maximum Sum of an Hourglass.py ├── Monty_hall.py ├── N-Queen(SSF)100.py ├── Number of Longest Increasing Subsequence.py ├── Password_genertaor.py ├── Queries on Number of Points Inside a Circle.py ├── Quiz Game.py ├── Rolling Dice program.py ├── Score of Parentheses.py ├── Simplfy Path.py ├── Smallest Subsequence of Distinct Characters.py ├── Snake_Water_game.py ├── Teemo Attacking.py ├── alphaRangoli.py ├── analogClock.py ├── binarySearch.py ├── caterpiller_game.py ├── collage_maker.py ├── days.py ├── fibonacci.py ├── hangman_game.py ├── input.py ├── insert_interval.py ├── longest valid parenthesis.py ├── longestCommonSubsequence.py ├── odd_even.py ├── password.py ├── pearson_correlation.py ├── stock_price.py ├── storey_generator.py ├── sudoko.py ├── typing.py └── video_to_audio.py ├── README.md ├── Web-dev ├── CSS wildcard │ └── CSS-wildcard.html ├── DOM │ ├── index.html │ ├── index.js │ └── master.css ├── RestfulApi │ ├── .gitignore │ ├── api.js │ └── package-lock.json ├── Wheel_of_fortune │ ├── design.css │ ├── freecodecamp_js_tut │ │ ├── design.css │ │ ├── design5.css │ │ ├── index.js │ │ ├── index2.js │ │ ├── index3.js │ │ ├── index4.js │ │ ├── index5.js │ │ ├── station.jpg │ │ ├── table.jpg │ │ ├── tut.html │ │ ├── tut2.html │ │ ├── tut3.html │ │ ├── tut4.html │ │ └── tut5.html │ ├── index.html │ └── script.js ├── app.js ├── app1.js ├── colorguess │ ├── css │ │ └── styles.css │ ├── index.html │ └── random.js ├── html5-datetimezone │ └── index.html ├── readme.md └── styles.css ├── basicsCheatsheet.js ├── c programming ├── N-Queen(SFS)100.c ├── Pattern EQI-T 1.c ├── Pattern EQI-T 2.c ├── Pattern EQI-T 3.c ├── Pattern EQI-T 4.c ├── Pattern EQI-T with Space 5.c ├── To_find_keyword.c ├── To_find_keyword.txt ├── TowerofHanoi.c ├── doublelinkedlist.c ├── if-else statement.c ├── palindromeexplanation.c ├── palindromenum.c ├── palindromenumexample.c ├── pattern programs │ ├── pattern-1.c │ ├── pattern-2.c │ ├── pattern-3.c │ └── pattern-4.c └── singlelinkedlist.c └── hackerrank ├── Find the Runner-Up Score!.py ├── Games of thrones.py ├── compressTheString.py ├── defaultArguments.py ├── if_else.py ├── listcomprehension2.py ├── listcomprehsension.py ├── loops.py ├── printfunction.py ├── triangleQuest.py ├── tutorial11.cpp └── writeafunction.py /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "githubPullRequests.ignoredPullRequestBranches": [ 3 | "main" 4 | ] 5 | } -------------------------------------------------------------------------------- /C++/132-pattern.cpp: -------------------------------------------------------------------------------- 1 | bool find132pattern(vector& nums) { 2 | int s3 = INT_MIN; 3 | stacks; 4 | for(int i=nums.size()-1; i>=0; i--) 5 | { 6 | if(nums[i]s.top()) 13 | { 14 | s3 = s.top(); 15 | s.pop(); 16 | } 17 | s.push(nums[i]); 18 | 19 | } 20 | } 21 | return false; 22 | } 23 | -------------------------------------------------------------------------------- /C++/AdditionofComplexNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Complex 5 | { 6 | int x, y; 7 | public: 8 | Complex() 9 | { 10 | x = 10; 11 | y = 20; 12 | } 13 | Complex(int a, int b) 14 | { 15 | x = a; 16 | y = b; 17 | } 18 | Complex operator +(Complex c) 19 | { 20 | Complex temp; 21 | temp.x = x + c.x; 22 | temp.y = y + c.y; 23 | return temp; 24 | } 25 | void show() 26 | { 27 | cout << x << "+i" << y; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | Complex C1(2, 3), C2(4, 5), C3; 34 | C3 = C1 + C2; 35 | C3.show(); 36 | } -------------------------------------------------------------------------------- /C++/Ak9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | long factorial = 1.0; 7 | 8 | cout << "Enter a positive integer: "; 9 | cin >> n; 10 | 11 | if (n < 0) 12 | cout << "Error! Factorial of a negative number doesn't exist."; 13 | else { 14 | for(int i = 1; i <= n; ++i) { 15 | factorial *= i; 16 | } 17 | cout << "Factorial of " << n << " = " << factorial; 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C++/Ambiguity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class B1 5 | { 6 | public: 7 | void show_data() 8 | { 9 | cout << "\nIn the Base Class 1" << endl; 10 | } 11 | }; 12 | 13 | class B2 14 | { 15 | public: 16 | void show_data() 17 | { 18 | cout << "\nIn the Base Class 2" << endl; 19 | } 20 | }; 21 | 22 | class D : public B1, public B2 23 | { 24 | public: 25 | void display() 26 | { 27 | B1::show_data(); 28 | B2::show_data(); 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | D obj; 35 | // obj.show_data(); // Ambiguity 36 | // Solution 37 | // obj.B1::show_data(); 38 | // obj.B2::show_data(); 39 | // or 40 | obj.display(); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /C++/Any.cpp: -------------------------------------------------------------------------------- 1 | // Factorial of n = 1*2*3*...*n 2 | 3 | #include 4 | using namespace std; 5 | 6 | int factorial(int); 7 | 8 | int main() { 9 | int n, result; 10 | 11 | cout << "Enter a non-negative number: "; 12 | cin >> n; 13 | 14 | result = factorial(n); 15 | cout << "Factorial of " << n << " = " << result; 16 | return 0; 17 | } 18 | 19 | int factorial(int n) { 20 | if (n > 1) { 21 | return n * factorial(n - 1); 22 | } else { 23 | return 1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /C++/AreaOfCircle.cpp: -------------------------------------------------------------------------------- 1 | // Using PUBLIC Access Specifier 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Circle{ 7 | public: 8 | double radius; 9 | double compute_area() 10 | { 11 | return 3.14 * radius * radius; 12 | } 13 | }; 14 | int main() 15 | { 16 | Circle obj; 17 | obj.radius = 5.5; 18 | cout << "Radius is: " << obj.radius << "\n"; 19 | cout << "Area is: "< 27 | // using namespace std; 28 | // class Circle{ 29 | // private: 30 | // double radius; 31 | // public: 32 | // void compute_area(double r) 33 | // { 34 | // radius = r; 35 | // double area = 3.14 * radius * radius; 36 | // cout << "Radius is : " << radius << endl; 37 | // cout << "Area is : " << area << endl; 38 | 39 | // } 40 | // } -------------------------------------------------------------------------------- /C++/AreaOfRectangleUsingConstructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Rectangle{ 5 | private: 6 | int length; 7 | int breadth; 8 | public: 9 | Rectangle(int a, int b){ 10 | length = a; 11 | breadth = b; 12 | } 13 | 14 | int area(){ 15 | int area = (length * breadth); 16 | cout << "Area of Rectangle is: " << area << endl; 17 | } 18 | }; 19 | 20 | int main(){ 21 | Rectangle r(10, 20); 22 | r.area(); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /C++/ArrayOfObjects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class employee 5 | { 6 | char name[50]; 7 | int age; 8 | public: 9 | void getdata(void); 10 | void putdata(void); 11 | }; 12 | 13 | void employee::getdata(void) 14 | { 15 | cout<<"\n Enter the name of the Employee: "; 16 | cin>>name; 17 | cout<<"\n Enter the age of Employee: "; 18 | cin>>age; 19 | } 20 | 21 | void employee::putdata(void) 22 | { 23 | cout<<"\n Entered name is: "< 2 | using namespace std; 3 | class THuman 4 | { 5 | public: 6 | string name; 7 | int age,height; 8 | }; 9 | int main() 10 | { 11 | THuman *worker[1000]; 12 | for(int i=0;i<5;i++) 13 | { 14 | worker[i]=new THuman; 15 | } 16 | for(int i=0;i<5;i++) 17 | { 18 | cin>>worker[i]->name>>worker[i]->age>>worker[i]->height; 19 | } 20 | for(int i=0;i<5;i++) 21 | { 22 | cout<name<<" "<age<<" "<height< 2 | using namespace std; 3 | struct emp 4 | { 5 | string name; 6 | }; 7 | 8 | int main() 9 | { 10 | emp e[5]; 11 | int i; 12 | for(i=0;i<5;i++) 13 | { 14 | cout<<"Enter the name of the Employee no."<>e[i].name; 16 | } 17 | 18 | cout<<"\nNames of all employees: \n"; 19 | for (i = 0; i < 5; i++) 20 | { 21 | cout< 2 | using namespace std; 3 | union Data 4 | { 5 | int intValue; 6 | char charValue; 7 | double doubleValue; 8 | }; 9 | 10 | int main() 11 | { 12 | const int SIZE = 5; 13 | Data dataArray[SIZE]; 14 | } 15 | 16 | // Populate the ARRAY with DATA 17 | dataArray[0].intValue = 42; 18 | dataArray[1].charValue = 'A'; 19 | dataArray[2].doubleValue = 3.14; 20 | dataArray[3].intValue = 100; 21 | dataArray[4].charValue = 'Z'; 22 | 23 | // Access and display the data from the ARRAY 24 | cout<<"Array of Union: "< elements[i + 1]: 7 | swapped = True 8 | # swapping data if the element is less than next element in the array 9 | elements[i], elements[i + 1] = elements[i + 1], elements[i] 10 | if not swapped: 11 | # exiting the function if we didn't make a single swap 12 | # meaning that the array is already sorted. 13 | return 14 | 15 | elements = [39, 12, 18, 85, 72, 10, 2, 18] 16 | 17 | print("Unsorted list is,") 18 | print(elements) 19 | bubblesort(elements) 20 | print("Sorted Array is, ") 21 | print(elements) 22 | -------------------------------------------------------------------------------- /C++/Calculator.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char op; 7 | float num1, num2; 8 | 9 | cout << "Enter operator: +, -, *, /: "; 10 | cin >> op; 11 | 12 | cout << "Enter two operands: "; 13 | cin >> num1 >> num2; 14 | 15 | switch(op) { 16 | 17 | case '+': 18 | cout << num1 << " + " << num2 << " = " << num1 + num2; 19 | break; 20 | 21 | case '-': 22 | cout << num1 << " - " << num2 << " = " << num1 - num2; 23 | break; 24 | 25 | case '*': 26 | cout << num1 << " * " << num2 << " = " << num1 * num2; 27 | break; 28 | 29 | case '/': 30 | cout << num1 << " / " << num2 << " = " << num1 / num2; 31 | break; 32 | 33 | default: 34 | // If the operator is other than +, -, * or /, error message is shown 35 | cout << "Error! operator is not correct"; 36 | break; 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /C++/Check Armstrong number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num, originalNum, remainder, result = 0; 6 | cout << "Enter a three-digit integer: "; 7 | cin >> num; 8 | originalNum = num; 9 | 10 | while (originalNum != 0) { 11 | // remainder contains the last digit 12 | remainder = originalNum % 10; 13 | 14 | result += remainder * remainder * remainder; 15 | 16 | // removing last digit from the orignal number 17 | originalNum /= 10; 18 | } 19 | 20 | if (result == num) 21 | cout << num << " is an Armstrong number."; 22 | else 23 | cout << num << " is not an Armstrong number."; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /C++/ClassesContainingPointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Array 4 | { 5 | int *arr; int size; 6 | public: 7 | void get_data(int n) 8 | { 9 | size=n; 10 | arr=new int[size]; 11 | for(int i=0;i>(arr+i); 14 | } 15 | } 16 | 17 | void add() 18 | { 19 | int sum=0; 20 | for(int i=0;i>"\n Enter the size of array "; 33 | a.get_data(n); 34 | a.add(); return 0;} -------------------------------------------------------------------------------- /C++/ConstructorListInitialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // class rectangle{ 5 | // int l,b; 6 | // public: 7 | // rectangle(int x=12, int y=34):l(x),b(y){ 8 | // cout<<"Constructor called"< 2 | using namespace std; 3 | 4 | class rectangle{ 5 | int l,b; 6 | public: 7 | rectangle(int x=12, int y=34){ 8 | l = x; 9 | b = y; 10 | } 11 | int area(){ 12 | return l*b; 13 | } 14 | }; 15 | int main() 16 | { 17 | rectangle r; 18 | cout<<"Area is "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string s1, s2; 7 | 8 | cout << "Enter string s1: "; 9 | getline (cin, s1); 10 | 11 | s2 = s1; 12 | 13 | cout << "s1 = "<< s1 << endl; 14 | cout << "s2 = "<< s2; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /C++/CopyConstructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class rectangle 4 | { 5 | private: 6 | int length; 7 | int breadth; 8 | public: 9 | // Default Constructor method 10 | rectangle(int x = 12, int y = 34) 11 | { 12 | cout << "Constructor called" << endl; 13 | length = x; 14 | breadth = y; 15 | } 16 | // Copy Constructor method 17 | rectangle(rectangle &r) 18 | { 19 | cout << "Copy Constructor called" << endl; 20 | length = r.length; 21 | breadth = r.breadth; 22 | } 23 | 24 | int area() 25 | { 26 | return length * breadth; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | rectangle r1; 33 | rectangle r2(r1); 34 | cout << "Area of rectangle r1 is: " << r1.area() << endl; 35 | cout << "Area of rectangle r2 is: " << r2.area() << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /C++/CountSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void countSort(int arr[], int size){ 5 | int countSize=arr[0]; 6 | for(int i=1;i countArr(countSize+1,0); 11 | for(int i=0;i=0;i--){ 19 | output[--countArr[arr[i]]]=arr[i]; 20 | } 21 | 22 | for(int i=0;i>n; 31 | int* arr=new int[n]; 32 | cout<<"Enter "<>arr[i]; 35 | } 36 | countSort(arr,5); 37 | delete[] arr; 38 | } 39 | -------------------------------------------------------------------------------- /C++/Count_letter_frequency.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void countFrequency(int arr[], int n) 5 | { 6 | unordered_map map; 7 | 8 | for (int i = 0; i < n; i++) 9 | map[arr[i]]++; //store array element as key and frequency as value 10 | 11 | for (auto x : map) 12 | cout << x.first << " " << x.second << endl; //x.first -> key and x.second -> value 13 | } 14 | 15 | int main() 16 | { 17 | int arr[] = {10,5,10,15,10,5}; 18 | int n = sizeof(arr) / sizeof(arr[0]); 19 | countFrequency(arr, n); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C++/Cpp_pattern/C++_invert_V-shape.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | /* 4 | * 5 | * * 6 | * * 7 | * * 8 | * * 9 | */ 10 | int main(){ 11 | cout << "--->"; 12 | int a; 13 | cin >> a; 14 | for(int i =0;i 2 | using namespace std; 3 | /* 4 | * * * * * * * * * 5 | * * * * * * * 6 | * * * * * 7 | * * * 8 | * 9 | */ 10 | main(){ 11 | cout << "--->"; 12 | int a; 13 | cin >> a; 14 | for(int i=0;i 2 | using namespace std; 3 | /* 4 | * * * * * * * * * 5 | * * * * * * * 6 | * * * * * 7 | * * * 8 | * 9 | */ 10 | int main(){ 11 | cout << "--->"; 12 | int a; 13 | cin >> a; 14 | for(int i =0;i 2 | using namespace std; 3 | /* 4 | ---> 6 5 | * * * * * * 6 | * * * * * 7 | * * * * 8 | * * * 9 | * * 10 | * 11 | */ 12 | 13 | main(){ 14 | cout<< "--->"; 15 | int a; 16 | cin >> a; 17 | for(int i =0;i 2 | using namespace std; 3 | class X 4 | { 5 | public: 6 | int a; 7 | void f(int b) 8 | { 9 | cout<<"The value of b is "< 2 | using namespace std; 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | Node(int x){ 8 | data=x; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | void printlist(Node *head){ 14 | Node *curr=head; 15 | while(curr!=NULL){ 16 | cout<data<<" "; 17 | curr=curr->next; 18 | }cout<next==NULL){ 24 | delete head; 25 | return NULL; 26 | } 27 | Node *curr=head; 28 | while(curr->next->next!=NULL) 29 | curr=curr->next; 30 | delete (curr->next); 31 | curr->next=NULL; 32 | return head; 33 | } 34 | 35 | int main() 36 | { 37 | Node *head=new Node(10); 38 | head->next=new Node(20); 39 | head->next->next=new Node(30); 40 | printlist(head); 41 | head=delTail(head); 42 | printlist(head); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /C++/Destructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class counter 6 | { 7 | int id; 8 | public: 9 | counter(int i) 10 | { 11 | id = i; 12 | cout<<"Constructor of object with ID: "< 2 | using namespace std; 3 | struct Node{ 4 | int data; 5 | struct Node *next; 6 | }*first = NULL; 7 | void Create(int A[], int n){ 8 | int i; 9 | struct Node *t, *last; 10 | first = new Node(); 11 | first->data = A[0]; 12 | first->next = NULL; 13 | last = first; 14 | for(i =1; idata = A[i]; 17 | t->next = NULL; 18 | last->next = t; 19 | last =t; 20 | } 21 | } 22 | void Display(struct Node *p) 23 | { 24 | while(p!=NULL) 25 | { 26 | printf("%d ",p->data); 27 | p=p->next; 28 | } 29 | } 30 | void RDisplay(struct Node *p) 31 | { 32 | if(p!=NULL) 33 | { 34 | RDisplay(p->next); 35 | printf("%d ",p->data); 36 | 37 | } 38 | } 39 | int main(){ 40 | int A[] = {0,1,2,3,4,5,6,7,8,9}; 41 | Create(A, 10); 42 | RDisplay(first); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /C++/Display1to10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int i; 5 | for (i = 1; i < 101; i++) 6 | { 7 | cout< 2 | using namespace std; 3 | enum color { black=1, blue, red}; 4 | int main() 5 | { 6 | color obj = blue; 7 | cout << obj; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /C++/Factorial.cpp: -------------------------------------------------------------------------------- 1 | // Factorial of n = 1*2*3*...*n 2 | 3 | #include 4 | using namespace std; 5 | 6 | int factorial(int); 7 | 8 | int main() { 9 | int n, result; 10 | 11 | cout << "Enter a non-negative number: "; 12 | cin >> n; 13 | 14 | result = factorial(n); 15 | cout << "Factorial of " << n << " = " << result; 16 | return 0; 17 | } 18 | 19 | int factorial(int n) { 20 | if (n > 1) { 21 | return n * factorial(n - 1); 22 | } else { 23 | return 1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /C++/FileHandling-Create.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | ofstream of("result.txt"); 9 | of<<"Hello"; 10 | of.close(); 11 | cout<<"Data Saved\n"; 12 | } -------------------------------------------------------------------------------- /C++/FileHandling-OpenFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ofstream fout; 8 | fout.open("result.txt"); 9 | fout<<"India\n"; 10 | fout<<"USA\n"; 11 | fout<<"UK\n"; 12 | fout.close(); 13 | 14 | fout.open("result1.txt"); 15 | fout<<"Delhi\n"; 16 | fout<<"Washington\n"; 17 | fout<<"London\n"; 18 | fout.close(); 19 | 20 | ifstream fin; 21 | fin.open("result.txt"); 22 | char ch; 23 | while(fin) 24 | { 25 | fin.get(ch); 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // ifstream rf("result.txt"); 8 | // string line; 9 | // // Without Loop 10 | // getline(rf, line); 11 | // cout << line << endl; 12 | 13 | // With Loop 14 | // while (getline(of, line)) 15 | // { 16 | // cout << line << endl; 17 | // } 18 | 19 | char s[10]; 20 | ifstream inf("result.txt"); 21 | inf >> s; 22 | inf.close(); 23 | cout << s << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /C++/FileHandling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char s[30]; 8 | ofstream of("result.txt"); 9 | // Manual Input 10 | string userInput; 11 | cout << "Enter whatever you want to write: " << endl; 12 | getline(cin, userInput); 13 | of << userInput; 14 | of.close(); 15 | 16 | cout << "Data Saved to File" << endl; 17 | 18 | ifstream ifs("result.txt"); 19 | ifs >> s; 20 | cout << s << endl; 21 | ifs.close(); 22 | 23 | return 0; 24 | } 25 | 26 | 27 | // Can also be done by using cin instead of getline by passing char in cin -------------------------------------------------------------------------------- /C++/FileModes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | char s[30]; 6 | ofstream fileout; 7 | ifstream filein; 8 | fileout.open("result.txt", ios::out); 9 | fileout<<"Hello World"; 10 | fileout.close(); 11 | 12 | filein.open("result.txt", ios::in); 13 | filein.getline(s, 30); 14 | cout< 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ofstream myFile; 10 | ifstream file; 11 | char ch[30]; 12 | char text; 13 | 14 | file.open("result.txt", ios::in); 15 | if (file) 16 | { 17 | file.seekg(7, ios::beg); 18 | cout<<"The output after skipping first n bytes is: "; 19 | while(!file.eof()) 20 | { 21 | file.get(text); 22 | cout< 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | ofstream myFile; 10 | ifstream file; 11 | char ch[30]; 12 | char text; 13 | 14 | cout<<"Enter some text here: "< 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | ofstream fp; // Object of type ofstream 9 | 10 | fp.open("result.txt", ios::out); // Open file 11 | if (fp) 12 | { 13 | fp<<"Iamneo.ai"< 2 | using namespace std; 3 | int FindMaxNum(int num1, int num2, int num3); 4 | int main(){ 5 | int a; 6 | cout << "Enter your first number: " << endl; 7 | cin >> a; 8 | int b; 9 | cout << "Enter your second number: " << endl; 10 | cin >> b; 11 | int c; 12 | cout << "Enter your third number: " << endl; 13 | cin >> c; 14 | int MaxNum = FindMaxNum(a,b,c); 15 | cout << "Maximum number is: " << MaxNum << endl; 16 | } 17 | 18 | int FindMaxNum(int num1, int num2, int num3){ 19 | if(num1>num2 && num1>num3){ 20 | return num1; 21 | } 22 | else if(num2>num1 && num2>num3){ 23 | return num2; 24 | } 25 | else{ 26 | return num3; 27 | } 28 | } -------------------------------------------------------------------------------- /C++/FriendClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class GFG{ 5 | private: 6 | int private_variable; 7 | protected: 8 | int protected_variable; 9 | public: 10 | GFG() 11 | { 12 | private_variable = 10; 13 | protected_variable = 99; 14 | } 15 | friend class FriendClass; 16 | }; 17 | 18 | class FriendClass{ 19 | public: 20 | void display(GFG& x) 21 | { 22 | cout << "Private Variable: " << x.private_variable << endl; 23 | cout << "Protected Variable: " << x.protected_variable << endl; 24 | } 25 | }; 26 | 27 | int main(){ 28 | GFG g; 29 | FriendClass f; 30 | f.display(g); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /C++/FunctionOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | public: 7 | void show() 8 | { 9 | cout 10 | } 11 | } -------------------------------------------------------------------------------- /C++/Functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct profile 4 | { 5 | int id; 6 | }; 7 | struct employee 8 | { 9 | profile p; 10 | }; 11 | 12 | int main() 13 | { 14 | employee e; 15 | int &ref=e.p.id; 16 | ref=34; 17 | cout< 5 | using namespace std; 6 | int main() { 7 | int n, i, j; 8 | cout << "Enter number of lines: "; 9 | cin >> n; 10 | for(i = 1; i<=n; i++) { 11 | for(j = 1; j<=(n-i); j++) { //print the blank spaces before star 12 | cout << " "; 13 | } 14 | if(i == 1 || i == n) { //for the first and last line, print the stars continuously 15 | for(j = 1; j<=i; j++) { 16 | cout << "* "; 17 | } 18 | }else{ 19 | cout << "*"; //in each line star at start and end position 20 | for(j = 1; j<=2*i-3; j++) { //print space to make hollow 21 | cout << " "; 22 | } 23 | cout << "*"; 24 | } 25 | cout << endl; 26 | } 27 | } -------------------------------------------------------------------------------- /C++/InlineMemberFunc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class operation 4 | { 5 | int a, b, add; 6 | public: 7 | void get() // Inline Member Function 8 | { 9 | cout << "Enter first value:"; 10 | cin >> a; 11 | cout << "Enter second value:"; 12 | cin >> b; 13 | } 14 | 15 | void sum(); 16 | }s; 17 | inline void operation :: sum() 18 | { 19 | add = a + b; 20 | cout << "Addition of two numbers: " << add; 21 | } 22 | 23 | int main() 24 | { 25 | cout << "Program using inline function\n"; 26 | s.get(); 27 | s.sum(); 28 | return 0; 29 | } 30 | 31 | // Functions which are declared in a class declaration and defined outside the class is known as : 32 | // Inline Member Function because it has the keyword inline before the return type of the function. -------------------------------------------------------------------------------- /C++/Largest subarray 0 sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | //SOLUTION 3 | class Solution 4 | { 5 | 6 | 7 | public: 8 | 9 | int maxLen (vector < int >&A, int n) 10 | 11 | { 12 | 13 | 14 | int maxi = 0; 15 | 16 | 17 | int sum = 0; 18 | 19 | 20 | unordered_map < int, int >m; 21 | 22 | 23 | for (int i = 0; i < n; i++) 24 | 25 | 26 | { 27 | 28 | 29 | sum += A[i]; 30 | 31 | 32 | if (sum == 0) 33 | 34 | 35 | { 36 | 37 | 38 | maxi = i + 1; 39 | 40 | 41 | } 42 | 43 | 44 | else 45 | 46 | 47 | { 48 | 49 | 50 | if (m.find (sum) != m.end ()) 51 | 52 | 53 | { 54 | 55 | 56 | maxi = max (maxi, i - m[sum]); 57 | 58 | 59 | } 60 | 61 | 62 | else 63 | 64 | 65 | { 66 | 67 | 68 | m[sum] = i; 69 | 70 | 71 | } 72 | 73 | 74 | } 75 | 76 | 77 | } 78 | 79 | 80 | return maxi; 81 | 82 | 83 | } 84 | 85 | 86 | }; 87 | -------------------------------------------------------------------------------- /C++/Leap Year.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int yr; 6 | cout<<"Enter the Year: "; 7 | cin>>yr; 8 | if((yr%4==0) && (yr%100!=0)) 9 | cout<<"\nIt is a Leap Year"; 10 | else if(yr%400==0) 11 | cout<<"\nIt is a Leap Year"; 12 | else 13 | cout<<"\nIt is not a Leap Year"; 14 | cout< 2 | using namespace std; 3 | void linear(int a[], int n) { 4 | int temp = -1; 5 | for (int i = 0; i < 6; i++) { 6 | if (a[i] == n) { 7 | cout << "Element found at place: " << i + 1 << endl; 8 | temp = 0; 9 | break; 10 | } 11 | } 12 | if (temp == -1) { 13 | cout << "Element Not Found" << endl; 14 | } 15 | } 16 | int main() { 17 | int arr[6]; 18 | cout << "Please enter 6 elements of the Array" << endl; 19 | for (int i = 0; i < 6; i++) { 20 | cin >> arr[i]; 21 | } 22 | cout << "Please enter an element to search" << endl; 23 | int num; 24 | cin >> num; 25 | linear(arr, num); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /C++/MaxNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int arr[5] = { 1, 2, 5, 4, 3 }; 5 | int max = 0; 6 | for(int i = 0;i<5;i++) 7 | { 8 | if(max < arr[i]) 9 | max = arr[i]; 10 | } 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | double num1, num2, product; 6 | cout << "Enter two numbers: "; 7 | 8 | // stores two floating point numbers in num1 and num2 respectively 9 | cin >> num1 >> num2; 10 | 11 | // performs multiplication and stores the result in product variable 12 | product = num1 * num2; 13 | 14 | cout << "Product = " << product; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /C++/NonInlineMemberFunc.cpp: -------------------------------------------------------------------------------- 1 | // Non Inline Member Function Definition 2 | // Functions defined outside the class are called non-inline member functions, even though they are defined inside the class. 3 | 4 | #include 5 | using namespace std; 6 | int square(int s) // Non Inline Member Function Declaration 7 | { 8 | return s*s; 9 | } 10 | 11 | int main(){ 12 | cout<<"Enter number to compute its square: 5"< 2 | using namespace std; 3 | class Rectangle 4 | { 5 | public: 6 | int length, breadth; 7 | int area() 8 | { 9 | return length * breadth; 10 | } 11 | int Perimeter(); 12 | }; 13 | int Rectangle::Perimeter() 14 | { 15 | return 2 * (length + breadth); 16 | } 17 | 18 | int main() 19 | { 20 | Rectangle r; 21 | 22 | Rectangle *p = new Rectangle(); 23 | // r.length = 10; 24 | p->length = 10; 25 | p->breadth = 5; 26 | cout << p->area(); 27 | cout << p->Perimeter(); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C++/OperatorOverloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class abc 5 | { 6 | private: 7 | int x; 8 | int y; 9 | int z; 10 | public: 11 | abc(int a,int b,int c) 12 | { 13 | x=a; 14 | y=b; 15 | z=c; 16 | } 17 | 18 | void display() 19 | { 20 | cout< 2 | using namespace std; 3 | 4 | class A 5 | { 6 | int x; 7 | public: 8 | A(int i) 9 | { 10 | x = i; 11 | cout << "Calling Base Class - Parameterized " << x << endl; 12 | } 13 | ~A() 14 | { 15 | cout << "Calling Base Class - Destructor" << endl; 16 | } 17 | }; 18 | 19 | class B: public A 20 | { 21 | int l; 22 | public: 23 | B(int p):A(p) 24 | { 25 | l=p; 26 | cout << "Calling Derived Class - Parameterized " << l << endl; 27 | } 28 | ~B() 29 | { 30 | cout << "Calling Base Class - Destructor" << endl; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | B obj2(1); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /C++/OrderOfExecution.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A 5 | { 6 | public: 7 | A() 8 | { 9 | cout << "\nCalling default Base Class constructor"; 10 | } 11 | ~A() 12 | { 13 | cout << "\nCalling Base Class destructor"; 14 | } 15 | }; 16 | 17 | class B:public A 18 | { 19 | public: 20 | B() 21 | { 22 | cout << "\nCalling default Derived Class constructor"; 23 | } 24 | ~B() 25 | { 26 | cout << "\nCalling Derived Class destructor"; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | B b; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /C++/OverloadAdditionOperator.cpp: -------------------------------------------------------------------------------- 1 | // You are using GCC 2 | #include 3 | #include 4 | using namespace std; 5 | class sum 6 | { 7 | public: 8 | int b,a; 9 | sum() //constructor 10 | {b=50;} 11 | void set_data() 12 | { 13 | cout<<"Enter A: "<>a; 15 | } 16 | int operator+(sum s1) 17 | { 18 | return(a+s1.b); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | int d; 25 | sum s,t; 26 | s.set_data(); 27 | d=s+t; 28 | cout<<"SUM is : "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, num, digit, rev = 0; 7 | 8 | cout << "Enter a positive number: "; 9 | cin >> num; 10 | 11 | n = num; 12 | 13 | do 14 | { 15 | digit = num % 10; 16 | rev = (rev * 10) + digit; 17 | num = num / 10; 18 | } while (num != 0); 19 | 20 | cout << " The reverse of the number is: " << rev << endl; 21 | 22 | if (n == rev) 23 | cout << " The number is a palindrome."; 24 | else 25 | cout << " The number is not a palindrome."; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /C++/PassingByReference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Function definition to swap the values 4 | void swap(int &x, int &y) 5 | { 6 | int temp; 7 | temp = x; /* save the value at address x */ 8 | x = y; /* put y into x */ 9 | y = temp; /* put x into y */ 10 | return; 11 | } 12 | int main() 13 | { 14 | // Local variable declaration: 15 | int a = 100; 16 | int b = 200; 17 | cout << "Before swap, value of a :" << a << endl; 18 | cout << "Before swap, value of b :" << b << endl; 19 | /* calling a function to swap the values.*/ 20 | swap(a, b); 21 | cout << "After swap, value of a :" << a << endl; 22 | cout << "After swap, value of b :" << b << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /C++/Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | void solve(vectornums, vector>&ans, int index) 4 | { 5 | if(index>=nums.size()) 6 | { 7 | ans.push_back(nums); 8 | return; 9 | } 10 | 11 | for(int j=index; j> permute(vector& nums) { 20 | vector>ans; 21 | int index = 0; 22 | solve(nums, ans, index); 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /C++/PointerinObjects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Person 4 | { 5 | char name[30]; 6 | int age; 7 | public: 8 | void getData() 9 | { 10 | cout << "Enter name: "; 11 | cin >> name; 12 | cout << "Enter age: "; 13 | cin >> age; 14 | } 15 | void display() 16 | { 17 | cout << "\nName: " << name; 18 | cout << "\nAge: " << age; 19 | } 20 | }; 21 | 22 | int main(){ 23 | Person x, *ptr; 24 | ptr = &x; 25 | ptr->getData(); 26 | ptr->display(); 27 | cout << endl; 28 | (*ptr).getData(); 29 | (*ptr).display(); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /C++/PointersToObject.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class X 4 | { 5 | public: 6 | int a; 7 | void f(int b) 8 | { 9 | cout << "The value of b is " << b << endl; 10 | } 11 | }; 12 | 13 | int main(){ 14 | // declare pointer to data member 15 | int X::*ptra = &X::a; 16 | // declare pointer to member function 17 | void (X::*ptrf)(int) = &X::f; 18 | // declare object of class X 19 | X xobject; 20 | // initialize data member 21 | xobject.*ptra = 10; 22 | // call member functions 23 | (xobject.*ptrf)(20); 24 | } -------------------------------------------------------------------------------- /C++/Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int rec(int a) 5 | { 6 | int b; 7 | if(a<=1) 8 | { 9 | return(1); 10 | } 11 | else 12 | { 13 | b=a*rec(a-1); 14 | return(b); 15 | } 16 | } 17 | 18 | int main(){ 19 | int rec(int); 20 | int n, fact; 21 | cout<<"Enter a number: "; 22 | cin>>n; 23 | fact=rec(n); 24 | cout<<"Factorial of "< 2 | using namespace std; 3 | 4 | void Reverse_Queue_using_Stack(queue &q) 5 | { 6 | stack s; 7 | while(!q.empty()) 8 | { 9 | s.push(q.front()); 10 | q.pop(); 11 | } 12 | while(!s.empty()) 13 | { 14 | q.push(s.top()); 15 | s.pop(); 16 | } 17 | } 18 | 19 | void Reverse_Queue_using_Recursion(queue &q) 20 | { 21 | int a=q.front(); 22 | if(q.size()==1) return; 23 | q.pop(); 24 | Reverse_Queue_using_Recrustion(q); 25 | q.push(a); 26 | } 27 | 28 | void Show(queue q) 29 | { 30 | int a=q.front();q.pop(); 31 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string str1=("Lovely Professional University"); 7 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string s1 = "arlcome"; 7 | string s2 = "wearomea"; 8 | 9 | string s3 = "welcome"; 10 | string s4; 11 | s4 = s3.substr(0,2); 12 | 13 | cout << s1.find("lc") << endl; 14 | cout << s2.find("ea") << endl; 15 | cout << s1.find_last_of("e") << endl; 16 | cout< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string str="Welcome"; 7 | // Size of String 8 | cout<<"Size: " << str.size() << endl; 9 | // Length of String 10 | cout<<"Length: " << str.length() << endl; 11 | // Max Size of String 12 | cout<<"Max Size: " << str.max_size() << endl; 13 | // Tells if String is empty or not 14 | cout<<"Empty: " << (str.empty()?"Yes":"No") << endl; 15 | return 0; 16 | } -------------------------------------------------------------------------------- /C++/StringOperations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string str1 = "lovely professional university"; 7 | string str2 = "ve"; 8 | cout << str1 << endl; 9 | int x = str1.find(str2); 10 | cout << x << endl; 11 | 12 | string temp = str1.substr(x + str2.size(), 30 ); 13 | cout << temp << endl; 14 | 15 | int y = temp.find(str2); 16 | cout << y << endl; 17 | cout << y+x+str2.size() << endl; 18 | } -------------------------------------------------------------------------------- /C++/Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | main() 5 | { 6 | string s1("man"); 7 | string s2("hi"); 8 | string s3; 9 | s3 = s1; 10 | cout << s3 << endl; 11 | 12 | s3="neither" + s1 + "nor"; 13 | s3+=s2; 14 | cout << s3 << endl; 15 | 16 | s1.swap(s2); 17 | cout << s1 << "nor" << endl; 18 | } -------------------------------------------------------------------------------- /C++/StringsOperation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string s1("12345"); 7 | string s2("abcde"); 8 | s1.append(s2); 9 | cout << s1 << endl; 10 | 11 | s1.append(s2, 1, 2); 12 | cout << s1 << endl; 13 | 14 | cout< 2 | using namespace std; 3 | struct employee 4 | { 5 | char name[50]; 6 | int age; 7 | float salary; 8 | }; 9 | 10 | int main() 11 | { 12 | employee e1; 13 | cout << "Enter Full name: "; 14 | cin.get(e1.name, 50); 15 | cout << "Enter age: "; 16 | cin >> e1.age; 17 | cout << "Enter salary: "; 18 | cin >> e1.salary; 19 | cout << "\nDisplaying Information." << endl; 20 | cout << "Name: " << e1.name << endl; 21 | cout << "Age: " << e1.age << endl; 22 | cout << "Salary: " << e1.salary; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /C++/ThisPointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Test 4 | { 5 | int a, b; 6 | public: 7 | void show() 8 | { 9 | a=10; 10 | b=20; 11 | cout<<"object address is "< 3 | using namespace std; 4 | // Function to demonstrate printing pattern 5 | 6 | void tri(int n) 7 | { 8 | int i = 0, j = 0, k = 0; 9 | 10 | while (i < n) { 11 | // for spacing 12 | 13 | while (k <= n - i - 2) { 14 | cout << " "; 15 | k++; 16 | } 17 | k = 0; 18 | 19 | // For Pattern printing 20 | 21 | while (j <= i) { 22 | cout << "* "; 23 | j++; 24 | } 25 | j = 0; 26 | i++; 27 | cout << endl; 28 | } 29 | } 30 | // Driver Code 31 | 32 | int main() 33 | { 34 | int n = 5; 35 | 36 | // Function Call 37 | tri(n); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /C++/UnaryOperator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class point 3 | { 4 | int x,y; 5 | public: 6 | point() 7 | { 8 | x=0;y=0; 9 | } 10 | point(int i, int j) 11 | { 12 | x=i;y=j; 13 | } 14 | point operator-(); 15 | void display() 16 | { 17 | cout< 2 | using namespace std; 3 | class Base 4 | { 5 | public: 6 | virtual void fun()=0; 7 | }; 8 | class Derived : public Base 9 | { 10 | public: 11 | void fun() 12 | { 13 | cout << "Derived class" << endl; 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | Base b; 20 | Derived d; 21 | Base *ptr = &d; 22 | ptr->fun(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /C++/binary search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // A recursive binary search function. 5 | int binarySearch(int arr[], int l, int r, int x) 6 | { 7 | if (r >= l) 8 | { 9 | int mid = l + (r - l) / 2; 10 | 11 | if (arr[mid] == x) 12 | return mid; 13 | 14 | if (arr[mid] > x) 15 | return binarySearch(arr, l, mid - 1, x); 16 | 17 | return binarySearch(arr, mid + 1, r, x); 18 | } 19 | 20 | return -1; 21 | } 22 | 23 | int main(void) 24 | { 25 | int arr[] = {2, 3, 4, 10, 40}; 26 | int x = 10; 27 | int n = sizeof(arr) / sizeof(arr[0]); 28 | int result = binarySearch(arr, 0, n - 1, x); 29 | (result == -1) 30 | ? cout << "Element is not present in array" 31 | : cout << "Element is present at index " << result; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C++/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void bubbleSort(int arr[], int n){ 6 | int i, j; 7 | bool swapp; 8 | for (i = 0; i < n-1; i++){ 9 | swapp = false; 10 | for (j=0;jarr[j+1]){ 12 | swap(arr[j], arr[j+1]); 13 | swapp= true; 14 | } 15 | } 16 | 17 | if (swapp == false) 18 | break; 19 | } 20 | } 21 | 22 | void printArray(int arr[], int size){ 23 | int i; 24 | for(i=0;idata << " "; 13 | temp = temp->next; 14 | } while (temp != head); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /C++/cs50.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a = 5, b = 10, temp; 7 | 8 | cout << "Before swapping." << endl; 9 | cout << "a = " << a << ", b = " << b << endl; 10 | 11 | temp = a; 12 | a = b; 13 | b = temp; 14 | 15 | cout << "\nAfter swapping." << endl; 16 | cout << "a = " << a << ", b = " << b << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C++/e4t7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { int x,temp,temp1,y,n,n1,n2,sum; 7 | cout<<"Enter a number: "; 8 | cin>>x; 9 | temp=x; 10 | temp1=x; 11 | n=0; 12 | while(x!=0) 13 | { x=x/10; 14 | n++; 15 | } 16 | 17 | sum=0; 18 | while(temp!=0) 19 | { n1=temp%10; 20 | n2=pow(n1,n); 21 | sum=sum+n2; 22 | temp=temp/10; 23 | } 24 | if (sum==temp1) 25 | { cout<<"It's an Armstrong number."; 26 | } 27 | else cout<<"It's not an Armstrong number."; 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /C++/factorial1.cpp: -------------------------------------------------------------------------------- 1 | // This program will print the pascal triangle 2 | #include 3 | using namespace std; 4 | int fact(int n){ 5 | int factorial=1; 6 | for (int i = 2; i <=n; i++) 7 | { 8 | factorial*=i; 9 | } 10 | return factorial; 11 | 12 | } 13 | int main(){ 14 | int n; 15 | cout<<"Enter the value of n: "; 16 | cin>>n; 17 | for(int i=0;i 2 | 3 | using namespace std; 4 | int main() { 5 | int n = 0, a = 0, b = 2; 6 | int c; 7 | int mod; 8 | cout << "Fibonacci series with the first 2 numbers as 0 and 2 is: "; 9 | cout << a << ", " << b; 10 | for (n = 0; n < 10; n++) { 11 | c = b + a; 12 | cout << ", "; 13 | cout << c; 14 | a = b; 15 | b = c; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /C++/find-bad-version.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstBadVersion(int n) { 4 | int low = 1, high = n; 5 | while(low<=high) 6 | { 7 | int mid = low + (high - low)/2; 8 | if(isBadVersion(mid)) 9 | { 10 | if(!isBadVersion(mid-1)) return mid; 11 | high = mid - 1; 12 | } 13 | else 14 | { 15 | low = mid + 1; 16 | } 17 | } 18 | assert(false); 19 | return -1; //unreachable code; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /C++/finding_roots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | float a,b,c,r1,r2; 8 | cout<<"Enter the value of a , b, c \t"; 9 | cin>>a>>b>>c; 10 | r1 = (-b+ sqrt(b*b-4*a*c))/(2*a); 11 | r2 = (-b- sqrt(b*b-4*a*c))/(2*a); 12 | cout<<"the two roots are "< 2 | using namespace std; 3 | class vehicle 4 | { 5 | public: 6 | vehicle() 7 | { 8 | cout<< "This is a vehicle\n"; 9 | } 10 | }; 11 | class Car: public vehicle 12 | { 13 | public: 14 | Car() 15 | { 16 | cout<< "This is a car\n"; 17 | } 18 | }; 19 | class Racing 20 | { 21 | public: 22 | Racing() 23 | { 24 | cout<< "This is for Racing\n"; 25 | } 26 | }; 27 | class Ferrari: public Car, public Racing 28 | { 29 | public: 30 | Ferrari() 31 | { 32 | cout<< "Ferrari is a Racing Car\n"; 33 | } 34 | 35 | }; 36 | int main() { 37 | Ferrari f; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /C++/insertion_sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for insertion sort 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to sort an array using 7 | // insertion sort 8 | void insertionSort(int arr[], int n) 9 | { 10 | int i, key, j; 11 | for (i = 1; i < n; i++) 12 | { 13 | key = arr[i]; 14 | j = i - 1; 15 | 16 | // Move elements of arr[0..i-1], 17 | // that are greater than key, to one 18 | // position ahead of their 19 | // current position 20 | while (j >= 0 && arr[j] > key) 21 | { 22 | arr[j + 1] = arr[j]; 23 | j = j - 1; 24 | } 25 | arr[j + 1] = key; 26 | } 27 | } 28 | 29 | // A utility function to print an array 30 | // of size n 31 | void printArray(int arr[], int n) 32 | { 33 | int i; 34 | for (i = 0; i < n; i++) 35 | cout << arr[i] << " "; 36 | cout << endl; 37 | } 38 | 39 | // Driver code 40 | int main() 41 | { 42 | int arr[] = { 12, 11, 13, 5, 6 }; 43 | int N = sizeof(arr) / sizeof(arr[0]); 44 | 45 | insertionSort(arr, N); 46 | printArray(arr, N); 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /C++/lowerbound.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int lower_bound(int arr[], int N, int X) 5 | { 6 | int mid; 7 | int low = 0; 8 | int high = N; 9 | 10 | while (low < high) { 11 | mid = low + (high - low) / 2; 12 | if (X <= arr[mid]) { 13 | high = mid; 14 | } 15 | 16 | else { 17 | low = mid + 1; 18 | } 19 | } 20 | 21 | if(low < N && arr[low] < X) { 22 | low++; 23 | } 24 | 25 | return low; 26 | } 27 | 28 | void printBound(int arr[], int N, int X) 29 | { 30 | int idx; 31 | 32 | if (lower_bound(arr, N, X) == N) { 33 | printf("Lower bound doesn't exist"); 34 | } 35 | else { 36 | 37 | idx = lower_bound(arr, N, X); 38 | printf("Lower bound of %d is" 39 | "% d at index % d\n ", 40 | X, 41 | arr[idx], idx); 42 | } 43 | 44 | } 45 | 46 | 47 | int main() 48 | { 49 | int arr[] = { 4, 6, 10, 12, 18, 20 }; 50 | int N = sizeof(arr) / sizeof(arr[0]); 51 | 52 | int X = 6; 53 | 54 | printBound(arr, N, X); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /C++/overlapping-intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eraseOverlapIntervals(vector>& intervals) { 4 | int count = 0; 5 | sort(intervals.begin(), intervals.end()); 6 | for(int i=1; iintervals[i][0]) 9 | { 10 | count++; 11 | intervals[i][1] = min(intervals[i-1][1], intervals[i][1]); 12 | } 13 | } 14 | 15 | return count; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /C++/palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n,r,sum=0,temp; 6 | cout<<"Enter the Number="; 7 | cin>>n; 8 | temp=n; 9 | while(n>0) 10 | { 11 | r=n%10; 12 | sum=(sum*10)+r; 13 | n=n/10; 14 | } 15 | if(temp==sum) 16 | cout<<"Number is Palindrome."; 17 | else 18 | cout<<"Number is not Palindrome."; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C++/printnnumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int i, num; 8 | 9 | // Take number from user 10 | cout << "Enter any number : "; 11 | cin >> num; 12 | 13 | cout << endl << "Natural numbers from 1 to " << num << endl; 14 | for(i = 1; i <= num; i++) { 15 | 16 | cout << i << " "; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /C++/removeDuplicatesInArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& numbers) { 4 | int insert_ptr {0}; 5 | int traverse_ptr {1}; 6 | int curr_value {numbers[0]}; 7 | 8 | while (traverse_ptr < (int)numbers.size()) { 9 | /* when you encounter a new number: 10 | 1) insert the old value in it's correct place in array 11 | 2) update the curr_value with the new value 12 | 3) move the insert_ptr to next index 13 | */ 14 | if (curr_value != numbers[traverse_ptr]) { 15 | numbers[insert_ptr] = curr_value; 16 | curr_value = numbers[traverse_ptr]; 17 | ++insert_ptr; 18 | } 19 | ++traverse_ptr; 20 | } 21 | 22 | // inserting the last unique element into the array 23 | numbers[insert_ptr] = curr_value; 24 | 25 | // since insert_ptr is an index, the no. of elements till that index would be insert_ptr + 1 26 | return insert_ptr + 1; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /C++/result.txt: -------------------------------------------------------------------------------- 1 | hello sup -------------------------------------------------------------------------------- /C++/right_trianglepattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n = 5; 7 | 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 1; j <= i; j++) 10 | cout << "* "; 11 | cout << endl; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C++/selectionsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void selectionsort(int [],int); 4 | int main() 5 | { 6 | int a[10],i,j,n,t,min; 7 | cout<< "enter value of n"; 8 | cin>>n; 9 | cout<<"enter array elements"; 10 | for(i=0;i>a[i]; 12 | selectionsort(a,n); 13 | } 14 | void selectionsort(int a[10],int n) 15 | { 16 | int i,j,t,min; 17 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int i,j,n; 6 | cout<<"Enter any number."; 7 | cin>>n; 8 | for(i=1;i<=n/2;i++) 9 | { 10 | for(j=1;j<=i;j++) 11 | { 12 | cout<<"* "; 13 | } 14 | for(j=i*2-1;j<=n-2;j++) 15 | cout<<" "; 16 | for(j=1;j<=i;j++) 17 | { 18 | cout<<"* "; 19 | } 20 | cout< 13 | 14 | #### For more information on how to contribute to open source projects, check out this [link](https://opensource.guide/how-to-contribute/) and [this](https://www.digitalocean.com/community/tutorial_series/an-introduction-to-open-source) and "How to Contribute to Open Source" by [freeCodeCamp](https://www.freecodecamp.org/news/how-to-contribute-to-open-source-projects/). 15 | -------------------------------------------------------------------------------- /Codechef/4a_Watermelon_cf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() 6 | { 7 | ll w; 8 | cin >> w; 9 | 10 | if (w % 2 == 0 && w>2) 11 | { 12 | cout << "YES"; 13 | } 14 | else 15 | { 16 | cout << "no"; 17 | } 18 | 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Codechef/ALTSTR.cpp: -------------------------------------------------------------------------------- 1 | // Problem Statement [ALTSTR](https://www.codechef.com/submit/ALTSTR) 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int T, N; 9 | string S; 10 | cin>>T; 11 | for(int i = 0; i < T; i++){ 12 | cin>>N; 13 | cin>>S; 14 | int ones = 0, zeros = 0; 15 | for(int i = 0; i < N; i++){ 16 | if(S[i] == '1') 17 | ones++; 18 | else 19 | zeros++; 20 | } 21 | int p = max(ones, zeros); 22 | int q = min(ones, zeros); 23 | if(p == q) 24 | cout<<2*q< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t, n; 8 | cin >> t; 9 | while (t--) 10 | { 11 | long long evencnt = 0, oddcnt = 0; 12 | cin >> n; 13 | for (int i = 0; i < n; i++) 14 | { 15 | long long x; 16 | cin >> x; 17 | if (x % 2 == 0) 18 | { 19 | evencnt++; 20 | } 21 | else 22 | { 23 | oddcnt++; 24 | } 25 | } 26 | if (oddcnt == 0) 27 | { 28 | cout << 0 << endl; 29 | } 30 | else 31 | { 32 | cout << evencnt << endl; 33 | } 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codechef/AUDIBLE.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t, x; 7 | cin >> t; 8 | while (t--) 9 | { 10 | cin >> x; 11 | if (x >= 67 && x <= 45000) 12 | { 13 | cout << "YES" << endl; 14 | } 15 | else 16 | { 17 | cout << "NO" << endl; 18 | } 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codechef/BALLON.cpp: -------------------------------------------------------------------------------- 1 | // Problem Statement [BALLOON](https://www.codechef.com/submit/BALLOON) 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int getresult(int arr[], int n) { 8 | if (n == 7) return n; 9 | 10 | int vib[8] = { 0 }, v, ans = -1; 11 | 12 | for (int i = 0; i < n; i++) { 13 | if (arr[i] >= 1 && arr[i] <= 7) 14 | vib[arr[i]] = 1; 15 | 16 | if (i <= 5) continue; 17 | 18 | for (v = 1; v <= 7; v++) { 19 | if (vib[v] == 0) 20 | break; 21 | } 22 | if (v == 8) { 23 | ans = i + 1; 24 | break; 25 | } 26 | } 27 | return ans; 28 | } 29 | 30 | int main() 31 | { 32 | int t; 33 | cin >> t; 34 | 35 | while (t--) { 36 | int n; 37 | cin >> n; 38 | 39 | int arr[n]; 40 | for (int i = 0; i < n; ++i) 41 | cin >> arr[i]; 42 | 43 | cout << getresult(arr, n) << endl; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Codechef/BINARYSUB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | long long int solve(long long int x){ 4 | long long int a[x+3]; 5 | a[0]=1; 6 | a[1]=1; 7 | a[2]=2; 8 | for(int i=3;i<=x;i++){ 9 | a[i]=((a[i-1]% 998244353)+(a[i-2]%998244353))%998244353; 10 | } 11 | return a[x]; 12 | } 13 | int main() { 14 | // your code goes here 15 | 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | string s; 20 | cin>>s; 21 | long long int ans=1; 22 | int count=1; 23 | for(int i=1;i1){ 28 | ans=((ans%998244353)*(solve(count)%998244353))%998244353; 29 | count=1; 30 | } 31 | } 32 | if(count>1){ 33 | ans=((ans%998244353)*(solve(count)%998244353))%998244353; 34 | count=1; 35 | } 36 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin >> t; 11 | 12 | while (t--) { 13 | int n; 14 | cin >> n; 15 | 16 | int arr[n]; 17 | for (int i = 0; i < n; i++) 18 | cin >> arr[i]; 19 | 20 | int ans = 0; 21 | 22 | for (int i = 0; i < n; i++) { 23 | if (i > 0 && arr[i] != arr[i - 1]) { 24 | ans++; 25 | continue; 26 | } 27 | if (i < n - 1 && arr[i] != arr[i + 1]) { 28 | ans ++; 29 | } 30 | } 31 | 32 | cout << ans << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Codechef/CP_solution(Chef and Wire Frames).py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | for i in range(t): 4 | n,m,x=map(int,input().split()) 5 | '''x--rupees 6 | n--length 7 | m--width''' 8 | a=(2*n)+(2*m) 9 | print(a*x) 10 | 11 | -------------------------------------------------------------------------------- /Codechef/DiceNumber.cpp: -------------------------------------------------------------------------------- 1 | // I have added solution for the problem : https://www.codechef.com/problems/DICENUM 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int t; cin>>t; 9 | while(t--){ 10 | int a[3]; for(int i=0;i<3;i++)cin>>a[i]; 11 | int b[3]; for(int j=0;j<3;j++) cin>>b[j]; 12 | sort(a,a+3); 13 | reverse(a,a+3); 14 | sort(b,b+3); 15 | reverse(b,b+3); 16 | bool alice = false; 17 | bool bob = false; 18 | for(int i=0;i<3;i++){ 19 | if(a[i]>b[i]){ 20 | alice = true; 21 | break; 22 | } 23 | else if(a[i] 2 | #define all(A) A.begin,A.end() 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | for(int i=0;i 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | int n; 10 | cin>>n; 11 | int a[n]; 12 | for(int i=0;i>a[i]; 14 | } 15 | sort(a, a+n); 16 | vectorb; 17 | for(int i=0;i 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // your code goes here 8 | int t; 9 | cin>>t; 10 | while(t--){ 11 | string s; 12 | bool b=false; 13 | cin>>s; 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | 13 | double d = pow(0.143 * n, n); 14 | 15 | int r = floor(d); 16 | 17 | if ((d - r) < 0.5) 18 | cout << r << '\n'; 19 | else 20 | cout << r + 1 << '\n'; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Codechef/FLIPCARDS.PY: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | 4 | for i in range(t): 5 | t, up = map(int, input().split()) 6 | 7 | print(min(up, t-up)) 8 | -------------------------------------------------------------------------------- /Codechef/HAPPYSTR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t, count = 0, max; 7 | string s; 8 | cin >> t; 9 | while (t--) 10 | { 11 | cin >> s; 12 | max = 0; 13 | for (int i = 0; i < s.size(); i++) 14 | { 15 | count = 0; 16 | for (int j = i; j < s.size(); j++) 17 | { 18 | if (s[j] == 'a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'o' || s[j] == 'u') 19 | { 20 | count++; 21 | } 22 | else 23 | { 24 | i = j; 25 | break; 26 | } 27 | } 28 | if (count > 2) 29 | { 30 | cout << "Happy" << endl; 31 | max = 1; 32 | break; 33 | } 34 | } 35 | if (max == 0) 36 | { 37 | cout << "Sad" << endl; 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Codechef/HOW_MANY_DIGITS_DO_I_HAVE.c: -------------------------------------------------------------------------------- 1 | /* 2 | Problem 3 | Write a program to obtain a number (N)(N) from the user and display whether the number is a one digit number, 2 digit number, 3 digit number or more than 3 digit number 4 | 5 | Input: 6 | First line will contain the number NN, 7 | Output: 8 | Print "1" if N is a 1 digit number. 9 | 10 | Print "2" if N is a 2 digit number. 11 | 12 | Print "3" if N is a 3 digit number. 13 | 14 | Print "More than 3 digits" if N has more than 3 digits. 15 | 16 | Constraints 17 | 0 \leq N \leq 10000000≤N≤1000000 18 | Sample Input: 19 | 9 20 | Sample Output: 21 | 1 22 | 23 | */ 24 | #include 25 | 26 | int main(void) { 27 | int n,count; 28 | scanf("%d",&n); 29 | while(n) 30 | { 31 | n=n/10; 32 | count++; 33 | } 34 | (count>3)?printf("More than 3 digits"):printf("%d",count); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Codechef/KNIGHT2.cpp: -------------------------------------------------------------------------------- 1 | // This is a solution to JOURNEY OF THE KNIGHT problem on CODECHEF 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // your code goes here 7 | int t, a, b, c, d; 8 | cin>>t; 9 | while(t--){ 10 | cin>>a>>b>>c>>d; 11 | if((abs(a-c)%2==0 && abs(b-d)%2==0)||(abs(a-c)%2!=0 && abs(b-d)%2!=0)){ 12 | cout<<"Yes"< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int N; 9 | int T; 10 | vectorS; 11 | int *A; 12 | cin>>N; 13 | A=new int[N]; 14 | for(int i=0;i>T; 16 | A[i]=T; 17 | } 18 | sort(A,A+N); 19 | /* To count the number of Integeres in the array*/ 20 | int Count=1; 21 | for(int i=0;i 3 | using namespace std; 4 | #define el "\n"; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | long t,a,b,s,r,m; 10 | cin>>t; 11 | while(t--){ 12 | cin>>a>>b; m=0; 13 | s=a+b; 14 | while(s>0){ 15 | r=s%10; 16 | if(r==0 || r==6 || r==9){ 17 | m+=6; 18 | } 19 | if(r==2 || r==3 || r==5){ 20 | m+=5; 21 | } 22 | if(r==1){ 23 | m+=2; 24 | } 25 | if(r==4){ 26 | m+=4; 27 | } 28 | if(r==7){ 29 | m+=3; 30 | } 31 | if(r==8){ 32 | m+=7; 33 | } 34 | s=s/10; 35 | } 36 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | // your code goes here 9 | int T, N; 10 | cin>>T; 11 | for(int i = 0; i < T; i++){ 12 | cin>>N; 13 | long long arr[N]; 14 | for(long long i = 0; i < N; i++) 15 | cin>>arr[i]; 16 | 17 | sort(arr, arr+N); 18 | cout<<2*abs(arr[N-1]-arr[0])< 2 | #define int long long 3 | using namespace std; 4 | 5 | int A[100001], B[100001], n, m; 6 | 7 | void solve() 8 | { 9 | cin >> n; 10 | for (int i = 1; i <= n; ++i) 11 | cin >> A[i]; 12 | cin >> m; 13 | for (int i = 1; i <= m; ++i) 14 | cin >> B[i]; 15 | int sa = 0; 16 | for (int i = 1, s = 0; i <= n; ++i) 17 | { 18 | s += A[i]; 19 | sa = max(sa, s); 20 | } 21 | for (int i = n, s = 0; i >= 0; --i) 22 | { 23 | s += A[i]; 24 | sa = max(sa, s); 25 | } 26 | for (int i = 1; i <= m; ++i) 27 | if (B[i] >= 0) 28 | sa += B[i]; 29 | cout << sa << endl; 30 | } 31 | signed main() 32 | { 33 | int T; 34 | cin >> T; 35 | while (T--) 36 | solve(); 37 | } 38 | -------------------------------------------------------------------------------- /Codechef/MEANMEDIAN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | while (t--) 9 | { 10 | int X, Y; 11 | cin >> X >> Y; 12 | int a, b, c; 13 | a = b = Y; 14 | c = 3 * X - 2 * Y; 15 | cout << min(a, c) << ' ' << b << ' ' << max(a, c) << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Codechef/ODDPAIRS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define edl cout << endl; 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | ll n, x; 9 | ll count = 0; 10 | cin >> x; 11 | for (int a = 0; a < x; a++) 12 | { 13 | cin >> n; 14 | if (n % 2) 15 | count = ((n - 1) / 2) * (n + 1); 16 | else 17 | count = 2 * n / 2 * n / 2; 18 | cout << count; 19 | edl 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codechef/PAIREQ.cpp: -------------------------------------------------------------------------------- 1 | //CodeChef Problem Code: PAIREQ 2 | #include 3 | using namespace std; 4 | #define el "\n"; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | int t,n,i,c,m; 10 | cin>>t; 11 | while(t--){ 12 | cin>>n; i=m=0; c=1; 13 | int *arr = new int[n]; 14 | while(i>arr[i++]; 16 | } 17 | sort(arr,arr+n); 18 | for(i=0;im){ 26 | m=c; 27 | } 28 | } 29 | cout< 3 | using namespace std; 4 | 5 | int main() { 6 | // your code goes here 7 | int t, n, k; 8 | cin>>t; 9 | while(t--){ 10 | cin>>n>>k; 11 | if(n%2!=0 && k==1){ 12 | cout<<"Yes"< 2 | #define edl cout<> t; 9 | while (t--) 10 | { 11 | int X; 12 | cin >> X; 13 | int a[X]; 14 | for (int i = 0; i < X; i++) 15 | cin >> a[i]; 16 | 17 | int Y; 18 | cin >> Y; 19 | int b[Y]; 20 | for (int i = 0; i < Y; i++) 21 | cin >> b[i]; 22 | sort(b, b + Y); 23 | for (int i = 0; i < X; i++) 24 | { 25 | if (!binary_search(b, b + Y, a[i])) 26 | cout << a[i] << ' '; 27 | } 28 | edl 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Codechef/Problems in your to-do list: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | for i in range(t): #--------1 4 | l=int(input()) 5 | n= list(map(int,input().split())) 6 | count=0 7 | for j in (n): 8 | if(j>=1000): 9 | count+=1 10 | print(count) 11 | 12 | -------------------------------------------------------------------------------- /Codechef/REMOVECARDS.cpp: -------------------------------------------------------------------------------- 1 | // This is a solution to the REMOVECARDS problem of CODECHEF 2 | #include 3 | using namespace std; 4 | int mostFrequent(int arr[], int n) 5 | { 6 | // int x =0; 7 | unordered_map hash; 8 | for (int i = 0; i < n; i++) 9 | hash[arr[i]]++; 10 | 11 | int max_count = 0, res = -1; 12 | for (auto i : hash) { 13 | if (max_count < i.second) { 14 | res = i.first; 15 | max_count = i.second; 16 | } 17 | } 18 | 19 | return max_count; 20 | } 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--){ 26 | int n; 27 | cin>>n; 28 | int a[n]; 29 | for(int i=0;i>a[i]; 32 | } 33 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t, n, a[100000]; 8 | cin >> t; 9 | while (t--) 10 | { 11 | map m; 12 | cin >> n; 13 | for (int i = 0; i < n; i++) 14 | { 15 | cin >> a[i]; 16 | m[a[i]]++; 17 | } 18 | auto it = m.begin(); 19 | int max = (*it).second; 20 | for (auto it1 : m) 21 | { 22 | if (it1.second > 1 && it1.second > max) 23 | { 24 | max = it1.second; 25 | } 26 | } 27 | cout << max - 1 << endl; 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Codechef/SUSSTR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int tc; 7 | cin >> tc; 8 | 9 | while (tc--) 10 | { 11 | int n; 12 | cin >> n; 13 | string s; 14 | cin >> s; 15 | 16 | string t = ""; 17 | 18 | int i = 0, j = n - 1; 19 | 20 | while (i <= j) 21 | { 22 | if (s[i] == '0') 23 | t = s[i] + t; 24 | else 25 | t = t + s[i]; 26 | 27 | if (i < j) 28 | { 29 | if (s[j] == '0') 30 | t = t + s[j]; 31 | else 32 | t = s[j] + t; 33 | j--; 34 | } 35 | 36 | i++; 37 | } 38 | 39 | cout << t << endl; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Codechef/TODOLIST.cpp: -------------------------------------------------------------------------------- 1 | //CodeChef Problem Code: TODOLIST 2 | #include 3 | using namespace std; 4 | #define el "\n"; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | int t,n,dl,r; 10 | cin>>t; 11 | while(t--){ 12 | cin>>n; r=0; 13 | while(n--){ 14 | cin>>dl; 15 | if(dl>999){ 16 | r++; 17 | } 18 | } 19 | cout< 3 | using namespace std; 4 | #define el "\n"; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | int t,i; string s,g,m=""; 10 | cin>>t; 11 | while(t--){ 12 | cin>>s>>g; m=""; 13 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t; 7 | cin >> t; 8 | 9 | while (t--) { 10 | int n, k; 11 | cin >> n >> k; 12 | 13 | string str; 14 | cin >> str; 15 | 16 | int change = 0; 17 | 18 | int l = 0, r = n - 1; 19 | while (l < r) { 20 | if (str[l] != str[r]) 21 | change++; 22 | l++; 23 | r--; 24 | } 25 | 26 | 27 | if (k >= change) { 28 | if ((k - change) % 2 == 0) 29 | cout << "YES" << '\n'; 30 | else if (n % 2 == 1) 31 | cout << "YES" << '\n'; 32 | else 33 | cout << "NO" << '\n'; 34 | } 35 | else 36 | cout << "NO" << '\n'; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codechef/addtwonumbers.py: -------------------------------------------------------------------------------- 1 | for t in range(int(input())): 2 | A, B = input().split() 3 | A, B = int(A), int(B) 4 | print(A+B) 5 | -------------------------------------------------------------------------------- /Codechef/aug4(5).c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | char gender,ms; 5 | int age; 6 | printf("Enter the gender M/m/F/f\n"); 7 | scanf("%c",&gender); 8 | if(gender=='M' || gender=='m' || gender=='F' || gender=='f') 9 | { 10 | printf("Enter ur marital status R/r/U/u\n"); 11 | fflush(stdin); 12 | scanf("%c",&ms); 13 | if(ms=='R' || ms=='r' || ms=='U' || ms=='u') 14 | { 15 | printf("Enter ur age\n"); 16 | scanf("%d",&age); 17 | if(age>=18 && age<=20) 18 | { 19 | printf("pension alotted 5000\n"); 20 | } 21 | else 22 | { 23 | printf("not eligible"); 24 | } 25 | } 26 | else 27 | { 28 | printf("invalid marital status\n"); 29 | } 30 | } 31 | else 32 | { 33 | printf("invalid gender"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Codechef/credcoins.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t=int(input()) 3 | for i in range(t): 4 | x,y=map(int,input().split()) 5 | z=x*y 6 | print(int(z/100)) 7 | -------------------------------------------------------------------------------- /Codechef/expertsetter.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | for i in range(t): 3 | x,y = map(int,input().split()) 4 | y=y*100 5 | a=y/x 6 | if(a>=50): 7 | print("yes") 8 | else: 9 | print("no") 10 | -------------------------------------------------------------------------------- /Codechef/howmanycodechefquestionc++.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin>>n; 7 | if(n==0){ 8 | cout<<"1"; 9 | } 10 | else if(n/10<1){ 11 | cout<<"1"; 12 | } 13 | else if(n/10<10){ 14 | cout<<"2"; 15 | } 16 | else if(n/10<100){ 17 | cout<<"3"; 18 | } 19 | else{ 20 | cout<<"More than 3 digits"; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Codechef/minimumpizza.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | for i in range(t): 4 | n,x = map(int,input().split()) 5 | a=n*x 6 | if(a%4==0): 7 | print(int(a/4)) 8 | else: 9 | print(int(a/4)+1) 10 | -------------------------------------------------------------------------------- /Codechef/saleseason.py: -------------------------------------------------------------------------------- 1 | #cook your dish here 2 | t = int(input()) 3 | for i in range(t): 4 | x=int(input()) 5 | if(x>100 and x<=1000): 6 | print(x-25) 7 | elif(x<=5000 and x>1000): 8 | print(x-100) 9 | elif(x>5000): 10 | print(x-500) 11 | else: 12 | print(x)sales 13 | -------------------------------------------------------------------------------- /Codechef/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Codechef/test.cpp -------------------------------------------------------------------------------- /Codechef/testaverage.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | for i in range(t): 4 | x =list(map(int,input().split())) 5 | if((x[0]+x[1])//2)<35 or ((x[0]+x[2])//2)<35 or ((x[2]+x[1])//2)<35: 6 | print("fail") 7 | else: 8 | print("pass") 9 | -------------------------------------------------------------------------------- /Codeforces/405A_Gravity_Flip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n; cin>>n; int a[n]; 5 | for(int i=0;i>a[i]; 7 | } 8 | sort(a,a+n); 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | void solve() 8 | { 9 | int n ; cin >> n ; 10 | string text ; 11 | cin >> text ; 12 | 13 | string ans = ""; 14 | 15 | for(int i = 0 ; i < n; i++){ 16 | if(i + 2 < n and text[i+2] == '0' and (i + 3 == n or (text[i+3] != '0'))){ 17 | int temp = (text[i] -'0') * 10 + text[i+1] - '0' - 1; 18 | ans += char(temp + 'a'); 19 | i += 2; 20 | } 21 | else{ 22 | int temp = text[i] - '0' - 1 + 'a'; 23 | ans += char(temp); 24 | } 25 | } 26 | 27 | cout << ans << endl; 28 | 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | ios_base::sync_with_stdio(false); 35 | 36 | int t; 37 | cin >> t; 38 | 39 | while (t--) 40 | { 41 | Solution obj; 42 | obj.solve(); 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Codeforces/825_B_Playing_with_GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(){ 5 | int n; 6 | cin>>n; 7 | vector a(n); 8 | vector v(n+1); 9 | for(int i=0 ; i>a[i]; 11 | } 12 | v[0]=a[0]; 13 | for(int i=1 ; i>t; 31 | while(t--){ 32 | solve(); 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Codeforces/827_C_Stripes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(){ 5 | vector v(8); 6 | for(int i=0 ; i<8 ; i++){ 7 | cin>>v[i]; 8 | } 9 | for(int i=0 ; i<8 ; i++){ 10 | if(v[i][0]!='R') continue; 11 | char st=v[i][0]; 12 | bool other=false; 13 | for(int j=1 ; j<8 ; j++){ 14 | if(v[i][j]==st) continue; 15 | other=true; 16 | break; 17 | } 18 | if(!other){ 19 | cout<>t; 42 | while(t--){ 43 | solve(); 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Codeforces/A._Football.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | int main() 6 | { 7 | string s; 8 | cin >> s; 9 | ll count=0; 10 | for (ll i = 0; i < s.length() - 1; i++) 11 | { 12 | if (s[i] == s[i + 1]) 13 | { 14 | count++; 15 | if (count >= 6) 16 | { 17 | cout << "YES" << endl; 18 | break; 19 | } 20 | 21 | } 22 | else 23 | { 24 | count=0; 25 | } 26 | 27 | } 28 | 29 | if (count<6) 30 | { 31 | cout<<"NO"< 2 | using namespace std; 3 | int main(){ 4 | int a[5][5]; 5 | for(int i=0;i<5;i++){ 6 | for(int j=0;j<5;j++){ 7 | cin>>a[i][j]; 8 | if(a[i][j]==1){ 9 | cout< 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | const ll N = 1 << 19; 15 | const ll C = 1e12 + 1; 16 | 17 | void fsolve() 18 | { 19 | ll a[N], n, t, mn, i, x; 20 | string s; 21 | 22 | cin >> s; 23 | n = s.size(); 24 | mn = C * N; 25 | for (i = 1; i <= n; i++) 26 | a[i] = a[i - 1] + s[i - 1] - '0'; 27 | for (i = 0; i <= n; i++) 28 | mn = min(mn, (a[i] + n - i - a[n] + a[i]) * C - ((s[i - 1] == '1' && s[i] == '0') | (s[i + 1] == '0' && s[i] == '1'))); 29 | cout << mn << endl; 30 | } 31 | 32 | int main() 33 | { 34 | ll t; 35 | t = 1; 36 | cin >> t; 37 | while (t--) 38 | { 39 | fsolve(); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Borze(Codeforces).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | string s; 5 | cin>>s; 6 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | ios_base::sync_with_stdio(false); cin.tie(0); 9 | 10 | set numbers; 11 | map left; 12 | map right; 13 | 14 | int n, v; 15 | cin >> n >> v; 16 | numbers.insert(v); 17 | for (int i = 0; i < n - 1; i++) { 18 | cin >> v; 19 | auto it = numbers.upper_bound(v); 20 | int result; 21 | if (it != numbers.end() && left.count(*it) == 0) { 22 | left[*it] = v; 23 | result = *it; 24 | } else { 25 | it--; 26 | right[*it] = v; 27 | result = *it; 28 | } 29 | numbers.insert(v); 30 | cout << result; 31 | if (i == n - 2) cout << 'n'; 32 | else cout << ' '; 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Codeforces/DivisorChain.cpp: -------------------------------------------------------------------------------- 1 | link to the problem- https://codeforces.com/problemset/problem/1864/C 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | int main(){ 8 | int T;cin>>T; 9 | while(T--){ 10 | ll x; 11 | cin >> x; 12 | vector arr; 13 | while (x != 1) { 14 | arr.push_back(x); 15 | ll p2 = 1; 16 | while (x % (2*p2) == 0) p2 *= 2; 17 | if (x == p2) p2 /= 2; 18 | x -= p2; 19 | } 20 | arr.push_back(1); 21 | cout << arr.size() << "\n"; 22 | for (int y: arr) cout << y << " "; 23 | cout << "\n"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Codeforces/GameOnPermutation.cpp: -------------------------------------------------------------------------------- 1 | //Link to the question- https://codeforces.com/problemset/problem/1860/C 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | int t;cin>>t; 8 | while(t--){ 9 | int noOfElement;cin>>noOfElement; 10 | int minTotal=noOfElement+1; 11 | int adjLeft=noOfElement+1; 12 | int count=0; 13 | for(int i=0;i>element; 16 | if(!(elementadjLeft)){ 17 | count++; 18 | adjLeft=element; 19 | } 20 | minTotal=min(minTotal,element); 21 | }cout< 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | string s; 18 | cin >> s; 19 | vll cnt(10); 20 | for (auto c : s) 21 | ++cnt[c - '0']; 22 | int mx = *max_element(cnt.begin(), cnt.end()); 23 | if (mx == 4) 24 | cout << -1; 25 | else if (mx == 3) 26 | cout << 6; 27 | else 28 | cout << 4; 29 | cout << '\n'; 30 | } 31 | 32 | int main() 33 | { 34 | ll t; 35 | t = 1; 36 | cin >> t; 37 | while (t--) 38 | { 39 | fsolve(); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Mex Master_Cf858B.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Codeforces Round 858 (Div. 2) 3 | Problem B - Mex Master 4 | Problem link - https://codeforces.com/contest/1806/problem/B 5 | */ 6 | 7 | #include 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | ll n; 18 | cin >> n; 19 | ll sum(0); 20 | ll f(1); 21 | for (ll i = 0; i < n; i++) 22 | { 23 | ll x; 24 | cin >> x; 25 | if (!x) 26 | sum++; 27 | else if (x >= 2) 28 | f ^= f; 29 | } 30 | if (sum <= (n + 1) / 2) 31 | cout << "0\n"; 32 | else if (!f || sum == n) 33 | cout << "1\n"; 34 | else 35 | cout << "2\n"; 36 | } 37 | 38 | int main() 39 | { 40 | ll t; 41 | t = 1; 42 | cin >> t; 43 | while (t--) 44 | { 45 | fsolve(); 46 | } 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Codeforces/Points on Plane_Edu145B.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Codeforces Round 145(Rated for div. 2) 3 | Problem B - Points on Plane 4 | Problem link - https://codeforces.com/contest/1809/problem/B 5 | */ 6 | 7 | #include 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | ll n; 18 | cin >> n; 19 | ll l = -1; 20 | ll r = 1e9; 21 | while (r - l > 1) 22 | { 23 | ll mid = (l + r) / 2; 24 | if (mid * mid >= n) 25 | r = mid; 26 | else 27 | l = mid; 28 | } 29 | cout << r - 1 << "\n"; 30 | } 31 | 32 | int main() 33 | { 34 | ll t; 35 | t = 1; 36 | cin >> t; 37 | while (t--) 38 | { 39 | fsolve(); 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Codeforces/Sequence Master_Cf858C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Codeforces Round 858 (Div. 2) 3 | Problem C - Sequence Master 4 | Problem link - https://codeforces.com/contest/1806/problem/C 5 | */ 6 | 7 | #include 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | ll n, d = 0, s = 0, r = 0; 18 | cin >> n; 19 | 20 | vll p(n << 1); 21 | 22 | for (auto &i : p) 23 | cin >> i, d += abs(i); 24 | 25 | sort(p.begin(), p.end()); 26 | 27 | if (n & 1) 28 | { 29 | cout << (n == 1 ? p[1] - p[0] : d) << endl; 30 | return; 31 | } 32 | if (n == 2) 33 | for (ll i : p) 34 | s += abs(i - 2); 35 | for (ll i = 0; i < (n << 1) - 1; i++) 36 | r += abs(p[i] + 1); 37 | cout << min({d, r + abs(n - p[(n << 1) - 1]), (n == 2 ? s : LLONG_MAX)}) << endl; 38 | } 39 | 40 | int main() 41 | { 42 | ll t; 43 | t = 1; 44 | cin >> t; 45 | while (t--) 46 | { 47 | fsolve(); 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Codeforces/Sum on Subarrays_Edu145C.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Codeforces Round 145(Rated for div. 2) 3 | Problem C - Sum on Subarrays 4 | Problem link - https://codeforces.com/contest/1809/problem/C 5 | */ 6 | 7 | #include 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | ll n, k; 18 | cin >> n >> k; 19 | for (int i = 1; i <= n; i++) 20 | { 21 | if (k >= i) 22 | { 23 | cout << "2 "; 24 | k -= i; 25 | } 26 | else if (k) 27 | { 28 | cout << -2 * (i - k) + 1 << " "; 29 | k = 0; 30 | } 31 | else 32 | cout << "-1000 "; 33 | } 34 | cout << "\n"; 35 | } 36 | 37 | int main() 38 | { 39 | ll t; 40 | t = 1; 41 | cin >> t; 42 | while (t--) 43 | { 44 | fsolve(); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Codeforces/Walking Master_Cf858A.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Codeforces Round 858 (Div. 2) 3 | Problem A - Walking Master 4 | Problem link - https://codeforces.com/contest/1806/problem/A 5 | */ 6 | 7 | #include 8 | #define amz fourrr 9 | #define ll long long 10 | #define vll vector 11 | using namespace std; 12 | const ll M = 1e9 + 7; 13 | const ll NUM = 1e18; 14 | 15 | void fsolve() 16 | { 17 | ll a, b, c, d; 18 | cin >> a >> b >> c >> d; 19 | if (b <= d && c <= a + d - b) 20 | { 21 | cout << (d - b) + (a + d - b - c) << "\n"; 22 | } 23 | else 24 | { 25 | cout << "-1\n"; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | ll t; 32 | t = 1; 33 | cin >> t; 34 | while (t--) 35 | { 36 | fsolve(); 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Codeforces/max_subarray_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // https://codeforces.com/gym/102961/problem/H 4 | // Defining 5 | 6 | #define ll long long 7 | #define l long 8 | #define svtype(name, size, type) vector name(size) 9 | #define fasterio \ 10 | ios_base::sync_with_stdio(false); \ 11 | cin.tie(NULL); 12 | 13 | int main() 14 | { 15 | fasterio int n; 16 | cin >> n; 17 | svtype(a, n, ll); 18 | ll sum = 0; 19 | for (int i = 0; i < n; i++) 20 | { 21 | cin >> a[i]; 22 | } 23 | ll tmp = -1000000001; 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | sum += a[i]; 28 | if (tmp < sum) 29 | { 30 | tmp = sum; 31 | } 32 | if (sum < 0) 33 | { 34 | sum = 0; 35 | } 36 | } 37 | ll ans = tmp; 38 | cout << ans << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Hackerearth/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Hackerearth/test.cpp -------------------------------------------------------------------------------- /Java Programs/3sumcloset.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int threeSumClosest(int[] nums, int target) { 3 | Arrays.sort(nums); 4 | int result=nums[0]+nums[1]+nums[nums.length-1]; 5 | for(int i=0;i A.length) i++; 6 | else if(A[A[i]-1] != A[i]) swap(A, i, A[i]-1); 7 | else i++; 8 | } 9 | i = 0; 10 | while(i < A.length && A[i] == i+1) i++; 11 | return i+1; 12 | } 13 | 14 | private void swap(int[] A, int i, int j){ 15 | int temp = A[i]; 16 | A[i] = A[j]; 17 | A[j] = temp; 18 | } 19 | } -------------------------------------------------------------------------------- /Java Programs/GOLDEN RATIO (VA) NEW 22.java: -------------------------------------------------------------------------------- 1 | // GOLDEN RATIO 2 | class Main { 3 | public static void main(String[] args) { 4 | 5 | int n = 10, firstTerm = 0, secondTerm = 1; 6 | System.out.println("Fibonacci Series till " + n + " terms:"); 7 | 8 | for (int i = 1; i <= n; ++i) { 9 | System.out.print(firstTerm + ", "); 10 | 11 | // Lets compute the next term 12 | int nextTerm = firstTerm + secondTerm; 13 | firstTerm = secondTerm; 14 | secondTerm = nextTerm; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Java Programs/Inorder_Traversing.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Java Programs/Inorder_Traversing.class -------------------------------------------------------------------------------- /Java Programs/Length.java: -------------------------------------------------------------------------------- 1 | package MAin; 2 | 3 | public class Length { 4 | public static void main(String args[]) 5 | { 6 | //Strings 7 | //replace 8 | String name="Aman"; 9 | String name2=name.replace('a','b'); 10 | System.out.println(name2); 11 | System.out.println(name); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Java Programs/MaxPointsOnALine.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public int maxPoints(int[][] points) { 4 | if(points.length < 3) 5 | return points.length; 6 | 7 | int max = 0; 8 | for(int i = 0; i < points.length; i++) 9 | { 10 | for(int j = i+1; j < points.length; j++) 11 | { 12 | int temp = 0; 13 | int[] vec = new int[]{points[i][0]-points[j][0], points[i][1]-points[j][1]}; 14 | int[] normal = new int[]{-1*vec[1], vec[0]}; 15 | 16 | for(int k = 0; k < points.length; k++) 17 | if(dotProduct(normal, new int[]{points[k][0]-points[i][0], points[k][1]-points[i][1]}) == 0) 18 | max = Math.max(max, ++temp); 19 | } 20 | } 21 | return max; 22 | } 23 | 24 | public int dotProduct(int[] u, int[] v) 25 | { 26 | int dp = 0; 27 | for(int i = 0; i < 2; i++) 28 | dp += u[i]*v[i]; 29 | return dp; 30 | } 31 | } -------------------------------------------------------------------------------- /Java Programs/Merge_KSortedLists.java: -------------------------------------------------------------------------------- 1 | public class Main{ 2 | public ListNode mergeKLists(ListNode[] lists) { 3 | PriorityQueue pq=new PriorityQueue<>(new Comparator(){ 4 | public int compare(ListNode o1, ListNode o2) 5 | { 6 | return o1.val-o2.val; //for min heap 7 | } 8 | }); 9 | for(int i=0;i dq=new LinkedList<>(); 11 | int j=0; 12 | for(int i=0;ileftbar) 16 | { 17 | leftbar=height[left]; 18 | } 19 | else { 20 | //it will store water 21 | ans+=leftbar-height[left]; 22 | left++; 23 | } 24 | } 25 | else { 26 | if(height[right]>rightbar) 27 | { 28 | rightbar=height[right]; 29 | 30 | } 31 | else { 32 | ans+=rightbar-height[right]; 33 | right--; 34 | } 35 | } 36 | } 37 | return ans; 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Java Programs/area_of_circle.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class AreaOfCircle 3 | { 4 | public static void main(String args[]) 5 | { 6 | 7 | Scanner s= new Scanner(System.in); 8 | 9 | System.out.println("Enter the radius:"); 10 | double r= s.nextDouble(); 11 | double area=(22*r*r)/7 ; 12 | System.out.println("Area of Circle is: " + area); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Java Programs/binsearch21.java: -------------------------------------------------------------------------------- 1 | 2 | // recursive Binary Search Java 3 | class BinarySearch { 4 | int binarySearch(int arr[], int l, int r, int x) 5 | { 6 | if (r >= l) { 7 | int mid = l + (r - l) / 2; 8 | if (arr[mid] == x) 9 | return mid; 10 | if (arr[mid] > x) 11 | return binarySearch(arr, l, mid - 1, x); 12 | return binarySearch(arr, mid + 1, r, x); 13 | } 14 | 15 | // when element is not present in array 16 | return -1; 17 | } 18 | 19 | // Driver Code 20 | public static void main(String args[]) 21 | { 22 | BinarySearch ob = new BinarySearch(); 23 | int arr[] = { 2, 3, 4, 10, 40 }; 24 | int n = arr.length; 25 | int x = 10; 26 | int result = ob.binarySearch(arr, 0, n - 1, x); 27 | if (result == -1) 28 | System.out.println("Element not present"); 29 | else 30 | System.out.println("Element found at index " 31 | + result); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Java Programs/factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class factorial 3 | { 4 | public static void main(String[] args) 5 | { 6 | int n, mul; 7 | Scanner s = new Scanner(System.in); 8 | System.out.print("Enter any integer:"); 9 | n = s.nextInt(); 10 | Factorial obj = new Factorial(); 11 | mul = obj.fact(n); 12 | System.out.println("Factorial of "+n+" :"+mul); 13 | } 14 | int fact(int x) 15 | { 16 | if(x > 1) 17 | { 18 | return(x * fact(x - 1)); 19 | } 20 | return 1; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Java Programs/java.java: -------------------------------------------------------------------------------- 1 | // reverse the given string 2 | public class reverse_string { 3 | public static void main(String args[]){ 4 | String s="MADAM"; 5 | String temp=s; 6 | int n=s.length(); 7 | String ans=""; 8 | for(int i=n-1;i>=0;i--){ 9 | ans+=s.charAt(i); 10 | } 11 | System.out.println(ans); 12 | if(ans.equals(ans)){ 13 | System.out.println("given string is plindrome"); 14 | } 15 | else{ 16 | System.out.println("given string is not plindrome"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Java Programs/main2.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | 5 | int num = 29; 6 | boolean flag = false; 7 | for (int i = 2; i <= num / 2; ++i) { 8 | // condition for nonprime number 9 | if (num % i == 0) { 10 | flag = true; 11 | break; 12 | } 13 | } 14 | 15 | if (!flag) 16 | System.out.println(num + " is a prime number."); 17 | else 18 | System.out.println(num + " is not a prime number."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Java Programs/volume_of_cylinder.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class VolumeOfCylinder 3 | { 4 | public static void main(String args[]) 5 | { 6 | 7 | Scanner s= new Scanner(System.in); 8 | 9 | System.out.println("Enter the radius:"); 10 | double r=s.nextDouble(); 11 | System.out.println("Enter the height:"); 12 | double h=s.nextDouble(); 13 | 14 | double volume=((22*r*r*h)/7); 15 | 16 | System.out.println("volume of Cylinder is: " +volume); 17 | 18 | 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /JavaScript/Javascript five courses for free.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/JavaScript/Javascript five courses for free.docx -------------------------------------------------------------------------------- /JavaScript/The Best Podcasts for JavaScript.doc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/JavaScript/The Best Podcasts for JavaScript.doc -------------------------------------------------------------------------------- /JavaScript/hello.js: -------------------------------------------------------------------------------- 1 | const hello = "Hello World!"; 2 | 3 | console.log(hello); 4 | 5 | 6 | Const visitorHere = "Dkeys4 was here"; 7 | 8 | Console.log(visitorHere); 9 | -------------------------------------------------------------------------------- /Javascript five courses for free.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Javascript five courses for free.docx -------------------------------------------------------------------------------- /Leetcode/1029_Two_City_Scheduling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int twoCitySchedCost(vector>& costs) { 4 | vector>v; 5 | for(int i=0;i pathInZigZagTree(int label) { 4 | vectorans; 5 | ans.push_back(label); 6 | int t,i=0,ans1=0; 7 | while(label>1){ 8 | t=label/2; 9 | while(pow(2,i)<=t)i++; 10 | ans1=pow(2,i)-t+pow(2,i-1)-1; 11 | ans.push_back(ans1); 12 | label=ans1; 13 | i=0; 14 | } 15 | reverse(ans.begin(),ans.end()); 16 | return ans; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/136_single_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNumber(vector& nums) { 4 | int set_sum=0; 5 | int vector_sum=0; 6 | set s1; 7 | for(int i=0;i& nums) { 4 | int count = 0; 5 | int n=nums.size(); 6 | for(int i=1 ; i nums[i]) 9 | { 10 | count++; 11 | } 12 | } 13 | if(nums[n-1] > nums[0]) 14 | { 15 | count++; 16 | } 17 | return count<=1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/189_Rotate_array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector& nums, int k) { 4 | vector nums2(nums); 5 | for(int i=0; i& nums) { 6 | int n = nums.size(); 7 | int ans = n; 8 | 9 | set st(nums.begin(), nums.end()); 10 | vector unique; 11 | for (int num : st) { 12 | unique.push_back(num); 13 | } 14 | int visited = 0; 15 | for (int mi = 0; mi < unique.size(); mi++) { 16 | while (visited < unique.size() && unique[visited] < unique[mi] + n) { 17 | visited++; 18 | } 19 | 20 | int subAns = visited - mi; 21 | ans = min(ans, n - subAns); 22 | } 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leetcode/209. Minimum_Size_Subarray_Sum.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link: https://leetcode.com/problems/minimum-size-subarray-sum/ 2 | 3 | class Solution { 4 | public: 5 | int minSubArrayLen(int target, vector& nums) { 6 | int n= nums.size(); 7 | vectorpre(n,0); 8 | pre[0]=nums[0]; 9 | for(int i=1; i=target){ 19 | ans= min(ans, right-left+1); 20 | currsum-= nums[left]; 21 | left++; 22 | } 23 | left=0; 24 | right++; 25 | } 26 | return ans; 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode/42Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n = height.size(); 5 | int lmax = height[0]; 6 | int rmax = height[n-1]; 7 | int lpos = 1; 8 | int rpos = n-2; 9 | int water = 0; 10 | while(lpos <= rpos) 11 | { 12 | if(height[lpos] >= lmax) 13 | { 14 | lmax = height[lpos]; 15 | lpos++; 16 | } 17 | else if(height[rpos] >= rmax) 18 | { 19 | rmax = height[rpos]; 20 | rpos--; 21 | } 22 | else if(lmax <= rmax && height[lpos] < lmax) 23 | { 24 | water += lmax - height[lpos]; 25 | lpos++; 26 | } 27 | else 28 | { 29 | water += rmax - height[rpos]; 30 | rpos--; 31 | } 32 | 33 | } 34 | return water; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Leetcode/42_Trapping_Rainwater_Problem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n = height.size(); 5 | int l = 0, r = n-1; 6 | int lMax = height[0], rMax = height[n-1]; 7 | 8 | int ans = 0; 9 | 10 | while(l <= r){ 11 | if(height[l] < height[r]){ 12 | if(height[l] >= lMax) 13 | lMax = height[l]; 14 | else 15 | ans += lMax - height[l]; 16 | 17 | l++; 18 | }else{ 19 | if(height[r] >= rMax) 20 | rMax = height[r]; 21 | else 22 | ans += rMax - height[r]; 23 | 24 | r--; 25 | } 26 | } 27 | 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/515._Find_Largest_Value_in_Each_Tree_Row.cpp: -------------------------------------------------------------------------------- 1 | // Problem Link : https://leetcode.com/problems/find-largest-value-in-each-tree-row/ 2 | 3 | 4 | /*SOLUTION*/ 5 | 6 | class Solution { 7 | public: 8 | vector largestValues(TreeNode* root) { 9 | 10 | if(!root) return {}; 11 | 12 | vectorans; 13 | queueq; 14 | q.push(root); 15 | while(!q.empty()){ 16 | int n=q.size(); 17 | 18 | int maxi=INT_MIN; 19 | for(int i=0;ival); 23 | 24 | if(node->left) q.push(node->left); 25 | if(node->right) q.push(node->right); 26 | } 27 | ans.push_back(maxi); 28 | } 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Leetcode/66_plus_one.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) 4 | { 5 | int carry = 1; 6 | int add; 7 | for(int i = digits.size()-1 ; i>=0 ; i--) 8 | { 9 | add = digits[i] + carry; 10 | digits[i] = add%10; 11 | carry = add/10; 12 | } 13 | if(carry == 1) 14 | { 15 | digits.insert(digits.begin(),carry); 16 | } 17 | return digits; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/7_rotate_image.cpp: -------------------------------------------------------------------------------- 1 | //Problem Question : https://leetcode.com/problems/rotate-image/ 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | // Solution : 7 | class Solution { 8 | public: 9 | void rotate(vector>& matrix) { 10 | int n = matrix.size(); 11 | //First Transpose matrix means {i,j} <-> {j,i} 12 | for(int i = 0; i < n ; i++){ 13 | for(int j = 0 ; j < i ; j++){ 14 | swap(matrix[i][j],matrix[j][i]); 15 | } 16 | } 17 | //Reverse every row 18 | for(int i = 0 ; i < n ; i++){ 19 | reverse(matrix[i].begin(),matrix[i].end()); 20 | } 21 | } 22 | }; 23 | int main(){ 24 | ios::sync_with_stdio(0); 25 | cin.tie(0); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Leetcode/8_Merge_intervals.cpp: -------------------------------------------------------------------------------- 1 | //Problem Question : https://leetcode.com/problems/merge-intervals/ 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | #define endl '\n' 6 | 7 | //solution : 8 | 9 | class Solution { 10 | public: 11 | vector> merge(vector>& intervals) { 12 | vector> v; 13 | if( intervals.size() ==0){ 14 | return v; 15 | } 16 | sort(intervals.begin(),intervals.end()); 17 | vector temp = intervals[0]; 18 | for(auto it : intervals){ 19 | if(temp[1] >= it[0]){ 20 | temp[1] = max(temp[1], it[1]); 21 | } 22 | else{ 23 | v.push_back(temp); 24 | temp = it; 25 | } 26 | } 27 | v.push_back(temp); 28 | return v; 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | ios::sync_with_stdio(0); 35 | cin.tie(0); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Leetcode/All_A's_Before_B's.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | //Solution to the question 2124. Check if All A's Appears Before All B's on leetcode. 4 | 5 | class Solution { 6 | public: 7 | bool checkString(string s) { 8 | 9 | if(s.find('a') == std::string::npos || s.find('b') == std::string::npos) return true; 10 | 11 | string str = s; 12 | 13 | sort(str.begin(),str.end()); 14 | 15 | return str == s; 16 | } 17 | }; -------------------------------------------------------------------------------- /Leetcode/Build_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dp[51][101][51], pre[51][101][51], mod = 1e9 + 7; 4 | 5 | int numOfArrays(int n, int m, int k) { 6 | for (int j = 0; j<= m; j++) { 7 | dp[1][j][1] = 1; 8 | pre[1][j][1] = j; 9 | } 10 | 11 | for (int len = 2; len <= n; len++) { 12 | for (int mx = 1; mx <= m; mx++) { 13 | for (int cost = 1; cost <=k; cost++) { 14 | 15 | dp[len][mx][cost] = (1LL * mx * dp[len-1][mx][cost]) % mod; 16 | 17 | /* In this second case, we can append the element "mx" to the end of the array. 18 | for (int i = 1; i < mx; i++) dp[len][mx][cost] += ways[len - 1][i][cost - 1];*/ 19 | dp[len][mx][cost] = (dp[len][mx][cost] + pre[len-1][mx-1][cost-1]) % mod;\ 20 | 21 | pre[len][mx][cost] = (pre[len][mx-1][cost] + dp[len][mx][cost]) % mod; 22 | } 23 | } 24 | } 25 | return pre[n][m][k]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Count-Number-of-Distinct-Integers-After-Reverse-Operations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countDistinctIntegers(vector& nums) { 4 | int s = nums.size(); 5 | for(int i=0; im; 19 | for(int i=0; i1) 28 | { 29 | count--; 30 | } 31 | }*/ 32 | return count; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode/Find-duplicate-number.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] 4 | inclusive. 5 | There is only one repeated number in nums, return this repeated number. 6 | You must solve the problem without modifying the array nums and uses only constant extra space. 7 | */ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class Solution { 14 | public: 15 | int findDuplicate(vector& nums) { 16 | int val; 17 | sort(nums.begin(),nums.end()); 18 | for(int i=1;i> findDuplicate(String[] paths) { 4 | 5 | Map> map = new HashMap<>(); 6 | for(String path : paths){ 7 | String s[] = path.split(" "); 8 | for(int i=1; i l = map.getOrDefault(key, new ArrayList<>()); 12 | String p = s[0] +"/"+ roots[0]; 13 | l.add(p); 14 | map.put(key,l); 15 | 16 | } 17 | } 18 | 19 | List> result = new ArrayList<>(); 20 | for(List l : map.values()){ 21 | if(l.size()>1){ 22 | result.add(l); 23 | } 24 | } 25 | return result; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Leetcode/Intersection_of_Arrays-II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector& nums1, vector& nums2) { 4 | sort(nums1.begin(),nums1.end()); 5 | sort(nums2.begin(),nums2.end()); 6 | 7 | vector ans; 8 | 9 | for(int i = 0,j = 0 ; i < nums1.size() && j < nums2.size();) 10 | { 11 | if(nums1[i] == nums2[j]) 12 | { 13 | ans.push_back(nums1[i]); 14 | i++,j++; 15 | } 16 | else if(nums1[i] > nums2[j]) 17 | { 18 | j++; 19 | } 20 | else 21 | { 22 | i++; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode/Intersection_of_Arrays.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | sort(nums1.begin(),nums1.end()); 5 | sort(nums2.begin(),nums2.end()); 6 | 7 | vector ans; 8 | unordered_set s; 9 | 10 | for(int i = 0,j = 0 ; i < nums1.size() && j < nums2.size();) 11 | { 12 | if(nums1[i] == nums2[j]) 13 | { 14 | s.insert(nums1[i]); 15 | i++,j++; 16 | } 17 | else if(nums1[i] > nums2[j]) 18 | { 19 | j++; 20 | } 21 | else 22 | { 23 | i++; 24 | } 25 | } 26 | for(auto it:s) 27 | { 28 | ans.push_back(it); 29 | } 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode/LinkedListCycle.cpp: -------------------------------------------------------------------------------- 1 | //Given head, the head of a linked list, determine if the linked list has a cycle in it. 2 | /** 3 | * Definition for singly-linked list. 4 | * struct ListNode { 5 | * int val; 6 | * ListNode *next; 7 | * ListNode(int x) : val(x), next(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool hasCycle(ListNode *head) { 13 | if(!head) return false; 14 | ListNode* slow = head; 15 | ListNode* fast = head; 16 | while(fast->next && fast->next->next) { 17 | fast = fast->next->next; 18 | slow = slow->next; 19 | if(slow == fast) return true; 20 | } 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/PalindromeNumber.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | x=str(x) 4 | # if x == x[::-1]: 5 | # return True 6 | # else: 7 | # return False 8 | if x == x[::-1]: 9 | return True 10 | else: 11 | return False 12 | -------------------------------------------------------------------------------- /Leetcode/PowerOfThree.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfThree(self, n: int) -> bool: 3 | for i in range(0,31): 4 | ans = 3**i 5 | if(ans == n): 6 | return True 7 | else: 8 | return False 9 | -------------------------------------------------------------------------------- /Leetcode/PowerOfTwo.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | for i in range(0,31): 4 | ans = 2**i 5 | if(ans == n): 6 | return True 7 | else: 8 | return False 9 | -------------------------------------------------------------------------------- /Leetcode/Q58_Length_of_last_word.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | /* We count the number of characters in each word and reset it to 0 as we hit a 5 | space. Also another variable is taken to store the latest value of length in 6 | case the string is ending in sapces. */ 7 | 8 | int lengthOfLastWord(string s) { 9 | int n=s.length(); 10 | int length=0; 11 | int last=0; 12 | for(int i=0 ; i0) 15 | last=length; 16 | length=0; 17 | } 18 | else length++; 19 | } 20 | if(length>0) last=length; 21 | return last; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode/Remove _Duplicates_from_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | // Problem : 26 Remove Duplicates from Sorted Array 2 | // Problem Link : https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 3 | 4 | class Solution { 5 | public: 6 | int removeDuplicates(vector& nums) 7 | { 8 | int n = nums.size(); 9 | int prev = nums[0]; 10 | int index = 1; 11 | for (int i = 1; i < n; i++) 12 | { 13 | if (nums[i] != prev) 14 | { 15 | nums[index] = nums[i]; 16 | prev = nums[i]; 17 | index++; 18 | } 19 | } 20 | return index; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Remove_Duplicates_from_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | // Problem : 26 Remove Duplicates from Sorted Array 2 | // Problem Link : https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 3 | 4 | class Solution { 5 | public: 6 | int removeDuplicates(vector& nums) 7 | { 8 | int n = nums.size(); 9 | int prev = nums[0]; 10 | int index = 1; 11 | for (int i = 1; i < n; i++) 12 | { 13 | if (nums[i] != prev) 14 | { 15 | nums[index] = nums[i]; 16 | prev = nums[i]; 17 | index++; 18 | } 19 | } 20 | return index; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Reverse_integer.c: -------------------------------------------------------------------------------- 1 | int reverse(int x){ 2 | if(x>=1534236469|| x==-2147483648){ 3 | return 0; 4 | } 5 | long long int dividend=0; 6 | int remainder; 7 | while(x!=0){ 8 | 9 | remainder=x%10; 10 | // dividend=10*dividend+remainder; 11 | x/=10; 12 | if (dividend > INT_MAX/10 || (dividend == INT_MAX / 10 && remainder > 7)) return 0; 13 | if (dividend < INT_MIN/10 || (dividend == INT_MIN / 10 && remainder < -8)) return 0; 14 | // x/=10; 15 | dividend=10*dividend+remainder; 16 | } 17 | return dividend; 18 | } 19 | -------------------------------------------------------------------------------- /Leetcode/Rotate_Array.cpp: -------------------------------------------------------------------------------- 1 | // Lets take an Example:- 2 | // nums = [-1,-100,3,99], k = 2 3 | // Algo 4 | // Step 1- Reverse first size-k element 5 | // nums=[-100,-1,3,99] 6 | // Step 2- Reverse last k element 7 | // nums=[-100,-1,99,3] 8 | // Step 3- At last, reverse whole array 9 | // nums[3,99,-1,-100] 10 | 11 | class Solution { 12 | public: 13 | void rotate(vector& nums, int k) 14 | { 15 | int len=nums.size(); 16 | k%=len; 17 | if(k==0) 18 | return; 19 | // reversing first size-k element 20 | reverse(nums.begin(),nums.end()-k); 21 | 22 | // reversing last k element 23 | reverse(nums.end()-k,nums.end()); 24 | 25 | //reversing the whole array 26 | reverse(nums.begin(),nums.end()); 27 | } 28 | }; -------------------------------------------------------------------------------- /Leetcode/Subsets.py: -------------------------------------------------------------------------------- 1 | 2 | class Solution: 3 | def subsets(self, nums: List[int]) -> List[List[int]]: 4 | ans=[] #result array , stores subset array 5 | array=[] #subset array 6 | make(nums,array,0,ans) #function call for recursion or backtracking 7 | return ans #final answer after updation of result array 8 | 9 | def make(nums,array,i,ans): 10 | if i==len(nums): #if the index reaches to end of nums, 11 | ans.append(array) #time to store the subset formed 12 | return #return back to the called function 13 | make(nums,array,i+1,ans) #when the element need not to include in subset and i+1 is for next element 14 | #will backtrack 15 | make(nums,array+[nums[i]],i+1,ans) #when the element is included in subset and i+1 is for next element 16 | 17 | -------------------------------------------------------------------------------- /Leetcode/addstring.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addStrings(self, num1: str, num2: str) -> str: 3 | num_1=int(num1) 4 | num_2=int(num2) 5 | sum=num_1+num_2 6 | return str(sum) 7 | -------------------------------------------------------------------------------- /Leetcode/palindrome_number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | x=str(x) 4 | # if x == x[::-1]: 5 | # return True 6 | # else: 7 | # return False 8 | if x == x[::-1]: 9 | return True 10 | else: 11 | return False 12 | 13 | -------------------------------------------------------------------------------- /Leetcode/poweroffour.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, n: int) -> bool: 3 | for i in range(0,31): 4 | ans = 4**i 5 | if(ans == n): 6 | return True 7 | else: 8 | return False 9 | -------------------------------------------------------------------------------- /Leetcode/sum-of-number-and-its-reverse.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool sumOfNumberAndReverse(int num) { 4 | int n; 5 | for(int i=0; i<=num; i++) 6 | { 7 | n = i; 8 | int rev = 0; 9 | while(n!=0) 10 | { 11 | int digit = n%10; 12 | rev = (rev*10)+digit; 13 | n /= 10; 14 | } 15 | if(i+rev==num) 16 | { 17 | return true; 18 | } 19 | } 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/sumoftwonumber.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sum(self, num1: int, num2: int) -> int: 3 | sum=num1+num2 4 | return sum 5 | -------------------------------------------------------------------------------- /Leetcode/test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Leetcode/test.cpp -------------------------------------------------------------------------------- /Leetcode/two sum: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | # for i in range(len(nums)): 4 | # for j in range(i+1,len(nums)): 5 | # if((nums[i]+nums[j])==target): 6 | # return [i,j] 7 | # else: 8 | # pass 9 | length = len(nums) 10 | for i in range(length): 11 | for j in range(i + 1, length): 12 | if nums[i] + nums[j] == target: 13 | return [i, j] 14 | -------------------------------------------------------------------------------- /Python programs/3D Graphs.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.colors as col 3 | from mpl_toolkits.mplot3d import Axes3D 4 | import matplotlib.pyplot as plt 5 | #Data for a three dimensional line 6 | z = np.linspace(0, 15, 1000) 7 | x = np.sin(z) 8 | y = np.cos(z) 9 | ax.plot3D(x, y, z, 'grey') 10 | #Data for three dimensional scattered points 11 | z = 15 * np.random.random(100) 12 | x = np.sin(z) + 0.1 * np.random.randn(100) 13 | y = np.cos(z) + 0.1 * np.random.randn(100) 14 | ax.scatter3D(x, y, z, c=z, cmap='Greens') 15 | plt.show() 16 | -------------------------------------------------------------------------------- /Python programs/3sum.py: -------------------------------------------------------------------------------- 1 | def threeSum(self, nums: List[int]) -> List[List[int]]: 2 | def find_pair(n,start,target,result): 3 | end=len(n)-1 4 | while start0): 7 | if(BMI<=16): 8 | print("you are severely underweight") 9 | elif(BMI<=18.5): 10 | print("you are underweight") 11 | elif(BMI<=25): 12 | print("you are Healthy") 13 | elif(BMI<=30): 14 | print("you are overweight") 15 | else: print("you are severely overweight") 16 | else:("enter valid details") 17 | -------------------------------------------------------------------------------- /Python programs/Buy and sell stocks IV.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, k: int, prices: List[int]) -> int: 3 | def func(i,buy,prices,ct,dic): 4 | if i>=len(prices) or ct==0: 5 | return 0 6 | if (i,buy,ct) in dic: 7 | return dic[(i,buy,ct)] 8 | x,y,a,b=0,0,0,0 9 | if buy: 10 | x=-prices[i]+func(i+1,False,prices,ct,dic) 11 | y=0+func(i+1,buy,prices,ct,dic) 12 | else: 13 | a=prices[i]+func(i+1,True,prices,ct-1,dic) 14 | b=0+func(i+1,buy,prices,ct,dic) 15 | dic[(i,buy,ct)]=max(a,b,x,y) 16 | return max(a,b,x,y) 17 | return func(0,True,prices,k,{}) 18 | 19 | -------------------------------------------------------------------------------- /Python programs/C.P_Solution.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | t = int(input()) 3 | for i in range(t): 4 | X,Y,Z=map(int,input().split()) 5 | if(X>Y and X>Z): 6 | print("Setter") 7 | elif(Z>X and Z>Y): 8 | print("Editorialist") 9 | else: 10 | print("Tester") 11 | -------------------------------------------------------------------------------- /Python programs/Count Good Nodes in Binary Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def goodNodes(self, root: TreeNode) -> int: 9 | def dfs(self,node,maxval): 10 | if not node: 11 | return 0 12 | res=1 if node.val>=maxval else 0 13 | maxval=max(node.val,maxval) 14 | res+=dfs(self,node.left,maxval) 15 | res+=dfs(self,node.right,maxval) 16 | return res 17 | return dfs(self,root,root.val) 18 | -------------------------------------------------------------------------------- /Python programs/Count Subarrays with fixed bound.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int: 3 | n=len(nums) 4 | ws,we=0,0 5 | rmin,rmax=-1,-1 6 | count=0 7 | while we int: 3 | m=len(word1) 4 | n=len(word2) 5 | dp=[[-1 for i in range(n)] for j in range(m)] 6 | 7 | def lcs(m,n): 8 | if m<0 or n<0: 9 | return 0 10 | if dp[m][n]!=-1: 11 | return dp[m][n] 12 | if word1[m]==word2[n]: 13 | dp[m][n]=1+lcs(m-1,n-1) 14 | return dp[m][n] 15 | dp[m][n]=max(lcs(m-1,n),lcs(m,n-1)) 16 | return dp[m][n] 17 | return m+n-lcs(m-1,n-1)*2 18 | -------------------------------------------------------------------------------- /Python programs/Dictionary_in_python.py: -------------------------------------------------------------------------------- 1 | Creating an empty Dictionary 2 | Dict = {} 3 | print("Empty Dictionary: ") 4 | print(Dict) 5 | 6 | Adding elements one at a time 7 | Dict[0] = 'hackoctberfest' 8 | Dict[2] = 'For' 9 | Dict[3] = 1 10 | print("\nDictionary after adding 3 elements: ") 11 | print(Dict) 12 | 13 | Adding set of values 14 | to a single Key 15 | Dict['Value_set'] = 2, 3, 4 16 | print("\nDictionary after adding 3 elements: ") 17 | print(Dict) 18 | 19 | Updating existing Key's Value 20 | Dict[2] = 'hackoctberfest' 21 | print("\nUpdated key value: ") 22 | print(Dict) 23 | 24 | Adding Nested Key value to Dictionary 25 | Dict[5] = {'Nested': {'1': 'Life', '2': 'shivani'}} 26 | print("\nAdding a Nested Key: ") 27 | print(Dict) 28 | -------------------------------------------------------------------------------- /Python programs/Dynamic_Programming_making_two_strings_equal..py: -------------------------------------------------------------------------------- 1 | def minDistance(self, word1: str, word2: str) -> int: 2 | m=len(word1) 3 | n=len(word2) 4 | dp=[[-1 for i in range(n+1)]for j in range(m+1)] 5 | return (len(word1)+len(word2))-2*(lcs(dp,word1,word2,len(word1)+1,len(word2)+1)) 6 | def lcs(dp,s1,s2,m,n): 7 | for i in range(m): 8 | for j in range(n): 9 | if i==0 or j==0: 10 | dp[i][j]=0 11 | elif s1[i-1]==s2[j-1]: 12 | dp[i][j]=1+dp[i-1][j-1] 13 | else: 14 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]) 15 | return max(max(dp)) 16 | -------------------------------------------------------------------------------- /Python programs/Guess The number.py: -------------------------------------------------------------------------------- 1 | import random 2 | number = random.randint(1,10) 3 | for i in range(0,3): 4 | user = int(input("guess the number")) 5 | if user == number: 6 | print("Hurray!!") 7 | print(f"you guessed the number right it's {number}") 8 | break 9 | if user != number: 10 | print(f"Your guess is incorrect the number is {number}") 11 | -------------------------------------------------------------------------------- /Python programs/Image Filtering.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from matplotlib import pyplot as plt 4 | from PIL import Image, ImageFilter 5 | %matplotlib inline 6 | image = cv2.imread('cm.jpg') # reads the image 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert to HSV 8 | figure_size = 9 # the dimension of the x and y axis of the kernal. 9 | new_image = cv2.blur(image,(figure_size, figure_size)) 10 | plt.figure(figsize=(11,6)) 11 | plt.subplot(121), plt.imshow(cv2.cvtColor(image, cv2.COLOR_HSV2RGB)),plt.title('Original') 12 | plt.xticks([]), plt.yticks([]) 13 | plt.subplot(122), plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_HSV2RGB)),plt.title('Mean filter') 14 | plt.xticks([]), plt.yticks([]) 15 | plt.show() 16 | -------------------------------------------------------------------------------- /Python programs/Leetcode solution - Subsets II.py: -------------------------------------------------------------------------------- 1 | 2 | class Solution: 3 | def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: 4 | nums.sort() 5 | ans=[] #result array , stores subset array 6 | array=[] #subset array 7 | make(nums,array,0,ans) #function call for recursion or backtracking 8 | 9 | l=[] 10 | for i in ans: #selecting unique subsets from ans array 11 | if i not in l: 12 | l.append(i) 13 | return l #final answer 14 | 15 | 16 | def make(nums,array,i,ans): 17 | if i==len(nums): #if the index reaches to end of nums, 18 | ans.append(array) #time to store the subset formed 19 | return #return back to the called function 20 | make(nums,array,i+1,ans) #when the element need not to include in subset and i+1 is for next element 21 | #will backtrack 22 | make(nums,array+[nums[i]],i+1,ans) #when the element is included in subset and i+1 is for next element 23 | -------------------------------------------------------------------------------- /Python programs/Longest Common Subsequence.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestCommonSubsequence(self, text1: str, text2: str) -> int: 3 | m=len(text1) 4 | n=len(text2) 5 | dp=[[-1 for i in range(n)] for j in range(m)] 6 | 7 | def lcs(m,n): 8 | if m<0 or n<0: 9 | return 0 10 | if dp[m][n]!=-1: 11 | return dp[m][n] 12 | if text1[m]==text2[n]: 13 | dp[m][n]=1+lcs(m-1,n-1) 14 | return dp[m][n] 15 | dp[m][n]=max(lcs(m-1,n),lcs(m,n-1)) 16 | return dp[m][n] 17 | return lcs(m-1,n-1) 18 | 19 | 20 | -------------------------------------------------------------------------------- /Python programs/Longest Substring Without Repeating Characters.py: -------------------------------------------------------------------------------- 1 | def lengthOfLongestSubstring(self, s: str) -> int: 2 | maxlen=0 3 | ws=0 4 | d={} 5 | for i in range(len(s)): 6 | c=s[i] 7 | if c in d: 8 | index=d[c] 9 | if index>=ws: 10 | ws=index+1 11 | d[c]=i 12 | maxlen=max(maxlen,i-ws+1) 13 | return maxlen 14 | -------------------------------------------------------------------------------- /Python programs/Max Chunks To Make Sorted II.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution: 4 | def maxChunksToSorted(self, arr: List[int]) -> int: 5 | st=[] 6 | for i in arr: 7 | m=i #as the max range of any chunk (as some chunks can be just single element also) 8 | ## check if stack not empty and if previous chunk's max range (i.e, st[-1]) is greater 9 | ##than current arr[i] (i.e, m) that means the m is the part of the previous 10 | ##chunk , so then update the max range (insert into the stack and pop the previous value) of that chunk 11 | 12 | while st and st[-1]>i: 13 | m=max(m,st[-1]) 14 | st.pop() 15 | 16 | ##m is going to make the new chunk (starting of the new chunk) 17 | st.append(m) 18 | 19 | return len(st) 20 | PLEASE UPVOTE IF YOU FOUND THE SOLUTION HELPFUL 21 | 22 | 23 | Comments: 0 24 | 25 | Type comment here... (Markdown is supported) 26 | Enter topic title... 27 | -------------------------------------------------------------------------------- /Python programs/Maximum Sum of an Hourglass.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSum(self, grid: List[List[int]]) -> int: 3 | rows=len(grid) 4 | columns=len(grid[0]) 5 | s=0 6 | maxs=0 7 | for i in range(0,rows-2): 8 | for j in range(0,columns-2): 9 | s=grid[i][j] + grid[i][j+1] + grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2] 10 | if s>maxs: 11 | maxs=s 12 | return maxs 13 | -------------------------------------------------------------------------------- /Python programs/Monty_hall.py: -------------------------------------------------------------------------------- 1 | import random 2 | from random import seed, randint 3 | import numpy 4 | 5 | def game(winningdoor, selecteddoor, change=False): 6 | assert winningdoor < 3 7 | assert winningdoor >= 0 8 | 9 | # Presenter removes the first door that was not selected neither winning 10 | removeddoor = next(i for i in range(3) if i != selecteddoor and i != winningdoor) 11 | 12 | # Player decides to change its choice 13 | if change: 14 | selecteddoor = next(i for i in range(3) if i != selecteddoor and i != removeddoor) 15 | 16 | # We suppose the player never wants to change its initial choice. 17 | return selecteddoor == winningdoor 18 | 19 | 20 | if __name__ == '__main__': 21 | playerdoors = numpy.random.random_integers(0,2, (1000 * 1000 * 1,)) 22 | 23 | winningdoors = [d for d in playerdoors if game(1, d)] 24 | print("Winning percentage without changing choice: ", len(winningdoors) / len(playerdoors)) 25 | 26 | winningdoors = [d for d in playerdoors if game(1, d, change=True)] 27 | print("Winning percentage while changing choice: ", len(winningdoors) / len(playerdoors)) 28 | -------------------------------------------------------------------------------- /Python programs/Number of Longest Increasing Subsequence.py: -------------------------------------------------------------------------------- 1 | def findNumberOfLIS(self, nums: List[int]) -> int: 2 | lis=[1]*len(nums) 3 | count=[1]*len(nums) 4 | m=1 5 | for i in range(len(nums)): 6 | for j in range(0,i): 7 | if (nums[i]>nums[j] and lis[j]+1>lis[i]): 8 | lis[i]=lis[j]+1 9 | count[i]=count[j] 10 | else: 11 | if nums[i]>nums[j] and lis[j]+1==lis[i]: 12 | count[i]+=count[j] 13 | m=max(m,lis[i]) 14 | ans=0 15 | for i in range(len(nums)): 16 | if lis[i]==m: 17 | ans+=count[i] 18 | return ans 19 | -------------------------------------------------------------------------------- /Python programs/Password_genertaor.py: -------------------------------------------------------------------------------- 1 | import random 2 | length = int(input("enter the length of password")) 3 | s="abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" 4 | p = "".join(random.sample(s,length)) 5 | print ("Your Genertaed password is: "p) 6 | -------------------------------------------------------------------------------- /Python programs/Queries on Number of Points Inside a Circle.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]: 3 | l=[] 4 | for circle in range(len(queries)): 5 | count=0 6 | for point in range(len(points)): 7 | if (queries[circle][0]-points[point][0])**2 + (queries[circle][1]-points[point][1])**2 <= queries[circle][2]**2: 8 | count+=1 9 | l.append(count) 10 | return l 11 | -------------------------------------------------------------------------------- /Python programs/Quiz Game.py: -------------------------------------------------------------------------------- 1 | def check_guess(guess, answer): 2 | global score 3 | still_guessing = True 4 | attempt = 0 5 | while still_guessing and attempt < 3: 6 | if guess.lower() == answer.lower(): 7 | print("Correct Answer") 8 | score = score + 1 9 | still_guessing = False 10 | else: 11 | if attempt < 2: 12 | guess = input("Sorry Wrong Answer, try again") 13 | attempt = attempt + 1 14 | if attempt == 3: 15 | print("The Correct answer is ",answer ) 16 | 17 | score = 0 18 | print("Guess the Animal") 19 | guess1 = input("Which bear lives at the North Pole? ") 20 | check_guess(guess1, "polar bear") 21 | guess2 = input("Which is the fastest land animal? ") 22 | check_guess(guess2, "Cheetah") 23 | guess3 = input("Which is the larget animal? ") 24 | check_guess(guess3, "Blue Whale") 25 | print("Your Score is "+ str(score)) 26 | -------------------------------------------------------------------------------- /Python programs/Rolling Dice program.py: -------------------------------------------------------------------------------- 1 | import random 2 | while True: 3 | print("Press 1 if you want to roll a dice else just enter any key to exit from program") 4 | user = input("Press key: ") 5 | if user=="1": 6 | print("Value on dice is : "+random.randint(1,6)) 7 | else: 8 | break 9 | print("Program terminated '_'") 10 | -------------------------------------------------------------------------------- /Python programs/Score of Parentheses.py: -------------------------------------------------------------------------------- 1 | def scoreOfParentheses(self, s: str) -> int: 2 | stack=[] 3 | for i in s: 4 | if i=='(': 5 | stack.append(-1) 6 | else: 7 | if stack[-1]==-1: 8 | stack.pop() 9 | stack.append(1) 10 | else: 11 | s=0 12 | while stack[-1]!=-1: 13 | s+=stack.pop() 14 | stack.pop() 15 | stack.append(2*s) 16 | return sum(stack) 17 | -------------------------------------------------------------------------------- /Python programs/Simplfy Path.py: -------------------------------------------------------------------------------- 1 | def simplifyPath(self, path: str) -> str: 2 | stack=[] 3 | l=path.split('/') 4 | for i in l: 5 | if i=='.' or i=='': 6 | continue 7 | elif i=='..': 8 | if len(stack)!=0: 9 | stack.pop() 10 | else: 11 | stack.append(i) 12 | return '/'+'/'.join(stack 13 | -------------------------------------------------------------------------------- /Python programs/Smallest Subsequence of Distinct Characters.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestSubsequence(self, s: str) -> str: 3 | d={} 4 | for i in range(len(s)): 5 | d[s[i]]=i 6 | ans=set() 7 | stack=[] 8 | for i in range(len(s)): 9 | if s[i] in ans: 10 | continue 11 | if len(stack)!=0 and stack[-1]s[i] and d[stack[-1]]>i: 16 | a=stack.pop() 17 | ans.remove(a) 18 | stack.append(s[i]) 19 | ans.add(s[i]) 20 | return "".join(stack) 21 | -------------------------------------------------------------------------------- /Python programs/Teemo Attacking.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int: 3 | poison=timeSeries[0]+duration-1 #first one will always be attacked without overlapping 4 | #of poisoned time of others 5 | 6 | s=duration #initially atarting from timeSeries[0] so storing the duration from start 7 | 8 | for i in range(1,len(timeSeries)): 9 | attack=timeSeries[i] 10 | if attack<=poison: #if poison time overlapped 11 | 12 | s-=(poison-attack+1) #but if poisoned before the previous one ends 13 | #then need to decerment the time which is already been added in duration 14 | 15 | s+=duration #everytime duration time need to be added by default in every attack 16 | 17 | poison=timeSeries[i]+duration-1 #again calculating the poison time or other attack 18 | return s 19 | -------------------------------------------------------------------------------- /Python programs/alphaRangoli.py: -------------------------------------------------------------------------------- 1 | def print_rangoli(n): 2 | for i in range(n-1,-1,-1): 3 | for j in range(i): 4 | print(end="--") 5 | for j in range(n-1,i,-1): 6 | print(chr(j+97),end="-") 7 | for j in range(i,n): 8 | if j != n-1: 9 | print(chr(j+97),end="-") 10 | else: 11 | print(chr(j+97),end="") 12 | for j in range(2*i): 13 | print(end="-") 14 | print() 15 | for i in range(1,n): 16 | for j in range(i): 17 | print(end="--") 18 | for j in range(n-1,i,-1): 19 | print(chr(j+97),end="-") 20 | for j in range(i,n): 21 | if j != n-1: 22 | print(chr(j+97),end="-") 23 | else: 24 | print(chr(j+97),end="") 25 | for j in range(2*i): 26 | print(end="-") 27 | print() 28 | 29 | if __name__ == '__main__': 30 | n = int(input()) 31 | print_rangoli(n) 32 | -------------------------------------------------------------------------------- /Python programs/binarySearch.py: -------------------------------------------------------------------------------- 1 | def binarySearch(num, l, r, key): 2 | 3 | # base case 4 | if r >= l: 5 | 6 | mid = l + (r - l) // 2 7 | 8 | # If element is present in the middle 9 | if num[mid] == key: 10 | return mid 11 | 12 | # If element is smaller than mid, search in left sub-array 13 | elif num[mid] > key: 14 | return binarySearch(num, l, mid-1, key) 15 | 16 | # If element is larger than mid, search in right sub-array 17 | else: 18 | return binarySearch(num, mid + 1, r, key) 19 | 20 | else: 21 | # Element not found 22 | return -1 23 | 24 | 25 | # main 26 | N=int(input('\nEnter number of elements in the array:')) 27 | 28 | num = list(map(int,input("\nEnter the sorted array : ").strip().split()))[:] 29 | 30 | key = int(input('\nElement to be searched: ')) 31 | 32 | result = binarySearch(num, 0, len(num)-1, key) 33 | 34 | if result != -1: 35 | print("Element found at index ", result) 36 | else: 37 | print("Element not found") 38 | -------------------------------------------------------------------------------- /Python programs/collage_maker.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import numpy as np 3 | def collage_maker(image1, image2, name): 4 | i1 = np.array(image1) 5 | i2 = np.array(image2) 6 | collage = np.vstack([i1, i2]) 7 | image = Image.fromarray(collage) 8 | image.save(name) 9 | 10 | # To Run The Above Function 11 | collage_maker("image1.jpg", "image2.jpg", "new.jpg") 12 | -------------------------------------------------------------------------------- /Python programs/days.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | 4 | data = pd.read_csv("Seattle2014.csv") 5 | print(data.head()) 6 | 7 | rainfall = data["PRCP"].values 8 | inches = rainfall/254 9 | print(inches.shape) 10 | -------------------------------------------------------------------------------- /Python programs/fibonacci.py: -------------------------------------------------------------------------------- 1 | num = int(input("Enter a number: ")) 2 | factorial = 1 3 | if num < 0: 4 | print(" Factorial does not exist for negative numbers") 5 | elif num == 0: 6 | print("The factorial of 0 is 1") 7 | else: 8 | for i in range(1,num + 1): 9 | factorial = factorial*i 10 | print("The factorial of",num,"is",factorial) 11 | -------------------------------------------------------------------------------- /Python programs/hangman_game.py: -------------------------------------------------------------------------------- 1 | import time 2 | import random 3 | name = input("What is your name? ") 4 | print ("Hello, " + name, "Time to play hangman!") 5 | time.sleep(1) 6 | print ("Start guessing...\n") 7 | time.sleep(0.5) 8 | ## A List Of Secret Words 9 | words = ['python','programming','treasure','creative','medium','horror'] 10 | word = random.choice(words) 11 | guesses = '' 12 | turns = 5 13 | while turns > 0: 14 | failed = 0 15 | for char in word: 16 | if char in guesses: 17 | print (char,end="") 18 | else: 19 | print ("_",end=""), 20 | failed += 1 21 | if failed == 0: 22 | print ("\nYou won") 23 | break 24 | guess = input("\nguess a character:") 25 | guesses += guess 26 | if guess not in word: 27 | turns -= 1 28 | print("\nWrong") 29 | print("\nYou have", + turns, 'more guesses') 30 | if turns == 0: 31 | print ("\nYou Lose") 32 | -------------------------------------------------------------------------------- /Python programs/input.py: -------------------------------------------------------------------------------- 1 | while True: 2 | reply = input("Enter Text: ") 3 | if reply == 'stop': break 4 | print(reply) 5 | -------------------------------------------------------------------------------- /Python programs/insert_interval.py: -------------------------------------------------------------------------------- 1 | def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: 2 | ans=[] 3 | i=0 4 | 5 | while iintervals[i][1]: 6 | ans.append(intervals[i]) 7 | i+=1 8 | 9 | while i int: 2 | stack=[] 3 | stack.append(-1) 4 | maxlen=0 5 | for i in range(len(s)): 6 | if s[i]=='(': 7 | stack.append(i) 8 | else: 9 | stack.pop() 10 | if len(stack)==0: 11 | stack.append(i) 12 | else: 13 | maxlen=max(maxlen,i-stack[-1]) 14 | return maxlen 15 | -------------------------------------------------------------------------------- /Python programs/longestCommonSubsequence.py: -------------------------------------------------------------------------------- 1 | def longestCommonSubsequence(self, text1: str, text2: str) -> int: 2 | dp=[[-1 for i in range(len(text2)+1)]for j in range(len(text1)+1)] 3 | # print(dp) 4 | m=len(text1)+1 5 | n=len(text2)+1 6 | for i in range(m): 7 | for j in range(n): 8 | if i==0 or j==0: 9 | dp[i][j]=0 10 | elif text1[i-1]==text2[j-1]: 11 | dp[i][j]=1+dp[i-1][j-1] 12 | else: 13 | dp[i][j]=max(dp[i][j-1],dp[i-1][j]) 14 | print(dp) 15 | return max(max(dp)) 16 | -------------------------------------------------------------------------------- /Python programs/odd_even.py: -------------------------------------------------------------------------------- 1 | # Program to check whether a number is even or odd 2 | # uses Bitwise AND operator 3 | 4 | def isOdd(n) : 5 | 6 | # n&1 is 1 for odd 7 | return (n & 1); 8 | 9 | # main 10 | n = int(input("Enter a number: ")); 11 | if isOdd(n) : 12 | print("Odd Number") 13 | else: 14 | print("Even Number"); 15 | -------------------------------------------------------------------------------- /Python programs/password.py: -------------------------------------------------------------------------------- 1 | import random 2 | passlen = int(input("enter the length of password")) 3 | s="abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" 4 | p = "".join(random.sample(s,passlen )) 5 | print(p) 6 | -------------------------------------------------------------------------------- /Python programs/pearson_correlation.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | movies = pd.read_csv("MoviesOnStreamingPlatforms_updated.csv") 3 | movies['Rotten Tomatoes'] = movies["Rotten Tomatoes"].str.replace("%", "").astype(float) 4 | movies.drop("Type", inplace=True, axis=1) 5 | correlations = movies.corr(method='pearson') 6 | # Correlation Between All The Features 7 | print(correlations) 8 | 9 | # Correlation Between A Particular column "Year" 10 | print(correlations["Year"]) 11 | 12 | # Visualizing Correlation 13 | import seaborn as sns 14 | 15 | import matplotlib.pyplot as plt 16 | sns.heatmap(correlations) 17 | plt.show() 18 | -------------------------------------------------------------------------------- /Python programs/stock_price.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import yfinance as yf 3 | import datetime 4 | from datetime import date, timedelta 5 | today = date.today() 6 | 7 | d1 = today.strftime("%Y-%m-%d") 8 | end_date = d1 9 | d2 = date.today() - timedelta(days=360) 10 | d2 = d2.strftime("%Y-%m-%d") 11 | start_date = d2 12 | 13 | data = yf.download('AAPL', 14 | start=start_date, 15 | end=end_date, 16 | progress=False) 17 | print(data.head()) 18 | -------------------------------------------------------------------------------- /Python programs/storey_generator.py: -------------------------------------------------------------------------------- 1 | import random 2 | when = ['A few years ago', 'Yesterday', 'Last night', 'A long time ago','On 20th Jan'] 3 | who = ['a rabbit', 'an elephant', 'a mouse', 'a turtle','a cat'] 4 | name = ['Ali', 'Miriam','daniel', 'Hoouk', 'Starwalker'] 5 | residence = ['Barcelona','India', 'Germany', 'Venice', 'England'] 6 | went = ['cinema', 'university','seminar', 'school', 'laundry'] 7 | happened = ['made a lot of friends','Eats a burger', 'found a secret key', 'solved a mistery', 'wrote a book'] 8 | print(random.choice(when) + ', ' + random.choice(who) + ' that lived in ' + random.choice(residence) + ', went to the ' + random.choice(went) + ' and ' + random.choice(happened)) 9 | -------------------------------------------------------------------------------- /Python programs/video_to_audio.py: -------------------------------------------------------------------------------- 1 | from pytube import YouTube 2 | import pytube 3 | import os 4 | 5 | def main(): 6 | video_url = input('Enter YouTube video URL: ') 7 | 8 | if os.name == 'nt': 9 | path = os.getcwd() + '\\' 10 | else: 11 | path = os.getcwd() + '/' 12 | 13 | name = pytube.extract.video_id(video_url) 14 | YouTube(video_url).streams.filter(only_audio=True).first().download(filename=name) 15 | location = path + name + '.mp4' 16 | renametomp3 = path + name + '.mp3' 17 | 18 | if os.name == 'nt': 19 | os.system('ren {0} {1}'. format(location, renametomp3)) 20 | else: 21 | os.system('mv {0} {1}'. format(location, renametomp3)) 22 | 23 | if __name__ == '__main__': 24 | main() 25 | -------------------------------------------------------------------------------- /Web-dev/CSS wildcard/CSS-wildcard.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | Practical-11 7 | 22 | 23 | 24 |

Test page

25 |
The first div element.
26 |
The second div element.
27 |
The third div element.
28 |

Paragraph Text

29 | 30 | -------------------------------------------------------------------------------- /Web-dev/DOM/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | My Website 6 | 7 | 8 | 9 |

Hello

10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Web-dev/DOM/index.js: -------------------------------------------------------------------------------- 1 | alert("hello"); 2 | document.querySelector("h1").innerHTML = "Good Bye"; 3 | -------------------------------------------------------------------------------- /Web-dev/DOM/master.css: -------------------------------------------------------------------------------- 1 | h1{ 2 | color: red; 3 | } 4 | -------------------------------------------------------------------------------- /Web-dev/RestfulApi/.gitignore: -------------------------------------------------------------------------------- 1 | README.md 2 | package.json 3 | node_modules -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/design.css: -------------------------------------------------------------------------------- 1 | body{ 2 | background-image: url("station.jpg"); 3 | background-size: cover; 4 | font-family: 'Roboto Slab', serif; 5 | font-weight: bold; 6 | text-align: center; 7 | } 8 | 9 | h1{ 10 | margin-top: 10px; 11 | margin-bottom: 10px; 12 | } 13 | 14 | h2{ 15 | font-size: 50px; 16 | margin-top: 0; 17 | margin-bottom: 20pxs; 18 | } 19 | button{ 20 | border: none; 21 | padding-top: 10px; 22 | padding-bottom: 10px; 23 | color: white; 24 | font-weight: bold; 25 | width: 200px; 26 | margin-bottom: 5px; 27 | border-radius: 5px; 28 | } 29 | 30 | #increment-btn{ 31 | background: darkred; 32 | } 33 | 34 | #save-btn{ 35 | background: green; 36 | } -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/design5.css: -------------------------------------------------------------------------------- 1 | body{ 2 | font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; 3 | text-align: center; 4 | background-image: url("table.jpg"); 5 | background-size: cover; 6 | color: white; 7 | font-weight: bold; 8 | } 9 | 10 | h1{ 11 | color: goldenrod; 12 | } 13 | 14 | #message-el 15 | { 16 | font-style: italic; 17 | } 18 | 19 | button{ 20 | color: #016f32; 21 | width: 150px; 22 | background-color: goldenrod; 23 | padding-top: 5px; 24 | padding-bottom: 5px; 25 | font-weight: bold; 26 | border: none; 27 | border-radius: 2px; 28 | margin-top: 2px; 29 | margin-bottom: 2px; 30 | } -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/index3.js: -------------------------------------------------------------------------------- 1 | let errorParagraph=document.getElementById("error") 2 | console.log(errorParagraph) 3 | function purchase() 4 | { 5 | errorParagraph.textContent="Something went wrong, please try again" 6 | } -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/index4.js: -------------------------------------------------------------------------------- 1 | let num1=8 2 | let num2=2 3 | document.getElementById("num1-el").textContent=num1; 4 | document.getElementById("num2-el").textContent=num2; 5 | 6 | function add() 7 | { 8 | let result=num1+num2; 9 | document.getElementById("sum-el").textContent="Sum: "+result; 10 | } 11 | 12 | function subtract() 13 | { 14 | let result=num1-num2; 15 | document.getElementById("sum-el").textContent="Sum: "+result; 16 | } 17 | 18 | function divide() 19 | { 20 | let result=num1/num2; 21 | document.getElementById("sum-el").textContent="Sum: "+result; 22 | } 23 | 24 | function multiply() 25 | { 26 | let result=num1*num2; 27 | document.getElementById("sum-el").textContent="Sum: "+result; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/station.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Web-dev/Wheel_of_fortune/freecodecamp_js_tut/station.jpg -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/table.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dixitt5/CP-Solutions/ed3a8ed832f6a0f3c6edacdd996413764b4b2b4e/Web-dev/Wheel_of_fortune/freecodecamp_js_tut/table.jpg -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/tut.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 |

People entered:

11 |

0

12 | 13 | 14 |

Previous entries:

15 | 16 | 17 | -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/tut2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 |

10 | 11 | 12 | -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/tut4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 |
17 | Sum: 18 | 19 | 20 | -------------------------------------------------------------------------------- /Web-dev/Wheel_of_fortune/freecodecamp_js_tut/tut5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 |

Blackjack

11 |

Want to play a round?

12 |

Cards:

13 |

Sum:

14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Web-dev/app.js: -------------------------------------------------------------------------------- 1 | const express=require("express") 2 | const bodyParser=require("body-parser") 3 | const { redirect } = require("statuses") 4 | 5 | const todo=["TODOLIST"] 6 | const works=["WORK"] 7 | 8 | const app=express() 9 | app.set('view engine', 'ejs') 10 | 11 | app.use(bodyParser.urlencoded({extended:true})) 12 | app.use(express.static("public")) 13 | 14 | app.get("/",(req,res)=>{ 15 | res.render("list",{theList:"TodoList",newItem:todo,theList1:"WORK",newItem1:works}) 16 | }) 17 | 18 | 19 | app.post("/",(req,res)=>{ 20 | console.log(req.body.list) 21 | if(req.body.list==="TodoList") 22 | { 23 | todo.push(req.body.item) 24 | res.redirect("/") 25 | } 26 | else{ 27 | works.push(req.body.item1) 28 | res.redirect("/") 29 | } 30 | 31 | }) 32 | 33 | app.listen("3000",()=>{console.log("fuck it's running")}) -------------------------------------------------------------------------------- /Web-dev/app1.js: -------------------------------------------------------------------------------- 1 | const express=require("express") 2 | const bodyParser=require("body-parser") 3 | const { redirect } = require("statuses") 4 | 5 | const todo=["TODOLIST"] 6 | const works=["WORK"] 7 | 8 | const app=express() 9 | app.set('view engine', 'ejs') 10 | 11 | app.use(bodyParser.urlencoded({extended:true})) 12 | app.use(express.static("public")) 13 | 14 | app.get("/",(req,res)=>{ 15 | res.render("list",{theList:"TodoList",newItem:todo,theList1:"WORK",newItem1:works}) 16 | }) 17 | 18 | 19 | app.post("/",(req,res)=>{ 20 | console.log(req.body.list) 21 | if(req.body.list==="TodoList") 22 | { 23 | todo.push(req.body.item) 24 | res.redirect("/") 25 | } 26 | else{ 27 | works.push(req.body.item1) 28 | res.redirect("/") 29 | } 30 | 31 | }) 32 | 33 | app.listen("3000",()=>{console.log("fuck it's running")}) -------------------------------------------------------------------------------- /Web-dev/readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Web-dev/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #D7E9F7; 3 | } 4 | hr { 5 | 6 | border-style: none; 7 | border-top-style: dotted; 8 | border-color: gray; 9 | border-width: 5px; 10 | 11 | width: 5%; 12 | } 13 | h1 { 14 | color: #66BFBF; 15 | } 16 | h3 { 17 | color: #66BFBF; 18 | } 19 | img:hover { 20 | background-color: gold; 21 | 22 | } 23 | 24 | .work{ 25 | background-color: aquamarine; 26 | } 27 | .circular{ 28 | border-radius: 100%; 29 | } -------------------------------------------------------------------------------- /basicsCheatsheet.js: -------------------------------------------------------------------------------- 1 | // Numbers: 2 | 1; 3 | -99; 4 | 0.345345; 5 | 6 | //Making variables with let: 7 | let numberOfFriends = 1; 8 | 9 | //Incrementing: 10 | numberOfFriends += 3; //numberOfFriends is now 4 11 | 12 | // Variables with const 13 | const minimumAge = 21; //CANNOT REASSIGN! 14 | 15 | //Booleans - true or false values 16 | true; 17 | false; 18 | let isHappy = true; 19 | 20 | //Naming Conventions 21 | // Use upper camel-cased names: 22 | let numberOfChickens = 6; //GOOD 23 | // NOT THE JS WAY: 24 | // let number_of_chickens = 6; -------------------------------------------------------------------------------- /c programming/Pattern EQI-T 1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,n; 6 | printf("Enter How many Rows to print: "); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { 10 | for(j=1;j<=(n*2-1);j++) 11 | { 12 | if(j>=(n+1)-i && j<=(n-1)+i) 13 | printf("*"); 14 | else 15 | printf(" "); 16 | } 17 | printf("\n"); 18 | } 19 | getch(); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /c programming/Pattern EQI-T 2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,n; 6 | printf("Enter How Many Rows to print: "); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { 10 | for(j=1;j<=(n*2-1);j++) 11 | { 12 | if(j>=i && j<=n*2-i) 13 | printf("*"); 14 | else 15 | printf(" "); 16 | } 17 | printf("\n"); 18 | } 19 | getch(); 20 | } 21 | -------------------------------------------------------------------------------- /c programming/Pattern EQI-T 3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,k=0,rows; 6 | printf("Enter How many rows to print: "); 7 | scanf("%d",&rows); 8 | for(i=1;i<=rows;i++) 9 | { 10 | if(rows%2==0) 11 | { 12 | if(i<=rows/2) 13 | k++; 14 | if(i>rows/2+1) 15 | k--; 16 | } 17 | else 18 | i<=(rows+1)/2?k++:k--; 19 | for(j=1;j<=(rows+1)/2;j++) 20 | { 21 | if(j<=k) 22 | { 23 | printf("*"); 24 | } 25 | else 26 | printf(" "); 27 | } 28 | printf("\n"); 29 | } 30 | getch(); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /c programming/Pattern EQI-T 4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,n,k=0; 6 | printf("Enter a Number of Rows: "); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { 10 | if(n%2==0) 11 | { 12 | if(i<=n/2) 13 | k++; 14 | if(i>n/2+1) 15 | k--; 16 | } 17 | else 18 | i<=(n/2+1)?k++:k--; 19 | for(j=1;j<=(n/2+1);j++) 20 | { 21 | if(j>=(n/2+2)-k) 22 | printf("*"); 23 | else 24 | printf(" "); 25 | } 26 | printf("\n"); 27 | } 28 | getch(); 29 | } 30 | -------------------------------------------------------------------------------- /c programming/Pattern EQI-T with Space 5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,k,n; 6 | printf("Enter How Many Rows to Print: "); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { k=1; 10 | for(j=1;j<=(n*2-1);j++) 11 | { 12 | if(j>=(n+1)-i && j<=(n-1)+i && k) 13 | { printf("*"); 14 | k=0; } 15 | else{ 16 | printf(" "); 17 | k=1; 18 | } 19 | } 20 | printf("\n"); 21 | } 22 | getch(); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /c programming/To_find_keyword.txt: -------------------------------------------------------------------------------- 1 | extern,return,union,const,float, short, auto, double, int, struct, break, else, long, goto, sizeof, voltile, do ,if, static, while, unsigned, continue, for, signed, void, default, switch, case, enum, register, typedef, char -------------------------------------------------------------------------------- /c programming/TowerofHanoi.c: -------------------------------------------------------------------------------- 1 | #include //preprocessor directive 2 | void tower_hanoi(int n,char A,char B,char C) //Tower of Hanoi Function 3 | { if(n>0) 4 | { tower_hanoi(n-1,A,C,B); 5 | printf("Moved: %d no. disc (%c,%c)\n",n,A,C); //Moving disc from one place to other 6 | tower_hanoi(n-1,B,A,C); 7 | } 8 | } 9 | int main() //Decleration of Main function 10 | { int a; 11 | printf("Enter number of disks in which TOH operation to be performed: "); 12 | scanf("%d",&a); 13 | tower_hanoi(a,'A','B','C'); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /c programming/if-else statement.c: -------------------------------------------------------------------------------- 1 | // if else statement 2 | #include 3 | int main(){ 4 | float retailPrice,payableAmount; 5 | puts("enter the retail price: "); 6 | scanf("%f", &retailPrice); 7 | if (retailPrice>=300) { 8 | puts("eligible for discount"); 9 | payableAmount = retailPrice * 0.8; 10 | } else { 11 | puts("not eligible for discount"); 12 | payableAmount = retailPrice; 13 | } 14 | printf("discount availed : %f\n", retailPrice - payableAmount); 15 | printf("net payable amount : %f", payableAmount); 16 | return 0; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /c programming/palindromeexplanation.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); //consider n=55 6 | scanf("%d",&n); 7 | temp=n; // let temp=n=55 8 | while(n>0) //55>0 true 9 | { 10 | r=n%10; //r=5 11 | sum=(sum*10)+r; //sum=0*10+5=5 12 | n=n/10; //n=55/10=5 13 | } //repeat again n=5>0 so,r=5%10=5 ,sum=5*10+5=55,n=5/10=0.5 14 | if(temp==sum) //temp=sum=55 15 | printf("palindrome number "); //so 55 is palindrome number 16 | else 17 | printf("not palindrome"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /c programming/palindromenum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); 6 | scanf("%d",&n); 7 | temp=n; 8 | while(n>0) 9 | { 10 | r=n%10; 11 | sum=(sum*10)+r; 12 | n=n/10; 13 | } 14 | if(temp==sum) 15 | printf("palindrome number "); 16 | else 17 | printf("not palindrome"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /c programming/palindromenumexample.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); //consider n=55 6 | scanf("%d",&n); 7 | temp=n; // let temp=n=55 8 | while(n>0) //55>0 true 9 | { 10 | r=n%10; //r=5 11 | sum=(sum*10)+r; //sum=0*10+5=5 12 | n=n/10; //n=55/10=5 13 | } //repeat again n=5>0 so,r=5%10=5 ,sum=5*10+5=55,n=5/10=0.5 14 | if(temp==sum) //temp=sum=55 15 | printf("palindrome number "); //so 55 is palindrome number 16 | else 17 | printf("not palindrome"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /c programming/pattern programs/pattern-1.c: -------------------------------------------------------------------------------- 1 | //Output:Right Triangle Star Pattern 2 | // Enter the number of rows:5 3 | // * 4 | // * * 5 | // * * * 6 | // * * * * 7 | // * * * * * 8 | 9 | #include 10 | 11 | int main() 12 | { 13 | int n; 14 | printf("Enter the number of rows:"); 15 | scanf("%d",&n); 16 | for(int i=1;i<=n;i++) 17 | { 18 | for(int j=1;j<=i;j++) 19 | { 20 | printf("* "); 21 | } 22 | printf("\n"); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /c programming/pattern programs/pattern-2.c: -------------------------------------------------------------------------------- 1 | // Output:Hollow Right Triangle Star Pattern 2 | // Enter the number of rows:10 3 | // * 4 | // ** 5 | // * * 6 | // * * 7 | // * * 8 | // * * 9 | // * * 10 | // * * 11 | // * * 12 | // ********** 13 | 14 | #include 15 | int main() 16 | { 17 | int n; 18 | printf("Enter the number of rows:"); 19 | scanf("%d",&n); 20 | for(int i=1;i<=n;i++) 21 | { 22 | for(int j=1;j<=i;j++) 23 | { 24 | if(j==1|| i==j || i==n ) 25 | { 26 | printf("*"); 27 | } 28 | else 29 | printf(" "); 30 | } 31 | printf("\n"); 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /c programming/pattern programs/pattern-3.c: -------------------------------------------------------------------------------- 1 | // Output:Inverted Mirrored Right Triangle Star Pattern 2 | // Enter the number of rows:5 3 | // ***** 4 | // **** 5 | // *** 6 | // ** 7 | // * 8 | 9 | #include 10 | 11 | int main() 12 | { 13 | int n,m; 14 | printf("Enter the number of rows:"); 15 | scanf("%d",&n); 16 | m=n; 17 | for(int i=1;i<=n;i++) 18 | { 19 | for(int j=1;j 15 | 16 | int main() 17 | { 18 | int n,m; 19 | printf("Enter the number of rows:"); 20 | scanf("%d",&n); 21 | m=n; 22 | for(int i=1;i<=n;i++) 23 | { 24 | for(int j=1;j 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int N; 9 | int T; 10 | vectorS; 11 | int *A; 12 | cin>>N; 13 | A=new int[N]; 14 | for(int i=0;i>T; 16 | A[i]=T; 17 | } 18 | sort(A,A+N); 19 | /* To count the number of Integeres in the array*/ 20 | int Count=1; 21 | for(int i=0;i