├── CodeChef ├── README.md ├── FSQRT Finding Square Roots.py ├── FLIPCARDS.py ├── trailingzero.cpp ├── biryani classes codechef ├── MANIPULATE ├── EVMHACK@codechef.cpp ├── CARVANS.cpp ├── Java │ ├── LUCKFOUR Lucky Four.java │ └── COMM3 Three Way Communications.java ├── CIELAB Ciel and A-B Problem.py ├── CNOTE.cpp ├── minimiseLCS.cpp ├── MEX-OR.cpp ├── NAICHEF Naive Chef.java ├── FODCHAIN Food Chain.cpp └── ARRCONS.cpp ├── LeetCode ├── README.md ├── 2446. Determine if Two Events Have Conflict.cpp ├── 1732 Find The Highest Altitude.cpp ├── 10. Regular Expression Matching.cpp ├── 47 Permutations II.cpp ├── Create Reverse Integer.cpp ├── 233. Number of Digit One.cpp ├── 147. Insertion Sort List.cpp ├── 96 Unique Binary Search Trees ├── Number of Steps to Reduce a Number to Zero.cpp ├── 198 House Robber.cpp ├── 2447. Number of Subarrays With GCD Equal to K.cpp ├── 70 Climbing Stairs.cpp ├── 2428. Maximum Sum of an Hourglass ├── 11. Container With Most Water.cpp ├── 621. Task Scheduler.cpp ├── 3_Remove_Element.cpp ├── 12 Integer to roman.cpp ├── 437 Path Sum III.cpp ├── 91 Decode Ways.cpp ├── Find Peak Element.cpp ├── 50 Pow(x, n).cpp ├── 309 Best Time to Buy and Sell Stock with Cooldown.cpp ├── 279 Perfect Squares.cpp ├── 41 First Missing positive.cpp ├── 724.FindPivotIndex.cpp ├── Decode ways.java ├── 1 Two Sum.cpp ├── 982 Triples with Bitwise AND Equal To Zero.cpp ├── 2139. Minimum Moves to Reach Target Score.cpp ├── 2_Remove_Duplicates_from_Sorted_Array.cpp ├── 6_Container_With_Most_Water.cpp ├── 7. Reverse Integer.cpp ├── 2443 Sum of Number and Its Reverse.cpp ├── 2443. Sum of Number and Its Reverse.cpp ├── 334. Increasing Triplet Subsequence ├── 2449. Minimum Number of Operations to Make Arrays Similar.cpp ├── Increasing Triplet Subsequence.cpp ├── 5_Plus_One.cpp ├── Java │ ├── GasStation134.java │ ├── 122 BestTimeToBuyAndSellStocks2.java │ ├── 349.-Intersection-of-Two-Arrays.java │ ├── KthSmallestElementBST.java │ ├── HouseRobber2.java │ ├── longestSubstring_3.java │ ├── 581 Shortest Unsorted Continuous Subarray.java │ ├── CoinChangePermutation.java │ ├── EditDistance.java │ └── 64 Minimum Path Sum.java ├── 976. Largest perimeter triangle.cpp ├── 3 Longest Substring Without Repeating Characters.cpp ├── 4_Search_Insert_Position.cpp ├── 20. Valid Parentheses.cpp ├── 9 Palindrome Number.cpp ├── 62 Unique Paths.cpp ├── 732 My Calendar III.cpp ├── 334 Increasing Triplet Subsequence.cpp ├── 45 Jump Game II.cpp ├── 1_Two_Sum.cpp ├── permutation.cpp ├── 786 K-th Smallest Prime Fraction.cpp ├── 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts ├── Valid-number.py ├── Maximum Depth of Binary Tree.cpp ├── 2448. Minimum Cost to Make Array Equal.cpp ├── 75. Sort colors.cpp ├── 2432. The Employee That Worked on the Longest Task ├── 583. Delete Operation for Two Strings ├── 2441. Largest Positive Integer That Exists With Its Negative.cpp ├── 2400. Number of Ways to Reach a Position After Exactly k Steps ├── 424. Longest Repeating Character Replacement.cpp ├── 72 Edit Distance.cpp ├── 10 Regular Expression Matching.cpp ├── 31. Next Permutation.cpp ├── 712. Minimum ASCII Delete Sum for Two Strings ├── 39 Combination Sum.cpp ├── 473. Matchsticks to Square ├── 200 Number of Islands.cpp ├── 42 Trapping Rain Water.cpp ├── 1664. Ways to Make a Fair Array ├── 56. Merge Intervals.cpp ├── 60. Permutation Sequence.cpp ├── 19 Remove Nth Node From End of List.cpp ├── 1383. Maximum Performance of a Team.cpp ├── Largest plus Sign Brute force.cpp ├── 653 Two SUM IV - Input is a BST.cpp ├── 1870. Minimum Speed to Arrive on Time ├── 112 Path Sum.cpp ├── 16 3Sum Closest.cpp ├── 6 ZigZag Conversion.cpp ├── 692. Top K Frequent Words.cpp ├── 2444. Count Subarrays With Fixed Bounds.cpp ├── 2442. Count Number of Distinct Integers After Reverse Operations.cpp ├── 150. Evaluate Reverse Polish Notation ├── 1328. Break a Palindrome ├── 17 Letter Combinations of a Phone Number.cpp ├── 169. majorityElement.cpp ├── 2 Add Two Numbers.cpp ├── 48 Rotate Image.cpp ├── 5. Longest Palindromic Substring.cpp ├── 238. Product of Array Except Self ├── 32 Longest Valid Parentheses.cpp ├── 234 Palindrome Linked List.cpp ├── 123 Best Time to Buy and Sell Stock III.cpp ├── 88_compare_two_linked_list.cpp ├── 15 3Sum.cpp ├── 1337 kWeakestRows.cpp ├── 34. Find First and Last Position of Element in Sorted Array.cpp ├── 983 Minimum Cost For Tickets.cpp └── 2409. Count Days Spent Together ├── C++ ├── Cpp_Pattrens │ ├── tempCodeRunnerFile.cpp │ ├── sixthPattren.cpp │ ├── fourthpattren.cpp │ ├── thirdpattren.cpp │ ├── fifthPattren.cpp │ ├── secondPattren.cpp │ ├── fristPattren.cpp │ ├── sevenPattren.cpp │ ├── ninthPattren.cpp │ ├── TwetlthPattren.cpp │ ├── elventhPattren.cpp │ ├── eightPattren.cpp │ └── tenthPattren.cpp ├── README.md ├── DecToBin.cpp └── reverseWordsInString.cpp ├── Codeforces ├── README.md ├── 4A Watermelon.cpp ├── 1537B Bad Boy.cpp ├── weirdalgo.cpp ├── 208A_Dubstep.cpp ├── Amr_and_Pins.cpp ├── 1742A_Sum.c ├── 507B_Amr_and_Pins.cpp ├── Jzzuh_and_Sequence.cpp ├── 271A.Beautiful Year.cpp ├── 231A Team.cpp ├── Queue_At_the_School.cpp ├── Random_Teams.cpp ├── 1551 B1 Wonderful Coloring-1 ├── DZY_loves_strings.cpp ├── Fedor_and_New_Game.cpp ├── 1530B Putting Plates.cpp ├── Xenia_and_Ringroad.cpp ├── 158B_Taxi.cpp ├── 1287A_Angry_Students.cpp └── twosets.cpp ├── InterviewBit ├── README.md ├── Maximum_Distance.cpp ├── Level_Order_Traversal.cpp └── First_missing_positive.cpp ├── 70+codechef_solutions ├── First.cpp ├── 2X.cpp ├── hoop.cpp ├── chef_office.cpp ├── playlist.cpp ├── reach_target.cpp ├── rich.cpp ├── Mango.cpp ├── exit.cpp ├── insurance.cpp ├── miami_GP.cpp ├── chef_n_choco.cpp ├── assignments.cpp ├── best_coupon.cpp ├── cooler.cpp ├── ROT.cpp ├── practice.cpp ├── taxes.cpp ├── pizza.cpp ├── Dominant_army.cpp ├── ac.cpp ├── jenga.cpp ├── qual.cpp ├── subscriptions.cpp ├── shoe.cpp ├── taller.cpp ├── MOnthly_budget.cpp ├── aud.rng.cpp ├── expiry.cpp ├── filcandy.cpp ├── RC.cpp ├── course_reg.cpp ├── mb.cpp ├── test_avg.cpp ├── blackjack.cpp ├── candy.cpp ├── dishes.cpp ├── high_accuracy.cpp ├── osp.cpp ├── greater_avg.cpp ├── stock.cpp ├── footbal.cpp ├── weightbal.cpp ├── discus.cpp ├── mt.cpp ├── mahasena.cpp ├── min_car.cpp ├── BWF.cpp ├── chef_and_masks.cpp ├── rank.cpp ├── tea.cpp ├── chess.cpp ├── chef_n_candies.cpp ├── chfspl.cpp ├── level.cpp ├── TV_discount.cpp ├── sale.cpp ├── speciality.cpp ├── vaccine.cpp ├── bottles.cpp ├── Football_cup.cpp ├── proglang.cpp ├── Vol_control.cpp ├── todolist.cpp ├── bird_farm.cpp ├── exp_setter.cpp ├── dcc.cpp ├── Cheaper_cab.cpp ├── motivation.cpp ├── tastiness.cpp └── chess_dist.cpp ├── subtwonum.cpp ├── RemoveElement.cpp ├── Pepcoding ├── 1. Basics of Programming │ ├── 2D-arrays │ │ ├── exitPointOfMatrix.java │ │ ├── array2d_demo.java │ │ └── stateOfWakanda1.java │ ├── Patterns │ │ ├── pattern14.java │ │ ├── pattern1.java │ │ ├── pattern2.java │ │ ├── pattern8.java │ │ ├── pattern7.java │ │ ├── pattern9.java │ │ ├── pattern11.java │ │ ├── pattern12.java │ │ ├── pattern4.java │ │ ├── pattern3.java │ │ ├── pattern_m.java │ │ ├── pattern20.java │ │ ├── pattern13.java │ │ ├── pattern5.java │ │ ├── pattern6.java │ │ ├── pattern18.java │ │ ├── pattern10.java │ │ ├── inverted_hourglass.java │ │ ├── pattern17.java │ │ ├── pattern16.java │ │ └── pattern15.java │ ├── Getting Started │ │ ├── digitCounter.java │ │ ├── Largest3.java │ │ ├── primeFactorisation.java │ │ ├── inverseNumber.java │ │ ├── PrimeChecker.java │ │ ├── lcm_gcd.java │ │ ├── eachDigit.java │ │ └── BenjaminBulbs.java │ └── function and arrays │ │ ├── digitFrequency1.java │ │ ├── decimal_to_any_base.java │ │ ├── findElementInArray.java │ │ ├── any_base_to_decimal.java │ │ ├── subArrayProblem.java │ │ ├── spanOfArray.java │ │ ├── barChart.java │ │ ├── invertedBarChart.java │ │ ├── subsetsOfArray.java │ │ ├── anyBaseAddition.java │ │ ├── inverseOfAnArray.java │ │ ├── brokenEconomy.java │ │ └── anyBaseSubtraction.java └── trial.java ├── Number of Steps to Reduce_a_Number_to_Zero.md ├── computing powers.cpp ├── Judging Delay.cpp ├── Program to print simple interest.c ├── leap_year.c ├── Google Kickstart 2022 Round G Walktober.py ├── factorialUsingRecursion.java ├── 229. Majority Element II.cpp ├── LCS.cpp ├── greatestCommonDivisor.cpp ├── largest-sum-contiguous.cpp ├── 581 Shortest Unsorted Continuous Subarray.java ├── trappingWater.cpp ├── test.cpp ├── 2095. Delete the Middle Node of a Linked List ├── insertionSort.cpp └── Range_Sum_Query.cpp /CodeChef/README.md: -------------------------------------------------------------------------------- 1 | # CodeChef Solutions 2 | -------------------------------------------------------------------------------- /LeetCode/README.md: -------------------------------------------------------------------------------- 1 | # LeetCode Solutions 2 | -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | { -------------------------------------------------------------------------------- /Codeforces/README.md: -------------------------------------------------------------------------------- 1 | # Codeforces Solutions 2 | -------------------------------------------------------------------------------- /InterviewBit/README.md: -------------------------------------------------------------------------------- 1 | # InterviewBit Solutions 2 | -------------------------------------------------------------------------------- /C++/README.md: -------------------------------------------------------------------------------- 1 | Any C++ program file can be added here 2 | -------------------------------------------------------------------------------- /Codeforces/4A Watermelon.cpp: -------------------------------------------------------------------------------- 1 | #import 2 | main(int n){ 3 | std::cin>>n;puts(n<3|n%2?"NO":"YES"); 4 | } 5 | -------------------------------------------------------------------------------- /CodeChef/FSQRT Finding Square Roots.py: -------------------------------------------------------------------------------- 1 | t = int(input()) 2 | 3 | for i in range(t): 4 | curr_num = int(input()) 5 | print(round(curr_num ** (1/2))) 6 | -------------------------------------------------------------------------------- /70+codechef_solutions/First.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | cout<<"this is the first file of this repository"; 7 | } -------------------------------------------------------------------------------- /CodeChef/FLIPCARDS.py: -------------------------------------------------------------------------------- 1 | for _ in range(int(input())): 2 | N,X = map(int, input().split()) 3 | if N>2*X: 4 | print(X) 5 | else: 6 | print(N-X) -------------------------------------------------------------------------------- /70+codechef_solutions/2X.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int x,y; 6 | cin>>x>>y; 7 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n; 6 | cin>>t; 7 | for(i=0;i>n; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | cout<<4*x+y< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,x; 6 | cin>>t; 7 | for(i=0;i>a>>b>>x; 10 | cout<<(b-a)/x< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | cout<<(z-y)/x< 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int f1, s2, sub; 7 | cout << "Enter two numbers: "; 8 | cin >> f1 >> s2; 9 | sub = f1 - s2; 10 | cout << f1 << " - " << s2 << " = " << sub; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /CodeChef/trailingzero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t, n = 5; 5 | cin>>t; 6 | int ans = 0; 7 | while(n<=t){ 8 | ans+=t/n; 9 | n*=5; 10 | } 11 | cout<& event1, vector& event2) { 4 | if(event2[0] <= event1[1] && event2[1] >= event1[0]) return true; 5 | return false; 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /CodeChef/biryani classes codechef: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int x, y ; 9 | cin>>x>>y; 10 | cout<& gain) { 4 | int p=0,m=0; 5 | for(auto g: gain){ 6 | p += g; 7 | m = max(m, p); 8 | } 9 | return m; 10 | } 11 | }; -------------------------------------------------------------------------------- /Codeforces/1537B Bad Boy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve() 4 | { 5 | int n,m,i,j; 6 | cin>>n>>m>>i>>j; 7 | cout<<1<<" "<<1<<" "<>t; 13 | while(t--) 14 | { 15 | solve(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /70+codechef_solutions/exit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>50) 11 | cout<<"RIGHT"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n; 6 | cin>>t; 7 | for(i=0;i>m>>n; 10 | if(m>n) 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(y<=x*1.07) 11 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,c,x,y; 6 | cin>>t; 7 | for(i=0;i>c>>x>>y; 10 | if(c<=x) 11 | cout<<0<vec; 2 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(10-x>=3) 11 | cout<<"Yes"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x/10 > 100) 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,m; 6 | cin>>t; 7 | for(i=0;i>x>>y>>m; 10 | if(x*m 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>=30) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[100]; 6 | int i,c=0; 7 | for(i=0;i<4;i++) 8 | { 9 | cin>>arr[i]; 10 | if(arr[i]>=10) 11 | { 12 | c+=1; 13 | } 14 | } 15 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>100) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if((n*x)%4==0) 11 | cout<<(n*x)/4< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x>y+z || y>x+z || z>x+y) 11 | cout<<"YES"<> permute(vector& v) { 4 | sort(v.begin(),v.end()); 5 | vector>p; 6 | do{ 7 | p.push_back(v); 8 | }while(next_permutation(v.begin(),v.end())); 9 | return p; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/Create Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | long long sum=0; 5 | while(x!=0) 6 | { 7 | sum=sum*10+x%10; 8 | x/=10; 9 | } 10 | if(sum>-pow(2,31) && sum 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if(a<=b && c<=b) 11 | { 12 | cout<<"Yes"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(x%n==0 && x>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,a,b; 6 | cin>>t; 7 | for(i=0;i>n>>a>>b; 10 | if(n<=a+2*b) 11 | { 12 | cout<<"Qualify"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(n%6!=0) 11 | { 12 | cout<<(n/6 + 1)*x< 2 | using namespace std; 3 | int main(){ 4 | long long int n; 5 | cin>>n; 6 | while(n!=1){ 7 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,m; 6 | cin>>t; 7 | for(i=0;i>n>>m; 10 | if(n>m) 11 | { 12 | cout<<(2*n-m)< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x>y) 11 | { 12 | cout<<"A"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,m,n; 6 | cin>>t; 7 | for(i=0;i>m>>n; 10 | if(m 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x>=67 && x<=45000) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,n,m,k; 6 | cin>>t; 7 | for(i=0;i>n>>m>>k; 10 | if(n<=m*k) 11 | { 12 | cout<<"yes"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,k,m; 6 | cin>>t; 7 | for(i=0;i>n>>k>>m; 10 | if(n%(k*m)==0) 11 | { 12 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int x, y; 10 | cin>>x>>y; 11 | if(x>=y) 12 | { 13 | cout<<"Yes"; 14 | } 15 | else 16 | { 17 | cout<<"No"; 18 | } 19 | cout< 0) { 6 | cnt += (n + 8) / 10 * i + (n % 10 == 1) * j; 7 | j += n % 10 * i; 8 | i *= 10; 9 | n /= 10; 10 | } 11 | return cnt; 12 | } 13 | }; -------------------------------------------------------------------------------- /70+codechef_solutions/RC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a!=c && b!=d) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n,k; 6 | cin>>t; 7 | for(i=0;i>n>>m>>k; 10 | if(m-k>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | 7 | cin>>t; 8 | for(i=0;i>x>>y>>z; 11 | if(z>y/x) 12 | { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if((a+b)/2 >= 35 && (a+c)/2 >= 35 && (c+b)/2 >= 35) 11 | cout<<"PASS"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | if((21-(a+b))<=10) 11 | { 12 | cout<<21-(a+b)< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,m; 6 | cin>>t; 7 | for(i=0;i>n>>m; 10 | if(n%m==0 && (n/m)%2==0) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=i;j++) 8 | { 9 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | string s; 6 | int count=1;cin>>s; 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | long double r,x,y,a,b; 6 | cin>>r>>x>>y>>a>>b; 7 | long double d1=(x-a)*(x-a); 8 | long double d2=(y-b)*(y-b); 9 | 10 | long double d=(sqrt(d1+d2)); 11 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,a,b,c; 6 | cin>>t; 7 | for(i=0;i>n>>a>>b>>c; 10 | if((a+c)>=n && b>=n) 11 | { 12 | cout<<"YES"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x!=0 && x%3 != 0) 11 | { 12 | cout<<3-x%3< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=5;j>=i;j--) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /* 18 | * * * * * 19 | * * * * 20 | * * * 21 | * * 22 | * 23 | 24 | */ 25 | -------------------------------------------------------------------------------- /70+codechef_solutions/osp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if((a%2==0 && b%2==0 && c%2==0) || (a%2!=0 && b%2!=0 && c%2!=0)) 11 | { 12 | cout<<"NO"< next) 5 | for(auto j = head; j != cur; j = j -> next) 6 | if(j -> val > cur -> val) 7 | swap(j -> val, cur -> val); 8 | return head; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/96 Unique Binary Search Trees: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | int dp[20]; 5 | dp[0]=dp[1]=1; 6 | for(int i=2;i<=n;i++) 7 | { 8 | int s=0; 9 | for(int j=i;j>0;j--) 10 | s+=dp[i-j]*dp[j-1]; 11 | dp[i]=s; 12 | } 13 | return dp[n]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/Number of Steps to Reduce a Number to Zero.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int numberOfSteps(int num) { 5 | int count = 0 ; 6 | while(num!=0){ 7 | if ( num %2==0) 8 | num = num/2; 9 | else 10 | num = num-1; 11 | count++; 12 | } 13 | 14 | return count; 15 | } 16 | 17 | }; 18 | -------------------------------------------------------------------------------- /70+codechef_solutions/greater_avg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | float s; 7 | cin>>t; 8 | for(i=0;i>a>>b>>c; 11 | s = ((float)a + (float)b)/2; 12 | if(s>c) 13 | { 14 | cout<<"YES"< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<=i;j++) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /** 18 | * * 19 | * ** 20 | * *** 21 | * **** 22 | * ***** 23 | * 24 | */ 25 | -------------------------------------------------------------------------------- /70+codechef_solutions/stock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t; 6 | double s,a,b,c; 7 | cin>>t; 8 | for(i=0;i>s>>a>>b>>c; 11 | if((s+s*c/100)>=a && (s+s*c/100)<=b) 12 | { 13 | cout<<"YES"< 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<5;j++) 8 | { 9 | cout<<"* "; 10 | } 11 | cout<<"\n"; 12 | 13 | } 14 | return 0; 15 | } 16 | 17 | /** 18 | * ***** 19 | * ***** 20 | * ***** 21 | * ***** 22 | * ***** 23 | * 24 | */ 25 | -------------------------------------------------------------------------------- /70+codechef_solutions/footbal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b; 10 | cin>>c>>d; 11 | if(a<=c && b<=d) 12 | { 13 | cout<<"POSSIBLE"< 2 | int main(){ 3 | int t; 4 | scanf("%d",&t); 5 | 6 | while(t--){ 7 | int a,b,c; 8 | scanf("%d%d%d",&a,&b,&c); 9 | 10 | if( ( a==b+c || b==a+c ) || c==a+b ){ 11 | printf("YES \n"); 12 | } 13 | 14 | else{ 15 | printf("NO \n"); 16 | } 17 | 18 | 19 | }; 20 | } -------------------------------------------------------------------------------- /LeetCode/198 House Robber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int rob(vector& nums) { 4 | int n=nums.size(); 5 | if(n==0) return 0; 6 | int lk[n+1]; 7 | lk[0]=0; 8 | lk[1]=nums[0]; 9 | for(int i=2;i 2 | #include 3 | using namespace std; 4 | 5 | int power(int x, int n) 6 | { 7 | if(n == 0) 8 | return 1; 9 | 10 | int temp = power(x, n/2); 11 | 12 | temp = temp * temp; 13 | 14 | if(n % 2 == 0) 15 | return temp; 16 | else 17 | return temp * x; 18 | } 19 | int main() { 20 | 21 | int x = 9, n = 6; 22 | 23 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if(a>=b && a>=c) 11 | { 12 | cout<=a && b>=c) 15 | { 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x%3==0) 11 | { 12 | cout<<"NORMAL"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t; 6 | cin>>t; 7 | int arr[100]; 8 | int count = 0; 9 | for(i=0;i>arr[i]; 12 | if(arr[i]%2==0) 13 | { 14 | count = count+1; 15 | } 16 | } 17 | if(count>t/2) 18 | cout<<"READY FOR BATTLE"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n; 6 | cin>>t; 7 | for(i=0;i>n; 10 | if(n%4==0) 11 | { 12 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,w,x,y,z; 6 | cin>>t; 7 | for(i=0;i>w>>x>>y>>z; 10 | if(x-w > y*z) 11 | { 12 | cout<<"Unfilled"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,m,n; 6 | long int d,c; 7 | string str[100]; 8 | cin>>t; 9 | for(i=0;i>m>>n; 12 | d = m*100; 13 | c = n*10; 14 | if(c<=d) 15 | { 16 | cout<<"Cloth"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x/25==0) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x%y==0) 11 | { 12 | cout<<(x/y)*z< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x>=y) 11 | { 12 | cout<<"0"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x; 6 | cin>>t; 7 | for(i=0;i>n>>x; 10 | if(n<=x) 11 | { 12 | cout<<"0"< 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int s[n],j[n]; 11 | for(int i=1;i<=n;i++){ 12 | cin>>s[i]>>j[i]; 13 | } 14 | int count=0; 15 | for(int i=1;i<=n;i++){ 16 | if(j[i]-s[i]>5) count++; 17 | else continue; 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c; 10 | if(a>=c && b>=c) 11 | { 12 | cout<=a && c>=a) 15 | { 16 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(x<3) 11 | { 12 | cout<& nums, int k) { 2 | int cnt = 0; 3 | for (int i = 0; i < size(nums); i++) 4 | { 5 | int currGcd = 0; 6 | for (int j = i; j < size(nums); j++) 7 | { 8 | currGcd = gcd(currGcd, nums[j]); 9 | if (currGcd == k) cnt++; 10 | } 11 | } 12 | return cnt; 13 | } 14 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Getting Started/digitCounter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class digitCounter { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int num = sc.nextInt(); 8 | int count = 0; 9 | 10 | while (num != 0) { 11 | num = num / 10; 12 | count++; 13 | } 14 | System.out.println(count); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Program to print simple interest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | float interest, p, r, n; 7 | clrscr(); 8 | 9 | printf("Enter value of P: "); 10 | scanf("%f",&p); 11 | 12 | printf("Enter value of R: "); 13 | scanf("%f",&r); 14 | 15 | printf("Enter value of N: "); 16 | scanf("%f",&n); 17 | 18 | interest=p*r*n/100f; 19 | 20 | printf("Simple Interest : %f", interest); 21 | 22 | getch(); 23 | } 24 | -------------------------------------------------------------------------------- /70+codechef_solutions/TV_discount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t,a,b,c,d,i; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a-c>b-d) 11 | { 12 | cout<<"Second"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x; 6 | cin>>t; 7 | for(i=0;i>x; 10 | if(x<=100) 11 | cout< 2 | using namespace std; 3 | #define int int64_t 4 | int32_t main() 5 | { 6 | int r; cin>>r; 7 | int x,y,x1,y1; 8 | cin>>x>>y>>x1>>y1; 9 | double d = sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y)); 10 | int ans = d/(2*r); 11 | if (ans*2*r==d) 12 | { 13 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t,i,k,l,m; 6 | cin>>t; 7 | for(i=0;i>k>>l>>m; 10 | if(k>l && k>m) 11 | { 12 | cout<<"Setter"<k && l>m) 15 | { 16 | cout<<"Tester"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,d,l,r; 6 | cin>>t; 7 | for(i=0;i>d>>l>>r; 10 | if(d>=l && d<=r) 11 | { 12 | cout<<"Take second dose now"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int num; 9 | cout<< "Enter number: "; 10 | cin>>num; 11 | 12 | int ans = 0; 13 | int i = 0; 14 | 15 | 16 | while(num!=0) 17 | { 18 | int bit = num & 1; 19 | ans = (bit * pow(10, i)) + ans ; 20 | cout<> 1; 23 | i++; 24 | } 25 | 26 | cout<>& g) { 4 | int rows = g.size(); 5 | int cols = g[0].size(); 6 | int ans = 0; 7 | for(int i=0;i<=rows-3;i++){ 8 | for(int j=0;j<=cols-3;j++){ 9 | ans = max(ans, g[i][j]+g[i][j+1]+g[i][j+2]+g[i+1][j+1]+g[i+2][j]+g[i+2][j+1]+g[i+2][j+2]); 10 | } 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern2 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | for (int i = num; i >= 1; i--) { 9 | for (int j = 1; j <= i; j++) { 10 | System.out.print("*\t"); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode/11. Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int lb=0,ub=height.size()-1,ans=0,curr; 5 | while(lbheight[lb]) 10 | lb++; 11 | else 12 | ub--; 13 | } 14 | return ans; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/621. Task Scheduler.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastInterval(vector& tasks, int n) { 4 | unordered_mapmp; 5 | int count = 0; 6 | for(auto e : tasks) 7 | { 8 | mp[e]++; 9 | count = max(count, mp[e]); 10 | } 11 | 12 | int ans = (count-1)*(n+1); 13 | for(auto e : mp) if(e.second == count) ans++; 14 | return max((int)tasks.size(), ans); 15 | } 16 | }; -------------------------------------------------------------------------------- /Codeforces/Jzzuh_and_Sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | #define N 1000000007 7 | int main(){ 8 | 9 | ll x,y; 10 | cin>>x>>y; 11 | ll t; 12 | cin>>t; 13 | ll a[6]; 14 | a[0]=(x-y); 15 | a[1]=x; 16 | a[2]=y; 17 | a[3]=(y-x); 18 | a[4]=-x; 19 | a[5]=-y; 20 | 21 | ll k=a[t%6]; 22 | 23 | ll h=(k%N+N)%N; 24 | cout< 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int removeElement(vector& nums, int val) { 8 | int size = nums.size(); 9 | int j = 0; 10 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,n,x,k; 6 | cin>>t; 7 | for(i=0;i>n>>x>>k; 10 | if(x>k) 11 | { 12 | cout<<"0"<k/x) 17 | { 18 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | float a,b,c,p,q,r; 7 | cin>>t; 8 | while(t--) 9 | { 10 | cin>>a>>b>>c>>p>>q>>r; 11 | double ans=(p+q+r)/2; 12 | if((a+b+c)>ans || (p+b+c)>ans || (a+q+c)>ans || (a+b+r)>ans) 13 | { 14 | cout<<"YES"<left, sum) + pathSum(root->right, sum); 6 | } 7 | private: 8 | int sumUp(TreeNode* root, int pre, int& sum){ 9 | if(!root) return 0; 10 | int current = pre + root->val; 11 | return (current == sum) + sumUp(root->left, current, sum) + sumUp(root->right, current, sum); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Codeforces/271A.Beautiful Year.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool vis[10]; 5 | 6 | bool isunique ( int y ) 7 | { 8 | memset(vis,0,sizeof vis); 9 | while(y) 10 | { 11 | if ( vis[ y%10 ] ) return 0 ; 12 | vis[y%10]=1; 13 | y/=10; 14 | } 15 | return 1; 16 | } 17 | 18 | int main() 19 | { 20 | int n ; 21 | scanf("%d",&n); 22 | bool f=0; 23 | for(int i=n+1 ; !f ; i++) 24 | { 25 | if ( isunique(i) ) 26 | { 27 | printf("%d\n",i); 28 | f=1; 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /LeetCode/91 Decode Ways.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDecodings(string s) { 4 | int n=s.size(); 5 | if(s[0]=='0') return 0; 6 | int lk[n+1]; 7 | memset(lk,0,sizeof(lk)); 8 | lk[0]=1; 9 | lk[1]=1; 10 | for(int i=2;i& nums) { 4 | //int ans=0; 5 | int s=0,e=nums.size()-1; 6 | 7 | while(s 2 | using namespace std; 3 | 4 | int main() { 5 | int i,k,m,n; 6 | string str[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | 12 | if(m==n && m>0 && n>0) 13 | { 14 | str[i] = "YES"; 15 | } 16 | else 17 | { 18 | str[i] = "NO"; 19 | } 20 | } 21 | 22 | for(i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,a1,b1,a2,b2; 6 | cin>>t; 7 | for(i=0;i>a>>b>>a1>>b1>>a2>>b2; 10 | if((a==a1 && b==b1)||(a==b1 && b==a1)) 11 | { 12 | cout<<"1"< 2 | using namespace std; 3 | 4 | int main() { 5 | int i,j,k,m,n; 6 | int arr[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | if(m>n) 12 | { 13 | j = m-n; 14 | } 15 | else if(n>m) 16 | { 17 | j = n-m; 18 | } 19 | else 20 | { 21 | j = m-n; 22 | } 23 | arr[i] = j; 24 | } 25 | for(i=0;i &A) { 2 | int n=A.size(); 3 | int rightMax[n]; 4 | rightMax[n-1]= A[n-1]; 5 | for(int i = n-2; i>=0; i--) 6 | rightMax[i] = max(rightMax[i+1] , A[i]); 7 | int maxDist = INT_MIN; 8 | int i = 0, j = 0; 9 | while(i= A[i]) 12 | { 13 | maxDist = max( maxDist, j-i ); 14 | j++; 15 | } 16 | else 17 | i++; 18 | } 19 | return maxDist; 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/50 Pow(x, n).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double myPow(double x, int n) 4 | { 5 | double answer=cal(x,llabs(n)); 6 | if(n>0) return answer; 7 | else return 1/answer; 8 | } 9 | double cal(double x,long long int n) 10 | { 11 | if(n==0) return 1; 12 | else 13 | { 14 | double temp=cal(x,n/2); 15 | if(n%2==0) return temp*temp; 16 | else return x*temp*temp; 17 | } 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /CodeChef/CARVANS.cpp: -------------------------------------------------------------------------------- 1 | //CodeChef Problem Code: CARVANS 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,m,c; 10 | cin>>t; 11 | while(t--){ 12 | cin>>n; i=c=0; 13 | int *arr = new int[n]; 14 | while(i>arr[i++]; 16 | } 17 | m=arr[0]; i=c=1; 18 | while(i& prices) { 4 | int buy = -prices[0], sell = 0, cool = 0; 5 | for(int i=1;i buy) 8 | buy = cool - prices[i]; 9 | cool = sell; 10 | if(buy + prices[i] > sell) 11 | sell = buy + prices[i]; 12 | 13 | } 14 | return sell; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /leap_year.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x; 5 | printf("Enter a Year => "); 6 | scanf("%d", &x); 7 | if(x%4==0) 8 | { 9 | if(x%100==0) 10 | { 11 | if(x%400==0) 12 | { 13 | printf("%d is a leap year", x); 14 | } 15 | else 16 | { 17 | printf("%d is not a leap year", x); 18 | 19 | } 20 | } 21 | else 22 | { 23 | printf("%d is a leap year", x); 24 | } 25 | } 26 | else 27 | { 28 | printf("%d is not a leap year"); 29 | } 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /70+codechef_solutions/todolist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int loop(int n) 4 | { 5 | int j; 6 | int arr[1000]; 7 | int count = 0; 8 | for(j=0;j>arr[j]; 11 | if(arr[j]>=1000) 12 | { 13 | count+=1; 14 | } 15 | } 16 | return count; 17 | } 18 | 19 | int main() { 20 | int i,t,n; 21 | cin>>t; 22 | for(i=0;i>n; 26 | cout< n/i) 11 | i = n; 12 | int e = 0; 13 | while (n % i == 0) 14 | n /= i, ++e; 15 | if (e % 2 && i % 4 == 3) 16 | return 3; 17 | min2 |= e % 2; 18 | } 19 | return 1 + min2; 20 | } 21 | 22 | }; 23 | -------------------------------------------------------------------------------- /70+codechef_solutions/bird_farm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,z; 6 | cin>>t; 7 | for(i=0;i>x>>y>>z; 10 | if(z%x==0 && z%y==0) 11 | { 12 | cout<<"ANY"< max) 13 | max = b; 14 | 15 | if (c > max) 16 | max = c; 17 | 18 | System.out.println("Max number : " + max); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/sevenPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout<<" "; 10 | } 11 | 12 | for(int j=1;j<=(2*i-1);j++) 13 | { 14 | cout<<"*"; 15 | } 16 | 17 | 18 | cout<<"\n"; 19 | 20 | } 21 | return 0; 22 | } 23 | 24 | // Reverse of this can be done easily do it yourself. 25 | /* 26 | 27 | * 28 | *** 29 | ***** 30 | ******* 31 | ********* 32 | 33 | */ -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/ninthPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=0;j<=i;j++) 8 | { 9 | cout<<"*"; 10 | } 11 | cout<<"\n"; 12 | } 13 | 14 | for(int i=0;i<=4;i++) 15 | { 16 | for(int j=4-i;j>0;j--) 17 | { 18 | cout<<"*"; 19 | } 20 | cout<<"\n"; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | /* 27 | 28 | * 29 | ** 30 | *** 31 | **** 32 | ***** 33 | **** 34 | *** 35 | ** 36 | * 37 | 38 | */ -------------------------------------------------------------------------------- /LeetCode/41 First Missing positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int firstMissingPositive(vector &nums) 5 | { 6 | vector m(nums.size() + 1, false); 7 | for (int i = 0; i < nums.size(); i++) 8 | { 9 | if (nums[i] > 0 && nums[i] <= nums.size()) 10 | m[nums[i]] = true; 11 | } 12 | for (int i = 1; i <= nums.size(); i++) 13 | { 14 | if (!m[i]) 15 | return i; 16 | } 17 | return nums.size() + 1; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/724.FindPivotIndex.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector& nums) { 4 | int k=nums.size(); 5 | int rightsum = 0; 6 | int leftsum = 0; 7 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int t, count = 0, flag = 0; 5 | cin>>t; 6 | while(t--){ 7 | int a[3]; 8 | for(int i=0;i<3;i++){ 9 | cin>>a[i]; 10 | } 11 | for(int i=0;i<3;i++){ 12 | if(a[i] == 1){ 13 | count++; 14 | } 15 | } 16 | if(count >=2){ 17 | flag++; 18 | } 19 | count = 0; 20 | 21 | } 22 | cout<26){ 16 | d2=0; 17 | } 18 | sing=s2; 19 | dou=d2; 20 | } 21 | return sing+dou; 22 | } 23 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Getting Started/primeFactorisation.java: -------------------------------------------------------------------------------- 1 | // Display all the prime factors of a number 2 | 3 | import java.util.Scanner; 4 | 5 | public class primeFactorisation { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int num = sc.nextInt(); 10 | 11 | for (int i = 2; i <= num; i++) { 12 | while (num % i == 0) { 13 | System.out.print(i + " "); 14 | num /= i; 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /70+codechef_solutions/exp_setter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,x,y,m; 6 | cin>>t; 7 | for(i=0;i>x>>y; 10 | if(x%2==0) 11 | { 12 | m = x/2; 13 | if(y>=m) 14 | cout<<"YES"<=m) 22 | cout<<"YES"< twoSum(vector& nums, int target) { 4 | vector answer; 5 | unordered_map map; 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n,k; 7 | cin>>n>>k; 8 | string s; 9 | cin>>s; 10 | int hhh=223; 11 | int ct=0; 12 | for(int j=0;j > m; 4 | TimeMap() {} 5 | 6 | void set(string key, string value, int timestamp) { 7 | m[key][ timestamp]=value; 8 | } 9 | 10 | string get(string key, int timestamp) { 11 | 12 | if( m.find( key)==m.end()) return ""; 13 | auto t=m[ key].upper_bound(timestamp); 14 | if( t==m[key].begin()) return ""; 15 | 16 | t--; 17 | return t->second; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/2139. Minimum Moves to Reach Target Score.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(int t, int d) { 4 | int c=0; 5 | while(t!=1){ 6 | if(t%2==0 && d!=0){ 7 | t/=2; 8 | d--; 9 | c++; 10 | continue; 11 | } 12 | if(d!=0){ 13 | t--; 14 | c++; 15 | } 16 | else{ 17 | c+=t-1; 18 | break; 19 | } 20 | } 21 | return c; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/2_Remove_Duplicates_from_Sorted_Array.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int removeDuplicates(vector& nums) { 8 | int size = nums.size(); 9 | int left = 0; 10 | for(int right = 1; right < size; right++){ 11 | if(nums[left] != nums[right]){ 12 | left++; 13 | nums[left] = nums[right]; 14 | } 15 | } 16 | return left+1; 17 | } 18 | }; -------------------------------------------------------------------------------- /CodeChef/CIELAB Ciel and A-B Problem.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | text_inp = input() 4 | A, B = text_inp.split(' ') 5 | 6 | ans = int(A) - int(B) 7 | 8 | # We change the digit at this index 9 | # to a random digit from 1 to 9 10 | rand_digit = random.randrange(0, len(str(ans))) 11 | dig_to_change = int(str(ans)[rand_digit]) 12 | 13 | new_dig = dig_to_change 14 | 15 | while not new_dig != dig_to_change: 16 | new_dig = random.randrange(1, 9) 17 | 18 | solution = list(str(ans)) 19 | solution[rand_digit] = str(new_dig) 20 | solution = ''.join(solution) 21 | print(solution) 22 | -------------------------------------------------------------------------------- /Codeforces/Random_Teams.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int minS(long long int a,long long int b){ 4 | if(a>b) return b; 5 | else return a; 6 | } 7 | int main(){ 8 | long long int n,m; 9 | cin>>n>>m; 10 | 11 | 12 | long long int d=n-(m-1)-1; 13 | 14 | long long int max=d*(d+1)/2; 15 | 16 | long long int a=n/m; 17 | long long int b=n%m; 18 | 19 | long long int min=(a*(a+1)/2)*(n%m)+ ((a-1)*a/2)*(m-b); 20 | 21 | cout< 3 | using namespace std; 4 | 5 | #include 6 | using namespace std; 7 | 8 | class Solution { 9 | public: 10 | int maxArea(vector& height) { 11 | int greatest = 0; 12 | int l = 0, r = height.size() - 1; 13 | while(l < r){ 14 | greatest = max(greatest, min(height[l],height[r]) * (r-l)); 15 | height[l] <= height[r] ? l++: r--; 16 | } 17 | return greatest; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/7. Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | bool flag = false; 5 | 6 | int num = 0; 7 | 8 | while(x) 9 | { 10 | if( (num > INT_MAX/10) || (num < INT_MIN/10) ) return 0; 11 | else num = num*10; 12 | 13 | if(x>0 && (num > INT_MAX - x%10 )) return 0; 14 | else if( x<0 && (num < INT_MIN - x%10) ) return 0; 15 | else num += x%10; 16 | x = x/10; 17 | } 18 | return num; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /70+codechef_solutions/dcc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int fun(int n) 4 | { 5 | int j; 6 | int count=0; 7 | char str[10000]; 8 | for(j=0;j>str[j]; 11 | } 12 | for(j=0;j>t; 27 | for(i=0;i>n; 30 | cout<& v) { 4 | int n=v.size(); 5 | int a=INT_MAX; 6 | int b=INT_MAX; 7 | int c=INT_MAX; 8 | for(int i=0;i=v[i] ){ 10 | a=v[i]; 11 | } 12 | else if(v[i]<=b){ 13 | b=v[i]; 14 | } 15 | else if(v[i]<=c){ 16 | c=v[i]; 17 | return true; 18 | } 19 | } 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern8.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern8 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | int space = 0; 9 | for (int i = 1; i <= num; i++) { 10 | 11 | for (int j = 1; j <= space; j++) { 12 | System.out.print("\t"); 13 | } 14 | System.out.print("*"); 15 | 16 | space++; 17 | 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /70+codechef_solutions/Cheaper_cab.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int i,k,m,n; 6 | string str[1000]; 7 | cin>>k; 8 | for(i=0;i>m>>n; 11 | 12 | if(mn) 17 | { 18 | str[i] = "SECOND"; 19 | } 20 | else 21 | { 22 | str[i] = "ANY"; 23 | } 24 | } 25 | 26 | for(i=0;i& A, vector& B) { 2 | sort(A.begin(), A.end()); sort(B.begin(), B.end()); 3 | vector oA, eA, oB, eB; 4 | for (int& x: A) (x % 2) ? oA.push_back(x) : eA.push_back(x); 5 | for (int& x: B) (x % 2) ? oB.push_back(x) : eB.push_back(x); 6 | auto incCount = [&](vector& a, vector &b, long long ans = 0) { 7 | for (int i = 0; i < a.size(); i++) 8 | if (a[i] > b[i]) ans += (a[i] - b[i]) / 2; 9 | return ans; 10 | }; 11 | return incCount(oA, oB) + incCount(eA, eB); 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode/Increasing Triplet Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector& nums) { 4 | vector lis; 5 | lis.push_back(nums[0]); 6 | for(int i=1;ilis.back()) 9 | lis.push_back(nums[i]); 10 | else 11 | { 12 | auto it=lower_bound(begin(lis),end(lis),nums[i])-lis.begin(); 13 | lis[it]=nums[i]; 14 | } 15 | } 16 | 17 | 18 | return lis.size()>=3; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/5_Plus_One.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/plus-one/description/ 2 | #include 3 | using namespace std; 4 | 5 | 6 | class Solution { 7 | public: 8 | vector plusOne(vector& digits) { 9 | int size = digits.size(); 10 | for(int i=size-1; i>=0; i--){ 11 | if(digits[i]<9){ 12 | ++digits[i]; 13 | return digits; 14 | } else { 15 | digits[i] = 0; 16 | } 17 | } 18 | digits.push_back(0); 19 | digits[0] = 1; 20 | return digits; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/Java/GasStation134.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int canCompleteCircuit(int[] gas, int[] cost) { 3 | int n=gas.length; 4 | int total_surplus=0; 5 | int s=0;//In the tank 6 | int start=0; 7 | for(int i=0;i& nums) { 4 | //sort in descending order 5 | sort(nums.rbegin(), nums.rend()); 6 | int n= nums.size(); 7 | 8 | //check every triplet from largest numbers 9 | for(int i=1;i mpp; 7 | int maxx = 0, start = 0; 8 | for (int i = 0; i < s.size(); ++i) 9 | { 10 | if (mpp.find(s[i]) != mpp.end()) 11 | { 12 | start = max(mpp[s[i]] + 1, start); 13 | } 14 | mpp[s[i]] = i; 15 | int len = i - start + 1; 16 | if (len > maxx) 17 | maxx = len; 18 | } 19 | return maxx; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/4_Search_Insert_Position.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/search-insert-position/ 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int searchInsert(vector& nums, int target) { 8 | int size = nums.size(); 9 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | 8 | int T,x,y,k,n,p,c; 9 | cin>>T; 10 | 11 | while(T>0) 12 | { 13 | cin>>x>>y>>k>>n; 14 | 15 | x=x-y; 16 | 17 | bool luck = false; 18 | for(int i =0;i>p>>c; 21 | if(x<=p&&k>=c) 22 | luck = true; 23 | } 24 | 25 | if(luck) 26 | cout<<"LuckyChef\n"; 27 | else 28 | cout<<"UnluckyChef\n"; 29 | 30 | T--; 31 | 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /LeetCode/20. Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #include 4 | 5 | 6 | class Solution { 7 | public: 8 | bool isValid(string s) { 9 | stack stk; 10 | for(char c:s){ 11 | if(stk.empty()) 12 | stk.push(c); 13 | else if( (c==')' && stk.top()=='(') || (c=='}' && stk.top()=='{') || (c==']' && stk.top()=='[') ) 14 | stk.pop(); 15 | else 16 | stk.push(c); 17 | } 18 | if(!stk.empty()) 19 | return false; 20 | return true; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/9 Palindrome Number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | 5 | if(x==0){ 6 | return true; 7 | } 8 | if(x%10==0 ){ 9 | return false; 10 | } 11 | int reversed = 0; 12 | while(x>reversed){ 13 | int pop = x%10; 14 | x /= 10; 15 | reversed = (reversed*10) + pop; 16 | } 17 | if(x == reversed || x == reversed/10){ 18 | return true; 19 | }else{ 20 | return false; 21 | } 22 | 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Codeforces/1551 B1 Wonderful Coloring-1: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve() 5 | { 6 | string s; 7 | cin>>s; 8 | int freq[26]{0}; 9 | int ans=0; 10 | //int c[s.length()]; 11 | int c1=0,c2=0; 12 | for(int i=0;i>t; 32 | while(t--) 33 | { 34 | solve(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Codeforces/DZY_loves_strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin>>s; 7 | int n; 8 | cin>>n; 9 | int a[26]; 10 | for(int i=1;i<=26;i++){ 11 | cin>>a[i-1]; 12 | } 13 | //cout<> dp(m, vector(n, 0)); 7 | for (int i = 0; i < m; i++) 8 | { 9 | dp[i][n - 1] = 1; 10 | } 11 | for (int i = 0; i < n; i++) 12 | dp[m - 1][i] = 1; 13 | for (int i = m - 2; i >= 0; i--) 14 | for (int j = n - 2; j >= 0; j--) 15 | dp[i][j] = dp[i + 1][j] + dp[i][j + 1]; 16 | return dp[0][0]; 17 | } 18 | int uniquePaths(int m, int n) 19 | { 20 | return ans(m, n); 21 | } 22 | }; -------------------------------------------------------------------------------- /factorialUsingRecursion.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class factorialUsingRecursion 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 | -------------------------------------------------------------------------------- /LeetCode/Java/122 BestTimeToBuyAndSellStocks2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] price) { 3 | int n=price.length; 4 | int profit[]=new int[n]; 5 | profit[0]=0; 6 | int prev=price[0],max; 7 | int i; 8 | for(i=1;i 2 | using namespace std; 3 | int fun(int n, int x) 4 | { 5 | int j,a[1000],b[1000],count=0; 6 | for(j=0;j>a[j]>>b[j]; 9 | } 10 | for(j=0;j=a[j]) 13 | { 14 | if(b[j]>count) 15 | { 16 | count = b[j]; 17 | } 18 | } 19 | } 20 | return count; 21 | 22 | } 23 | int main() { 24 | int i,t,n,x; 25 | cin>>t; 26 | for(i=0;i>n>>x; 29 | cout< s,e; 4 | set t; 5 | MyCalendarThree() { 6 | 7 | } 8 | 9 | int book(int start, int end) { 10 | s.insert(start); 11 | e.insert(end); 12 | t.insert(start); 13 | t.insert(end); 14 | int c=0,m=0; 15 | for(auto i:t) 16 | { 17 | if(s.count(i)) 18 | c+=s.count(i); 19 | // m=max(m,c); 20 | if(e.count(i)) 21 | c-=e.count(i); 22 | m=max(m,c); 23 | } 24 | return m; 25 | } 26 | }; -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Getting Started/PrimeChecker.java: -------------------------------------------------------------------------------- 1 | // java program to check if a number is prime or not 2 | public class PrimeChecker { 3 | public static void main(String[] args) { 4 | int num = 31; 5 | 6 | boolean isPrime = true; 7 | for (int i = 2; i <= Math.sqrt(num); i++) { 8 | if (num % i == 0) { 9 | isPrime = false; 10 | break; 11 | } 12 | } 13 | 14 | if (isPrime) 15 | System.out.println(num + " is prime."); 16 | else 17 | System.out.println(num + " is not prime"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern9.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern9 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | for (int i = 0; i < num; i++) { 9 | for (int j = 0; j < num; j++) { 10 | if ((i == j) || (i + j == num - 1)) { 11 | System.out.print("*\t"); 12 | } else { 13 | System.out.print("\t"); 14 | } 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /70+codechef_solutions/tastiness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d,p,q,r,s; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | p=a+c; 11 | q=a+d; 12 | r=b+c; 13 | s=b+d; 14 | if(p>=q && p>=r && p>=s) 15 | { 16 | cout<=p && q>=r && q>=s) 20 | { 21 | cout<=p && r>=q && r>=s) 24 | { 25 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | class Solution { 7 | public: 8 | bool increasingTriplet(vector& nums) { 9 | pair thr = make_pair(INT_MAX,INT_MAX); 10 | int m; 11 | for(int i = 1;i < nums.size();i++){ 12 | if(nums[i] > thr.second) return true; 13 | if((m = min(thr.first,nums[i-1])) < nums[i]){ 14 | thr = make_pair(m,nums[i]); 15 | } 16 | } 17 | return false; 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | 24 | Solution mysol ; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /LeetCode/45 Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int helper(vector& nums,vector& lk,int n) 4 | { 5 | if(n>=nums.size()) return INT_MAX; 6 | if(n==nums.size()-1) return 0; 7 | if(nums[n]==0) return INT_MAX; 8 | if(lk[n]!=-1) return lk[n]; 9 | lk[n]=INT_MAX-1; 10 | for(int i=1;i<=nums[n];i++) 11 | { 12 | lk[n]=min(lk[n],helper(nums,lk,n+i)); 13 | } 14 | return ++lk[n]; 15 | } 16 | int jump(vector& nums) { 17 | vector lk(nums.size()+1,-1); 18 | return helper(nums,lk,0); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/1_Two_Sum.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/two-sum/ 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | vector twoSum(vector& nums, int target) { 8 | int len = nums.size(); 9 | vector vec; 10 | for(int i=0; i& nums,vector>&v,int i) 4 | { 5 | if(i>=nums.size()) 6 | { 7 | v.push_back(nums); 8 | return; 9 | } 10 | for(int j=i;j> permute(vector& nums) 19 | { 20 | vector>v; 21 | int i=0; 22 | solve(nums,v,i); 23 | return v; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /CodeChef/minimiseLCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int a,ans=0; 12 | cin>>a; 13 | string A,B; 14 | cin>>A; 15 | cin>>B; 16 | 17 | 18 | int a1[26]={0}; 19 | int a2[26]={0}; 20 | for (int i=0;i(); 4 | 5 | for(int i=0;i= j) { 13 | System.out.print(num + "\t"); 14 | num++; 15 | } else { 16 | System.out.print("\t"); 17 | } 18 | } 19 | System.out.println(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern12.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern12 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int a = 0, b = 1; 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < n; j++) { 11 | 12 | if (i >= j) { 13 | System.out.print(a + "\t"); 14 | int c = a + b; 15 | a = b; 16 | b = c; 17 | } 18 | } 19 | System.out.println(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Codeforces/Fedor_and_New_Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int bitdiff(int x,int y,int n){ 5 | int ct=0; 6 | int i=0; 7 | while(i<=n){ 8 | int a=x&1; 9 | int b=y&1; 10 | if(a!=b) ct++; 11 | x=x>>1; 12 | y=y>>1; 13 | i++; 14 | } 15 | return ct; 16 | } 17 | int main(){ 18 | int ct=0; 19 | int n,m,k; 20 | cin>>n>>m>>k; 21 | int a[m+1]; 22 | for(int i=0;i>a[i]; 24 | } 25 | for(int i=0;i kthSmallestPrimeFraction(vector &arr, int k) 5 | { 6 | vector> a; 7 | for (int i = 0; i < arr.size(); i++) 8 | { 9 | for (int j = i + 1; j < arr.size(); j++) 10 | { 11 | a.push_back(make_pair(arr[i], arr[j])); 12 | } 13 | } 14 | sort(a.begin(), a.end(), [=](pair a, pair b) 15 | { return (double)(((double)a.first / (a.second))) < (double)(((double)b.first / (b.second))); }); 16 | return {a[k - 1].first, a[k - 1].second}; 17 | }; 18 | }; -------------------------------------------------------------------------------- /229. Majority Element II.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector majorityElement(vector &nums) 5 | { 6 | int n = nums.size(); 7 | int t = n / 3; 8 | if (n == 1) 9 | return nums; 10 | vector v; 11 | sort(nums.begin(), nums.end()); 12 | int i = 0; 13 | while (i < n) 14 | { 15 | int c = 1; 16 | while (i < n - 1 && nums[i] == nums[i + 1]) 17 | { 18 | c++; 19 | i++; 20 | } 21 | if (c > t) 22 | v.push_back(nums[i]); 23 | i++; 24 | } 25 | return v; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern4 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int star = n, space = 0; 9 | for (int i = 1; i <= n; i++) { 10 | 11 | for (int j = 1; j <= space; j++) { 12 | System.out.print("\t"); 13 | } 14 | 15 | for (int j = 1; j <= star; j++) { 16 | System.out.print("*\t"); 17 | } 18 | 19 | space++; 20 | star--; 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Codeforces/1530B Putting Plates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin >> t; 7 | 8 | while (t--) 9 | { 10 | int h, w; 11 | cin >> h >> w; 12 | vector> table(h, vector(w)); 13 | for (int i = 0; i < w; i++) 14 | { 15 | if (i % 2 == 0) 16 | table[0][i] = table[h - 1][i] = 1; 17 | } 18 | for (int i = 2; i < h - 2; i++) 19 | { 20 | if (i % 2 == 0) 21 | table[i][0] = 1; 22 | if (i % 2 == 0) 23 | table[i][w - 1] = 1; 24 | } 25 | for (int i = 0; i < h; i++) 26 | { 27 | for (int j = 0; j < w; j++) 28 | cout << table[i][j]; 29 | cout << endl; 30 | } 31 | cout << endl; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lcs(string X, string Y, int m, int n, vector >& dp) 5 | { 6 | if (m == 0 || n == 0) 7 | return 0; 8 | if (X[m - 1] == Y[n - 1]) 9 | return dp[m][n] = 1 + lcs(X, Y, m - 1, n - 1, dp); 10 | 11 | if (dp[m][n] != -1) { 12 | return dp[m][n]; 13 | } 14 | return dp[m][n] = max(lcs(X, Y, m, n - 1, dp), 15 | lcs(X, Y, m - 1, n, dp)); 16 | } 17 | 18 | int main() 19 | { 20 | string X, Y; 21 | cin>>X>>Y; 22 | int m = X.length(); 23 | int n = Y.length(); 24 | vector > dp(m + 1, vector(n + 1, -1)); 25 | cout << "Length of LCS is " << lcs(X, Y, m, n, dp); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Codeforces/Xenia_and_Ringroad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | long long int n,k; 7 | cin>>n>>k; 8 | long long int a[k]; 9 | long long int c[k]; 10 | for(long long int i=0;i>a[i]; 12 | } 13 | long long int sum=0; 14 | for(long long int i=0;i& hc, vector& vc) { 4 | hc.push_back(0);hc.push_back(h); 5 | vc.push_back(0);vc.push_back(w); 6 | sort(hc.begin(),hc.end()); 7 | sort(vc.begin(),vc.end()); 8 | long long mx_w=0,mx_h=0; 9 | for(int i=0;i bool: 3 | num, exp, sign, dec = False, False, False, False 4 | for c in S: 5 | if c >= '0' and c <= '9': num = True 6 | elif c == 'e' or c == 'E': 7 | if exp or not num: return False 8 | else: exp, num, sign, dec = True, False, False, False 9 | elif c == '+' or c == '-': 10 | if sign or num or dec: return False 11 | else: sign = True 12 | elif c == '.': 13 | if dec or exp: return False 14 | else: dec = True 15 | else: return False 16 | return num 17 | 18 | -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/TwetlthPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int size=6; 6 | for(int i=1;i<=size;i++) 7 | { 8 | 9 | if(i==size) 10 | { 11 | for(int j=0;j 2 | using namespace std; 3 | 4 | #define ll long long 5 | ll MAX = 1e9+7; 6 | int main(){ 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | ll t; 10 | cin>>t; 11 | while(t--){ 12 | ll x; 13 | cin>>x; 14 | ll power=log2(x); 15 | 16 | ll ans = pow(2,power%MAX); 17 | ll ans2=pow(2,power+1%MAX); 18 | if(x==0){ 19 | cout<<1<left); 19 | int t=maxDepth(root->right); 20 | 21 | return 1 + max(c,t); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /CodeChef/NAICHEF Naive Chef.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int t = sc.nextInt(), n; 8 | int[] arr, ab = new int[2]; 9 | double numA, numB; 10 | 11 | for(int i = 0; i < t; i++) { 12 | n = sc.nextInt(); 13 | arr = new int[n]; 14 | ab[0] = sc.nextInt(); 15 | ab[1] = sc.nextInt(); 16 | numA = numB = 0; 17 | 18 | for(int j = 0; j < n; j++) { 19 | arr[j] = sc.nextInt(); 20 | if(arr[j] == ab[0]) numA++; 21 | if(arr[j] == ab[1]) numB++; 22 | } 23 | 24 | double res = (double) ((numA/n) * (numB/n)); 25 | System.out.println(res); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/elventhPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int size=5; 6 | for(int i=0;i 2 | using namespace std ; 3 | 4 | #define ff first 5 | #define ss second 6 | #define ull unsigned long long 7 | #define mod 1000000007 8 | #define inf 1e18 9 | #define w(x) int x;cin>>x;while(x--) 10 | #define f(x,y) for( x=0;x>e>>k; 16 | //int c=log(e)/log(k); 17 | int counter=1 ; 18 | float c = e/k; 19 | while(floor(c)>=1){ 20 | counter++; 21 | c=c/k; 22 | } 23 | //cout<& A, vector& cost) { 2 | long long l = 1, r = 1000000, res = f(A, cost, 1), x; 3 | while (l < r) { 4 | x = (l + r) / 2; 5 | long long y1 = f(A, cost, x), y2 = f(A, cost, x + 1); 6 | res = min(y1, y2); 7 | if (y1 < y2) 8 | r = x; 9 | else 10 | l = x + 1; 11 | } 12 | return res; 13 | } 14 | 15 | long long f(vector& A, vector& cost, int x) { 16 | long long res = 0; 17 | for (int i = 0; i < A.size(); ++i) 18 | res += 1L * abs(A[i] - x) * cost[i]; 19 | return res; 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/75. Sort colors.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int n= nums.size(); 5 | int low=0; 6 | int mid=0; 7 | int high=n-1; 8 | 9 | while(mid <= high) 10 | { 11 | if(nums[mid] == 0) 12 | { 13 | swap(nums[mid], nums[low]); 14 | low++; 15 | mid++; 16 | } 17 | else if(nums[mid] == 1) 18 | { 19 | mid++; 20 | } 21 | else { 22 | swap(nums[mid], nums[high]); 23 | high--; 24 | } 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/2D-arrays/array2d_demo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class array2d_demo { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | int m = sc.nextInt(); 8 | 9 | int[][] arr = new int[n][m]; 10 | 11 | for (int i = 0; i < n; i++) { 12 | for (int j = 0; j < m; j++) { 13 | arr[i][j] = sc.nextInt(); 14 | } 15 | } 16 | 17 | for (int i = 0; i < n; i++) { 18 | for (int j = 0; j < m; j++) { 19 | System.out.print(arr[i][j] + " "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern_m.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern_m { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 1; j <= n; j++) { 10 | if (j == 1 || j == n) { 11 | System.out.print("*\t"); 12 | } else if (i <= n / 2 + 1 && (i == j || i + j == n + 1)) { 13 | System.out.print("*\t"); 14 | } else { 15 | System.out.print("\t"); 16 | } 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /LeetCode/Java/KthSmallestElementBST.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | class Solution { 3 | public int kthSmallest(TreeNode root, int k) { 4 | if(root==null) 5 | return Integer.MAX_VALUE; 6 | Stack s=new Stack<>(); 7 | TreeNode n; 8 | n=root; 9 | while(n!=null||!s.isEmpty()) 10 | { 11 | if(n!=null) 12 | { 13 | s.push(n); 14 | n=n.left; 15 | } 16 | else 17 | { 18 | n=s.pop(); 19 | k--; 20 | if(k==0) 21 | return n.val; 22 | n=n.right; 23 | } 24 | } 25 | return Integer.MAX_VALUE; 26 | } 27 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/digitFrequency1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class digitFrequency1 { 4 | 5 | public static int getDigitFrequency(int n, int d) { 6 | // write code here 7 | int count = 0; 8 | while (n != 0) { 9 | int digit = n % 10; 10 | if (digit == d) { 11 | count++; 12 | } 13 | n /= 10; 14 | } 15 | 16 | return count; 17 | } 18 | 19 | public static void main(String[] args) { 20 | Scanner sc = new Scanner(System.in); 21 | int n = sc.nextInt(); 22 | int d = sc.nextInt(); 23 | int f = getDigitFrequency(n, d); 24 | System.out.println(f); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Getting Started/eachDigit.java: -------------------------------------------------------------------------------- 1 | // Problem : Digits of a number 2 | 3 | import java.util.*; 4 | 5 | public class eachDigit { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int num = sc.nextInt(); 10 | 11 | // prints each individual digit in reverse order 12 | // while (num != 0) { 13 | // int r = num % 10; 14 | // System.out.println(r); 15 | // num /= 10; 16 | // } 17 | 18 | // prints each individual digit in correct order 19 | String str = Integer.toString(num); 20 | for (int i = 0; i < str.length(); i++) { 21 | System.out.println(str.charAt(i)); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern20.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern20 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | for (int i = 1; i <= num; i++) { 9 | for (int j = 1; j <= num; j++) { 10 | 11 | if (j == 1 || j == num) { 12 | System.out.print("*\t"); 13 | } else if ((i >= num / 2 + 1) && (i == j || (i + j) == (num + 1))) { 14 | System.out.print("*\t"); 15 | } else { 16 | System.out.print("\t"); 17 | } 18 | 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/2432. The Employee That Worked on the Longest Task: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | static bool compare(vector a, vector b){ 4 | return a[1]>& logs) { 8 | sort(logs.begin(), logs.end(), compare); 9 | 10 | int t = 0; 11 | int ans = -1, mx = -1; 12 | for(int i=0 ;imx){ 18 | mx = w; 19 | ans = logs[i][0]; 20 | } 21 | t = logs[i][1]; 22 | } 23 | 24 | 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/583. Delete Operation for Two Strings: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | int m=word1.size(),n=word2.size(); 7 | int lk[m+1][n+1]; 8 | memset(lk,0,sizeof(lk)); 9 | for(int i=1;i<=m;i++) 10 | { 11 | for(int j=1;j<=n;j++) 12 | { 13 | if(word1[i-1]==word2[j-1]) 14 | { 15 | lk[i][j]=lk[i-1][j-1]+1; 16 | } 17 | else 18 | { 19 | lk[i][j]=max(lk[i-1][j],lk[i][j-1]); 20 | } 21 | } 22 | } 23 | return m-lk[m][n]+n-lk[m][n]; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /greatestCommonDivisor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int gcd_naive(int a, int b) { 5 | int current_gcd = 1; 6 | for (int d = 2; d <= a && d <= b; d++) { 7 | if (a % d == 0 && b % d == 0) { 8 | if (d > current_gcd) { 9 | current_gcd = d; 10 | } 11 | } 12 | } 13 | return current_gcd; 14 | } 15 | 16 | int euclid_gcd(int a, int b){ 17 | if(b==0){ 18 | return a; 19 | } 20 | else{ 21 | return euclid_gcd(b,a%b); 22 | } 23 | } 24 | 25 | void test_solution() { 26 | assert(euclid_gcd(30,21) == 3); 27 | assert(euclid_gcd(1035,759) == 69); 28 | } 29 | 30 | int main() { 31 | int a, b; 32 | std::cin >> a >> b; 33 | std::cout << euclid_gcd(a, b) << std::endl; 34 | //test_solution(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /LeetCode/2441. Largest Positive Integer That Exists With Its Negative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int findMaxK(vector& nums) { 6 | // keep track of maximum 7 | int maxi = INT_MIN; 8 | // set to remeber the other value 9 | unordered_set s; 10 | for(auto i : nums){ 11 | if(s.find(i * -1) == s.end()){ 12 | // if not found insert 13 | s.insert(i); 14 | }else{ 15 | // if found update maxi 16 | maxi = max(maxi, abs(i)); 17 | } 18 | } 19 | // if not present return -1 else return maxi 20 | return maxi == INT_MIN ? -1 : maxi; 21 | } 22 | }; 23 | // time complexiy O(n) 24 | // space complexity O(n) 25 | -------------------------------------------------------------------------------- /largest-sum-contiguous.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to print largest contiguous array sum 2 | #include 3 | using namespace std; 4 | 5 | int maxSubArraySum(int a[], int size) 6 | { 7 | int max_so_far = INT_MIN, max_ending_here = 0; 8 | 9 | for (int i = 0; i < size; i++) { 10 | max_ending_here = max_ending_here + a[i]; 11 | if (max_so_far < max_ending_here) 12 | max_so_far = max_ending_here; 13 | 14 | if (max_ending_here < 0) 15 | max_ending_here = 0; 16 | } 17 | return max_so_far; 18 | } 19 | 20 | // Driver Code 21 | int main() 22 | { 23 | int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; 24 | int n = sizeof(a) / sizeof(a[0]); 25 | 26 | // Function Call 27 | int max_sum = maxSubArraySum(a, n); 28 | cout << "Maximum contiguous sum is " << max_sum; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /LeetCode/Java/HouseRobber2.java: -------------------------------------------------------------------------------- 1 | class HouseRobber2 { 2 | public int rob(int[] nums) { 3 | if(nums.length==1) 4 | return nums[0]; 5 | int n=nums.length; 6 | int copy[]=new int[n-1]; 7 | for(int i=0;i=0;i--) 23 | { 24 | max[i]=Math.max(max[i+1],max[i+2]+nums[i]); 25 | } 26 | return max[0]; 27 | } 28 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/decimal_to_any_base.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class decimal_to_any_base { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | int n = scn.nextInt(); 8 | int b = scn.nextInt(); 9 | int dn = getValueInBase(n, b); 10 | System.out.println(dn); 11 | } 12 | 13 | public static int getValueInBase(int n, int b) { 14 | // write code here 15 | int num = 0, count = 0; 16 | while (n != 0) { 17 | int r = n % b; 18 | int ad_val = r * (int) (Math.pow(10, count)); 19 | num = ad_val + num; 20 | count++; 21 | n = n / b; 22 | } 23 | return num; 24 | } 25 | } -------------------------------------------------------------------------------- /581 Shortest Unsorted Continuous Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findUnsortedSubarray(int[] nums) { 3 | int left = 0, right = -1; 4 | 5 | /// Find the rightmost number which is not in the correct potsition 6 | int max = nums[0]; 7 | for (int i = 1; i < nums.length; i++) { 8 | if(nums[i] < max) right = i; 9 | max = Math.max(max, nums[i]); 10 | } 11 | 12 | /// Find the leftmost number which is not in the correct potsition 13 | int min = nums[nums.length - 1]; 14 | for (int j = nums.length - 2; j >= 0; j--) { 15 | if(nums[j] > min) left = j; 16 | min = Math.min(min, nums[j]); 17 | } 18 | 19 | return right - left + 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CodeChef/ARRCONS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define int long long 3 | using namespace std; 4 | int n, m, s; 5 | long long power(long long b, long long n) 6 | { 7 | long long s = 1; 8 | while (n) 9 | { 10 | if (n % 2 == 1) 11 | s = s * b % 998244353; 12 | b = b * b % 998244353; 13 | n /= 2; 14 | } 15 | return s; 16 | } 17 | long long count(int i, int j) 18 | { 19 | return ((i / (1 << (j + 1))) << j) + (max(0ll, i % (1 << (j + 1)) - (1 << j) + 1)); 20 | } 21 | void solve() 22 | { 23 | cin >> n >> m; 24 | s = 0; 25 | for (int i = 0; i < 31; ++i) 26 | s = (s + (power(count(m, i), n) << i) % 998244353) % 998244353; 27 | cout << s << endl; 28 | } 29 | signed main() 30 | { 31 | int T; 32 | cin >> T; 33 | while (T--) 34 | solve(); 35 | } 36 | -------------------------------------------------------------------------------- /LeetCode/2400. Number of Ways to Reach a Position After Exactly k Steps: -------------------------------------------------------------------------------- 1 | #define mod 1000000007 2 | class Solution { 3 | vector> dp; 4 | int recur(int startPos, int endPos, int k){ 5 | if(k==0) return (startPos==endPos); 6 | if(dp[startPos+1000][k]!=-1) return dp[startPos+1000][k]; 7 | return dp[startPos+1000][k]=((1ll*recur(startPos+1, endPos, k-1)+recur(startPos-1, endPos, k-1))%mod); 8 | } 9 | public: 10 | int numberOfWays(int startPos, int endPos, int k) { 11 | dp.resize(5000, vector(1001, -1)); 12 | return recur(startPos, endPos, k); 13 | // vector> dp(k+1, vector(2*k+2,0)); 14 | // dp[endPos][] 15 | // for(int i=1;i<=k;i++){ 16 | // for(int j=0;j<) 17 | // } 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/findElementInArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class findElementInArray { 5 | 6 | public static void main(String[] args) throws Exception { 7 | Scanner sc = new Scanner(System.in); 8 | int n = sc.nextInt(); 9 | 10 | int[] arr = new int[n]; 11 | 12 | for (int i = 0; i < arr.length; i++) { 13 | arr[i] = sc.nextInt(); 14 | } 15 | 16 | int key = sc.nextInt(); 17 | 18 | int flag = 1; 19 | for (int i = 0; i < arr.length; i++) { 20 | if (arr[i] == key) { 21 | flag = i; 22 | break; 23 | } 24 | flag = -1; 25 | } 26 | 27 | System.out.print(flag); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /LeetCode/424. Longest Repeating Character Replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(string s,int k,char ch){ 4 | int i=0; 5 | int j=0; 6 | int count=0; 7 | int n=s.length(); 8 | 9 | while(i> dp(s1 + 1, vector(s2 + 1, 0)); 8 | for (int i = 1; i < s1 + 1; i++) 9 | dp[i][0] = i; 10 | for (int i = 1; i < s2 + 1; i++) 11 | dp[0][i] = i; 12 | 13 | for (int i = 1; i < s1 + 1; i++) 14 | for (int j = 1; j < s2 + 1; j++) 15 | { 16 | if (word2[j - 1] == word1[i - 1]) 17 | dp[i][j] = dp[i - 1][j - 1]; 18 | else 19 | dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1; 20 | } 21 | return dp[s1][s2]; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/Java/longestSubstring_3.java: -------------------------------------------------------------------------------- 1 | class longestSubstring_3 { 2 | public int lengthOfLongestSubstring(String s) { 3 | HashMap substring_char = new HashMap(); 4 | int start_idx = 0; 5 | int end_idx = 0; 6 | int max_length = 0; 7 | 8 | while(end_idx < s.length()) { 9 | if(substring_char.containsKey(s.charAt(end_idx))) { 10 | int last_appear_idx = substring_char.get(s.charAt(end_idx)); 11 | start_idx =Math.max(last_appear_idx + 1, start_idx); 12 | } 13 | substring_char.put(s.charAt(end_idx), end_idx); 14 | max_length = Math.max(end_idx - start_idx + 1, max_length); 15 | end_idx++; 16 | } 17 | return max_length; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Java/581 Shortest Unsorted Continuous Subarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int findUnsortedSubarray(int[] nums) { 3 | int left = 0, right = -1; 4 | 5 | /// Find the rightmost number which is not in the correct potsition 6 | int max = nums[0]; 7 | for (int i = 1; i < nums.length; i++) { 8 | if(nums[i] < max) right = i; 9 | max = Math.max(max, nums[i]); 10 | } 11 | 12 | /// Find the leftmost number which is not in the correct potsition 13 | int min = nums[nums.length - 1]; 14 | for (int j = nums.length - 2; j >= 0; j--) { 15 | if(nums[j] > min) left = j; 16 | min = Math.min(min, nums[j]); 17 | } 18 | 19 | return right - left + 1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/any_base_to_decimal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class any_base_to_decimal { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | int n = scn.nextInt(); 8 | int b = scn.nextInt(); 9 | int d = getValueIndecimal(n, b); 10 | System.out.println(d); 11 | } 12 | 13 | public static int getValueIndecimal(int n, int b) { 14 | // write your code here 15 | int num = 0, count = 0; 16 | 17 | while (n != 0) { 18 | int r = n % 10; 19 | int ad_val = r * (int) Math.pow(b, count); 20 | num += ad_val; 21 | 22 | n = n / 10; 23 | count++; 24 | } 25 | 26 | return num; 27 | } 28 | } -------------------------------------------------------------------------------- /trappingWater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findWater(int heights[], int n) { 5 | int l[n], r[n], ans = 0; 6 | l[0] = heights[0]; 7 | 8 | for (int i = 1; i < n; i++) 9 | l[i] = max(l[i-1], heights[i]); 10 | 11 | r[n - 1] = heights[n - 1]; 12 | 13 | for (int i = n - 2; i >= 0; i--) 14 | r[i] = max(r[i+1], heights[i]); 15 | 16 | for (int i = 1; i < n-1; i++) { 17 | int temp=min(l[i-1],r[i+1]); 18 | if(temp > heights[i]) { 19 | ans += temp - heights[i]; 20 | } 21 | } 22 | return ans; 23 | } 24 | 25 | int main() { 26 | int n; 27 | cin >> n; 28 | int heights[n] = {0}; 29 | for(int i = 0; i < n; i++) 30 | cin >> heights[i]; 31 | int ans = findWater(heights, n); 32 | cout << ans << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern13.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern13 { 4 | 5 | public static int fact(int n) { 6 | int f = 1; 7 | for (int i = 1; i <= n; i++) { 8 | f *= i; 9 | } 10 | return f; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | int num = sc.nextInt(); 16 | 17 | for (int i = 0; i < num; i++) { 18 | for (int j = 0; j <= i; j++) { 19 | int n = fact(i); 20 | int r = fact(j); 21 | int q = fact(i - j); 22 | int nCr = n / (r * q); 23 | System.out.print(nCr + "\t"); 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/eightPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=1;i<=5;i++) 6 | { 7 | for(int j=1;j<=5-i;j++) 8 | { 9 | cout<<" "; 10 | } 11 | 12 | for(int j=1;j<=(2*i-1);j++) 13 | { 14 | cout<<"*"; 15 | } 16 | 17 | 18 | cout<<"\n"; 19 | 20 | } 21 | 22 | for(int i=5;i>=1;i--) 23 | { 24 | for(int j=5-i;j>0;j--) 25 | { 26 | cout<<" "; 27 | } 28 | 29 | for(int j=(2*i-1);j>=1;j--) 30 | { 31 | cout<<"*"; 32 | } 33 | 34 | 35 | cout<<"\n"; 36 | 37 | } 38 | 39 | return 0; 40 | } 41 | 42 | /* 43 | 44 | * 45 | *** 46 | ***** 47 | ******* 48 | ********* 49 | ********* 50 | ******* 51 | ***** 52 | *** 53 | * 54 | */ -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/subArrayProblem.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class subArrayProblem { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | 12 | int arr[] = new int[n]; 13 | 14 | for (int i = 0; i < n; i++) { 15 | arr[i] = sc.nextInt(); 16 | } 17 | 18 | for (int i = 0; i < arr.length; i++) { 19 | for (int j = i; j < arr.length; j++) { 20 | for (int k = i; k <= j; k++) { 21 | System.out.print(arr[k] + "\t"); 22 | } 23 | System.out.println(); 24 | } 25 | } 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern5 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int star = 1, space = n / 2; 9 | for (int i = 1; i <= n; i++) { 10 | 11 | for (int j = 1; j <= space; j++) { 12 | System.out.print("\t"); 13 | } 14 | 15 | for (int j = 1; j <= star; j++) { 16 | System.out.print("*\t"); 17 | } 18 | 19 | if (i <= n / 2) { 20 | star += 2; 21 | space--; 22 | } else { 23 | space++; 24 | star -= 2; 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/spanOfArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class spanOfArray { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | 11 | int arr[] = new int[n]; 12 | 13 | for (int i = 0; i < arr.length; i++) { 14 | arr[i] = sc.nextInt(); 15 | } 16 | 17 | int max = arr[0]; 18 | int min = arr[0]; 19 | for (int i = 0; i < arr.length; i++) { 20 | if (arr[i] > max) 21 | max = arr[i]; 22 | 23 | if (arr[i] < min) { 24 | min = arr[i]; 25 | } 26 | } 27 | 28 | System.out.print(max - min); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /LeetCode/10 Regular Expression Matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector>dp; 4 | bool isMatch(string s, string p) { 5 | int n = s.length(),m=p.length(); 6 | vector>dp(n+1,vector(m+1,false)); 7 | dp[0][0] = true; 8 | for(int j=1;j<=m;j++){ 9 | if(p[j-1]=='*')dp[0][j] = dp[0][j-2]; 10 | } 11 | for(int i=1;i<=n;i++){ 12 | for(int j=1;j<=m;j++){ 13 | if(p[j-1]=='*'){ 14 | dp[i][j] = ((s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j]); 15 | dp[i][j] = (dp[i][j] || dp[i][j-2]); 16 | }else if((s[i-1]==p[j-1]) || p[j-1]=='.'){ 17 | dp[i][j] = dp[i-1][j-1]; 18 | } 19 | } 20 | } 21 | return dp[n][m]; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/31. Next Permutation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | // Optimal Solution Time O(N) & Auxiliary Space O(1) 5 | 6 | int len=nums.size(), k=0, l=0; // For example, if nums=[2,4,6,5,3] 7 | for(k=len-2;k>=0;k--){ 8 | if(nums[k]k;l--){ 16 | if(nums[l]>nums[k]) 17 | break; // l will point to 5 in nums=[2,4,6,5,3] 18 | } 19 | swap(nums[k],nums[l]); // nums=[2,5,6,4,3] 20 | reverse(nums.begin()+k+1,nums.end()); // nums=[2,5,3,4,6] 21 | } 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/712. Minimum ASCII Delete Sum for Two Strings: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumDeleteSum(string s1, string s2) { 4 | ios_base::sync_with_stdio(false); 5 | cin.tie(NULL); 6 | 7 | int sum=0; 8 | for(char i:s1) sum+=i; 9 | for(char i:s2) sum+=i; 10 | 11 | int m=s1.size(),n=s2.size(); 12 | int lk[m+1][n+1]; 13 | memset(lk,0,sizeof(lk)); 14 | 15 | for(int i=1;i<=m;i++) 16 | { 17 | for(int j=1;j<=n;j++) 18 | { 19 | if(s1[i-1]==s2[j-1]) 20 | { 21 | lk[i][j]=lk[i-1][j-1]+(int)(s1[i-1]); 22 | } 23 | else lk[i][j]=max(lk[i-1][j],lk[i][j-1]); 24 | } 25 | } 26 | return sum-2*lk[m][n]; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/39 Combination Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void helper(vector>&res, vector&temp,vector& candidates,int sum,int target,int index){ 4 | if(sum > target){ 5 | return; 6 | } 7 | if(sum == target){ 8 | res.push_back(temp); 9 | return; 10 | } 11 | int size=candidates.size(); 12 | for(int i=index; i < size;++i ){ 13 | temp.push_back(candidates[i]); 14 | helper(res,temp,candidates,sum+candidates[i],target,i); 15 | temp.pop_back(); 16 | } 17 | 18 | } 19 | vector> combinationSum(vector& candidates, int target) { 20 | vector> res; 21 | vector temp; 22 | helper(res,temp,candidates,0,target,0); 23 | return res; 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/473. Matchsticks to Square: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int tar; 4 | bool solve(int i,int n,vector&res,vector&ms){ 5 | if(i>=n){ 6 | return (res[0]==res[1]&&res[1]==res[2]&&res[2]==res[3]&&res[3]== tar); 7 | } 8 | for(int j=0;j<4;j++){ 9 | if(res[j]+ms[i] > tar) 10 | continue; 11 | res[j]+=ms[i]; 12 | if(solve(i+1,n,res,ms)) 13 | return true; 14 | res[j]-=ms[i]; 15 | } 16 | return false; 17 | } 18 | bool makesquare(vector& ms) { 19 | int n=ms.size(); 20 | int sum =0; 21 | for(auto it : ms) 22 | sum+=it; 23 | if(sum % 4 != 0) 24 | return false; 25 | tar=sum/4; 26 | sort(ms.rbegin(), ms.rend()); 27 | // for(auto it : ms) 28 | // cout<res(4,0); 30 | return solve(0,n,res,ms); 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Codeforces/158B_Taxi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define f(i,n,p) for(int i=0;i>n; 9 | int a[n]; 10 | f(i,n,p) 11 | {cin>>a[i]; 12 | } 13 | f(i,n,p) 14 | { 15 | if(a[i]==1) 16 | {count1++;} 17 | else if (a[i]==2) 18 | {count2++;} 19 | else if (a[i]==3) 20 | {count3++;} 21 | else if (a[i]==4) 22 | {count4++;} 23 | }int total = count4 + count3 + count2/ 2; 24 | count1 -= count3; 25 | if (count2 % 2 == 1) 26 | { 27 | total += 1; 28 | count1 -= 2; 29 | } 30 | if (count1 > 0) 31 | { 32 | total += (count1 + 3) / 4; 33 | }cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | char get(int i) 10 | { 11 | return 'a' + i - 1; 12 | } 13 | 14 | void solve() 15 | { 16 | int n; 17 | string s; 18 | cin >> n >> s; 19 | int i = n - 1; 20 | string res; 21 | while (i >= 0) 22 | { 23 | if (s[i] != '0') 24 | { 25 | res += get(s[i] - '0'); 26 | i--; 27 | } 28 | else 29 | { 30 | res += get(stoi(s.substr(i - 2, 2))); 31 | i -= 3; 32 | } 33 | } 34 | reverse(res.begin(), res.end()); 35 | cout << res << '\n'; 36 | } 37 | 38 | int main() 39 | { 40 | int t = 1; 41 | cin >> t; 42 | for (int it = 0; it < t; ++it) 43 | { 44 | solve(); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /LeetCode/200 Number of Islands.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int vis[301][301]; 4 | void dfs(vector>& grid,int i,int j,int n,int m) 5 | { 6 | if(i<0 or i>=m or j<0 or j>=n or vis[i][j]==1 or grid[i][j]=='0') 7 | return ; 8 | vis[i][j]=1; 9 | dfs(grid,i+1,j,n,m); 10 | dfs(grid,i-1,j,n,m); 11 | dfs(grid,i,j-1,n,m); 12 | dfs(grid,i,j+1,n,m); 13 | } 14 | int numIslands(vector>& grid) { 15 | int m=grid.size(); 16 | int n=grid[0].size(); 17 | int ans=0; 18 | memset(vis,-1,sizeof(vis)); 19 | for(int i=0;i > Solution::levelOrder(TreeNode* A) { 11 | vector> ans; 12 | if(A==NULL) 13 | return ans; 14 | queue q; 15 | q.push(A); 16 | while(!q.empty()) 17 | { 18 | int n=q.size(); 19 | vector level; 20 | for(int i=0;ileft) 25 | q.push(curr->left); 26 | if(curr->right) 27 | q.push(curr->right); 28 | level.push_back(curr->val); 29 | } 30 | ans.push_back(level); 31 | } 32 | return ans; 33 | } 34 | -------------------------------------------------------------------------------- /LeetCode/42 Trapping Rain Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | int n=height.size(); 5 | if(n==1 or n==0) 6 | { 7 | return 0; 8 | } 9 | int mxl[n],mxr[n]; 10 | mxl[0]=height[0];mxr[n-1]=height[n-1]; 11 | int maxil=height[0]; 12 | for(int i=1;i=0;i--) 19 | { 20 | maxir=max(maxir,height[i]); 21 | mxr[i]=maxir; 22 | } 23 | int water[n]; 24 | for(int i=0;i"+ans); 13 | count++; 14 | return; 15 | } 16 | 17 | for(int i=0;i=denom[i]){ 20 | coinchange(denom,amount-denom[i],ans+denom[i]); 21 | } 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | // TODO code application logic here 27 | int denom[]={2,3,5,6}; 28 | 29 | coinchange(denom,10,""); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /LeetCode/1664. Ways to Make a Fair Array: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int waysToMakeFair(vector& nums) { 4 | 5 | ios_base::sync_with_stdio(false); 6 | cin.tie(NULL); 7 | 8 | int n=nums.size(),ans=0; 9 | int before[n],after[n]; 10 | 11 | memset(before,0,sizeof(before)); 12 | memset(after,0,sizeof(after)); 13 | 14 | for(int i=0;i1?before[i-2]:0)+nums[i]; 17 | } 18 | 19 | for(int i=n-1;i>=0;i--) 20 | { 21 | after[i]=(i0) 13 | return dp[l1][l2]; 14 | if(s1.charAt(l1)==s2.charAt(l2)) 15 | return dp[l1][l2]=find(s1,s2,l1-1,l2-1,dp); 16 | else 17 | { 18 | int x,y,z; 19 | x=find(s1,s2,l1,l2-1,dp);//insert 20 | y=find(s1,s2,l1-1,l2,dp);//delete 21 | z=find(s1,s2,l1-1,l2-1,dp);//replace 22 | dp[l1][l2]=1+Math.min(x,Math.min(y,z)); 23 | } 24 | return dp[l1][l2]; 25 | } 26 | } -------------------------------------------------------------------------------- /LeetCode/56. Merge Intervals.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | vector> v; 5 | sort(intervals.begin(), intervals.end()); 6 | 7 | vector ex=intervals[0]; 8 | int afirst=intervals[0][0]; 9 | int asecond=intervals[0][1]; 10 | 11 | for(int i=1; iasecond){ 14 | asecond=intervals[i][1]; 15 | } 16 | ex={afirst, asecond}; 17 | }else{ 18 | v.push_back(ex); 19 | afirst=intervals[i][0]; 20 | asecond=intervals[i][1]; 21 | ex=intervals[i]; 22 | } 23 | } 24 | v.push_back(ex); 25 | return v; 26 | } 27 | }; -------------------------------------------------------------------------------- /LeetCode/60. Permutation Sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void setPerm(vector& v,string& ans,int n,int k,vector& factVal){ 4 | if(n==1){ 5 | ans+=to_string(v.back()); 6 | return; 7 | } 8 | 9 | int index = (k/factVal[n-1]); 10 | if(k % factVal[n-1] == 0){ 11 | index--; 12 | } 13 | 14 | ans+= to_string(v[index]); 15 | v.erase(v.begin() + index); 16 | k -= factVal[n-1] * index; 17 | setPerm(v,ans,n-1,k,factVal); 18 | } 19 | 20 | string getPermutation(int n, int k) { 21 | if(n==1) return "1"; 22 | vectorfactVal = {1,1,2,6,24,120,720,5040,40320,362880}; 23 | string ans = ""; 24 | vector v; 25 | for(int i=1;i<=n;i++) 26 | v.emplace_back(i); 27 | setPerm(v,ans,n,k,factVal); 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /LeetCode/19 Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution 12 | { 13 | public: 14 | ListNode *removeNthFromEnd(ListNode *head, int n) 15 | { 16 | ListNode *temp = head; 17 | int cc = 0; 18 | while (temp) 19 | temp = temp->next, cc++; 20 | if (cc < n) 21 | return head; 22 | if (cc == n) 23 | return head->next; 24 | temp = head; 25 | for (int i = 1; i < cc - n; ++i) 26 | { 27 | temp = temp->next; 28 | } 29 | temp->next = temp->next->next; 30 | return head; 31 | } 32 | }; -------------------------------------------------------------------------------- /Pepcoding/trial.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class trial { 4 | 5 | public static void main(String[] args) { 6 | 7 | int nums[] = new int[] { -100, -98, -1, 2, 3, 4 }; 8 | 9 | for (int i = 0; i < nums.length; i++) { 10 | System.out.print(nums[i] + "\t"); 11 | } 12 | System.out.println(); 13 | 14 | Arrays.sort(nums); 15 | 16 | for (int i = 0; i < nums.length; i++) { 17 | System.out.print(nums[i] + "\t"); 18 | } 19 | System.out.println(); 20 | 21 | int len = nums.length; 22 | 23 | int fProd = -1; 24 | if (nums[0] < 0 && nums[1] < 0) { 25 | fProd = nums[0] * nums[1] * nums[len - 1]; 26 | } 27 | 28 | int lProd = nums[len - 1] * nums[len - 2] * nums[len - 3]; 29 | 30 | int maxProd = Math.max(fProd, lProd); 31 | 32 | System.out.println(maxProd); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /LeetCode/1383. Maximum Performance of a Team.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPerformance(int n, vector& speed, vector& efficiency, int k) { 4 | long long int ans=0,i=0,sum=0,mod=1000000007,z; 5 | priority_queue< long long int, vector< long long int>, greater< long long int> > pq; 6 | vector>vp; 7 | for(i=0;ik){ 16 | sum-=pq.top(); 17 | pq.pop(); 18 | } 19 | z=vp[i].first*sum; 20 | // if(pq.size()==k) 21 | ans=max(ans,z); 22 | } 23 | return ans % (int)(1e9+7); 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/Largest plus Sign Brute force.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int orderOfLargestPlusSign(int N, int[][] mines) { 4 | 5 | Set banned = new HashSet(); 6 | 7 | for (int[] mine: mines) 8 | 9 | banned.add(mine[0] * N + mine[1]); 10 | 11 | 12 | 13 | int ans = 0; 14 | 15 | for (int r = 0; r < N; ++r) for (int c = 0; c < N; ++c) { 16 | 17 | int k = 0; 18 | 19 | while (k <= r && r < N-k && k <= c && c < N-k && 20 | 21 | !banned.contains((r-k)*N + c) && 22 | 23 | !banned.contains((r+k)*N + c) && 24 | 25 | !banned.contains(r*N + c-k) && 26 | 27 | !banned.contains(r*N + c+k)) 28 | 29 | k++; 30 | 31 | 32 | 33 | ans = Math.max(ans, k); 34 | 35 | } 36 | 37 | return ans; 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /LeetCode/653 Two SUM IV - Input is a BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool findTarget(TreeNode* root, int k) { 4 | stack stk1, stk2; 5 | TreeNode *cur1 = root, *cur2 = root; 6 | int lo = 0, hi = 0; 7 | while (cur1) stk1.emplace(cur1), lo = cur1->val, cur1 = cur1->left; 8 | while (cur2) stk2.emplace(cur2), hi = cur2->val, cur2 = cur2->right; 9 | while (lo < hi) { 10 | int sum = lo + hi; 11 | if (sum < k) { // move cur1, increase lo 12 | while(cur1) stk1.emplace(cur1), lo = cur1->val, cur1 = cur1->left; 13 | lo = stk1.top()->val, cur1 = stk1.top()->right, stk1.pop(); 14 | } else if (sum > k) { // move cur2, decrease hi 15 | while(cur2) stk2.emplace(cur2), hi = cur2->val, cur2 = cur2->right; 16 | hi = stk2.top()->val, cur2= stk2.top()->left, stk2.pop(); 17 | } else { return true; } 18 | } 19 | return false; 20 | } 21 | }; -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Getting Started/BenjaminBulbs.java: -------------------------------------------------------------------------------- 1 | // 1. You are given n number of bulbs. They are all switched off. 2 | // A weird fluctuation in voltage hits the circuit n times. 3 | // In the 1st fluctuation all bulbs are toggled, in the 2nd fluctuation every 2nd bulb is toggled, 4 | // in the 3rd fluctuation every 3rd bulb is toggled and so on. 5 | // You've to find which bulbs will be switched on after n fluctuations. 6 | // 2. Take as input a number n, representing the number of bulbs. 7 | // 3. Print all the bulbs that will be on after the nth fluctuation in voltage. 8 | 9 | import java.util.Scanner; 10 | 11 | public class BenjaminBulbs { 12 | public static void main(String[] args) { 13 | Scanner sc = new Scanner(System.in); 14 | 15 | int num = sc.nextInt(); 16 | 17 | for (int i = 1; i * i <= num; i++) { 18 | System.out.println(i * i); 19 | } 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/1870. Minimum Speed to Arrive on Time: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& dist,int mid,double hour) 4 | { 5 | double time=0; 6 | for(int i=0;i& dist, double hour) { 15 | ios_base::sync_with_stdio(false); 16 | cin.tie(NULL); 17 | 18 | int l=1,r=1e7,ans=-1; 19 | while(l<=r) 20 | { 21 | int mid=l+(r-l)/2; 22 | if(check(dist,mid,hour)) 23 | { 24 | ans=mid; 25 | r=mid-1; 26 | } 27 | else 28 | { 29 | l=mid+1; 30 | } 31 | } 32 | return ans; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /LeetCode/112 Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool solve(TreeNode *root, int targetSum) 15 | { 16 | 17 | if(root->val==targetSum && root->left==NULL && root->right==NULL) return true; 18 | 19 | bool l=hasPathSum(root->left, targetSum-root->val); 20 | bool r=hasPathSum(root->right, targetSum-root->val); 21 | 22 | return l|r; 23 | } 24 | bool hasPathSum(TreeNode* root, int targetSum) { 25 | if(root==NULL) return false; 26 | return solve(root, targetSum); 27 | } 28 | }; -------------------------------------------------------------------------------- /LeetCode/16 3Sum Closest.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | sort(nums.begin(), nums.end()); 5 | int prev = nums[0] + nums[1] + nums[2]; 6 | 7 | for(int i=0;i target) high--; 18 | else return target; 19 | 20 | if(abs(target - prev) > abs(target - curr)) 21 | prev = curr; 22 | } 23 | if(abs(target - prev) > abs(target - curr)) 24 | prev = curr; 25 | 26 | } 27 | return prev; 28 | } 29 | }; -------------------------------------------------------------------------------- /LeetCode/6 ZigZag Conversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Solution { 4 | public: 5 | string convert(string s, int numRows) { 6 | vector v[numRows]; 7 | 8 | bool pry=false; 9 | int chk=0; 10 | if(numRows==1) 11 | return s; 12 | for(int i=0;i= 0; i--) { 25 | System.out.println(arr[i][j]); 26 | } 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LeetCode/692. Top K Frequent Words.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& words, int k) { 4 | unordered_map wordCount; 5 | for (const string& word : words) { 6 | wordCount[word]++; 7 | } 8 | 9 | priority_queue, vector>, Comp> minHeap; 10 | for (const auto& [ word, count ] : wordCount) { 11 | minHeap.emplace(count, word); 12 | if (minHeap.size() > k) 13 | minHeap.pop(); 14 | } 15 | 16 | vectorres; 17 | while (minHeap.size()) { 18 | res.emplace_back(minHeap.top().second); 19 | minHeap.pop(); 20 | } 21 | 22 | reverse(res.begin(), res.end()); 23 | return res; 24 | } 25 | 26 | private: 27 | struct Comp { 28 | bool operator()(const pair& p1, const pair& p2) { 29 | return p1.first == p2.first ? p1.second < p2.second : p1.first > p2.first; 30 | } 31 | }; 32 | }; -------------------------------------------------------------------------------- /2095. Delete the Middle Node of a Linked List: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | 14 | ListNode* deleteMiddle(ListNode* head) { 15 | if(head->next==NULL) return NULL; 16 | if(head->next->next==NULL){ 17 | head->next=NULL; 18 | return head; 19 | } 20 | ListNode* dummy=new ListNode(-1); 21 | dummy->next=head; 22 | ListNode* slow=dummy; 23 | ListNode* fast=head; 24 | while(fast!=NULL and fast->next!=NULL){ 25 | slow=slow->next; 26 | fast=fast->next->next; 27 | } 28 | slow->next=slow->next->next; 29 | return head; 30 | 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /C++/Cpp_Pattrens/tenthPattren.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i=0;i<5;i++) 6 | { 7 | for(int j=5-i;j>=1;j--) 8 | { 9 | cout<<"*"; 10 | } 11 | 12 | for( int j=1;j<=i*2;j++) 13 | { 14 | cout<<" "; 15 | } 16 | 17 | for(int j=5-i;j>=1;j--) 18 | { 19 | cout<<"*"; 20 | } 21 | cout<<"\n"; 22 | } 23 | 24 | for(int i=1;i<=5;i++) 25 | { 26 | for(int j=1;j<=i;j++) 27 | { 28 | cout<<"*"; 29 | } 30 | 31 | for( int j=1;j<=10-i*2;j++) 32 | { 33 | cout<<" "; 34 | } 35 | 36 | for(int j=1;j<=i;j++) 37 | { 38 | cout<<"*"; 39 | } 40 | cout<<"\n"; 41 | } 42 | return 0; 43 | } 44 | 45 | /* 46 | 47 | ********** 48 | **** **** 49 | *** *** 50 | ** ** 51 | * * 52 | * * 53 | ** ** 54 | *** *** 55 | **** **** 56 | ********** 57 | 58 | */ -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern6.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class pattern6 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | int star = (num / 2) + 1, space = 1; 9 | 10 | for (int i = 1; i <= num; i++) { 11 | for (int j = 1; j <= star; j++) { 12 | System.out.print("*\t"); 13 | } 14 | 15 | for (int j = 1; j <= space; j++) { 16 | System.out.print("\t"); 17 | } 18 | 19 | for (int j = 1; j <= star; j++) { 20 | System.out.print("*\t"); 21 | } 22 | 23 | if (i <= num / 2) { 24 | star--; 25 | space += 2; 26 | } else { 27 | star++; 28 | space -= 2; 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LeetCode/2444. Count Subarrays With Fixed Bounds.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long countSubarrays(vector& nums, int minK, int maxK) { 4 | long res = 0; 5 | bool minFound = false, maxFound = false; 6 | int start = 0, minStart = 0, maxStart = 0; 7 | for (int i = 0; i < nums.size(); i++) { 8 | int num = nums[i]; 9 | if (num < minK || num > maxK) { 10 | minFound = false; 11 | maxFound = false; 12 | start = i+1; 13 | } 14 | if (num == minK) { 15 | minFound = true; 16 | minStart = i; 17 | } 18 | if (num == maxK) { 19 | maxFound = true; 20 | maxStart = i; 21 | } 22 | if (minFound && maxFound) { 23 | res += (min(minStart, maxStart) - start + 1); 24 | } 25 | } 26 | return res; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/pattern18.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class pattern18 { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | int star = num, osp = 0; 9 | 10 | for (int i = 1; i <= num; i++) { 11 | 12 | for (int j = 1; j <= osp; j++) { 13 | System.out.print("\t"); 14 | } 15 | 16 | for (int j = 1; j <= star; j++) { 17 | if (i > 1 && i <= num / 2 && j > 1 && j < star) 18 | System.out.print("\t"); 19 | else 20 | System.out.print("*\t"); 21 | } 22 | 23 | if (i <= num / 2) { 24 | osp++; 25 | star -= 2; 26 | } else { 27 | osp--; 28 | star += 2; 29 | } 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/barChart.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class barChart { 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | int arr[] = new int[n]; 9 | 10 | for (int i = 0; i < arr.length; i++) { 11 | arr[i] = sc.nextInt(); 12 | } 13 | 14 | int max = arr[0]; 15 | for (int i = 0; i < arr.length; i++) { 16 | if (arr[i] > max) 17 | max = arr[i]; 18 | } 19 | 20 | for (int i = max; i >= 1; i--) { 21 | for (int j = 0; j < arr.length; j++) { 22 | if (arr[j] >= i) { 23 | System.out.print("*\t"); 24 | } else { 25 | System.out.print("\t"); 26 | } 27 | } 28 | System.out.println(); 29 | } 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /LeetCode/2442. Count Number of Distinct Integers After Reverse Operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int countDistinctIntegers(vector& nums) { 6 | // unordered_set constructor to insert all elements in a vector 7 | // Note :- unordered_set only maintains unique values in it 8 | unordered_set s(nums.begin() , nums.end()); 9 | // for every num find its reverse 10 | for(auto i : nums){ 11 | int num = 0; 12 | while(i){ 13 | int rem = i % 10; 14 | num = num * 10 + rem; 15 | i /= 10; 16 | } 17 | // if the number is not found in set then it is a unique element so add into set 18 | if(s.find(num) == s.end()){ 19 | s.insert(num); 20 | } 21 | } 22 | // since every element in unordered_set is unique the size of it itself is answer 23 | return s.size(); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/150. Evaluate Reverse Polish Notation: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int opr(int a,int b,string op) 4 | { 5 | if(op=="+") return a+b; 6 | if(op=="-") return b-a; 7 | if(op=="*") return b*a; 8 | return b/a; 9 | } 10 | bool isopr(string a) 11 | { 12 | if(a=="+"||a=="-"||a=="*"||a=="/") return true; 13 | return false; 14 | } 15 | int evalRPN(vector& tokens) { 16 | stack st; 17 | int n=tokens.size(); 18 | for(int i=0;i v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 5 | vector ans; 6 | void combinations(string digits, string s, int k) 7 | { 8 | if (s.length() == digits.length()) 9 | { 10 | ans.push_back(s); 11 | return; 12 | } 13 | int number = digits[k] - '0'; 14 | for (int i = 0; i < v[number].length(); i++) 15 | { 16 | char key = v[number][i]; 17 | 18 | s.push_back(key); 19 | cout << s << endl; 20 | combinations(digits, s, k + 1); 21 | s.pop_back(); 22 | } 23 | } 24 | vector letterCombinations(string digits) 25 | { 26 | string s = ""; 27 | combinations(digits, s, 0); 28 | if (digits.length() == 0) 29 | { 30 | return {}; 31 | } 32 | return ans; 33 | } 34 | }; -------------------------------------------------------------------------------- /70+codechef_solutions/chess_dist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int i,t,a,b,c,d; 6 | cin>>t; 7 | for(i=0;i>a>>b>>c>>d; 10 | if(a>c) 11 | { 12 | if(b>d) 13 | { 14 | if((a-c)>(b-d)) 15 | cout<(d-b)) 22 | cout<d) 30 | { 31 | if((c-a)>(b-d)) 32 | cout<(d-b)) 39 | cout< 1 && i < num) { 24 | System.out.print("*\t"); 25 | } 26 | 27 | if (i <= num / 2) { 28 | os--; 29 | is += 2; 30 | } else { 31 | os++; 32 | is -= 2; 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/invertedBarChart.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class invertedBarChart { 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | 9 | int arr[] = new int[n]; 10 | for (int i = 0; i < n; i++) { 11 | arr[i] = sc.nextInt(); 12 | } 13 | 14 | int max = arr[0]; 15 | for (int i = 0; i < arr.length; i++) { 16 | if (arr[i] > max) { 17 | max = arr[i]; 18 | } 19 | } 20 | 21 | for (int i = 0; i < max; i++) { 22 | for (int j = 0; j < arr.length; j++) { 23 | if (i < arr[j]) { 24 | System.out.print("*\t"); 25 | } else { 26 | System.out.print("\t"); 27 | } 28 | } 29 | System.out.println(); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /InterviewBit/First_missing_positive.cpp: -------------------------------------------------------------------------------- 1 | Given an unsorted integer array nums, return the smallest missing positive integer. 2 | 3 | You must implement an algorithm that runs in O(n) time and uses constant extra space. 4 | 5 | Example 1: 6 | 7 | Input: nums = [1,2,0] 8 | Output: 3 9 | Example 2: 10 | 11 | Input: nums = [3,4,-1,1] 12 | Output: 2 13 | Example 3: 14 | 15 | Input: nums = [7,8,9,11,12] 16 | Output: 1 17 | 18 | Solution: 19 | class Solution { 20 | public: 21 | int firstMissingPositive(vector& nums) { 22 | int n = nums.size(); 23 | 24 | vectorpresent(n); 25 | 26 | for(int i=0;i0 && nums[i]<=n) 29 | present[nums[i]]=true; 30 | } 31 | 32 | for(int i=1;i<=n;i++) 33 | { 34 | if(present[i]!=true) 35 | return i; 36 | } 37 | return n+1; 38 | } 39 | }; 40 | 41 | Time Complexity : O(n) 42 | Space Complexity : O(n) 43 | 44 | -------------------------------------------------------------------------------- /LeetCode/169. majorityElement.cpp: -------------------------------------------------------------------------------- 1 | // Contributed by : RajatRathore123-github 2 | 3 | // Given an array nums of size n, return the majority element. 4 | 5 | /*The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.*/ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int majorityElement(vector& nums) { 11 | int count = 0; 12 | int value = 0; 13 | for(int num : nums){ 14 | if(count == 0){ 15 | value = num; 16 | } 17 | if(num == value){ 18 | count += 1; 19 | } 20 | else{ 21 | count -= 1; 22 | } 23 | } 24 | return value; 25 | } 26 | 27 | int main(){ 28 | int n; 29 | cin >> n; 30 | vector nums(n); 31 | for(int i = 0; i < n ;i++){ 32 | cin >> nums[i]; 33 | } 34 | cout << "majority element is : " << majorityElement(nums) << endl; 35 | } 36 | -------------------------------------------------------------------------------- /LeetCode/2 Add Two Numbers.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | ListNode *ans = new ListNode(); 15 | ListNode *curr = ans; 16 | int carry = 0; 17 | while(l1 || l2){ 18 | 19 | int a = l1 ? l1->val : 0; 20 | int b = l2 ? l2->val : 0; 21 | 22 | curr->next = new ListNode((a+b+carry)%10); 23 | carry = (a+b+carry)/10; 24 | curr = curr->next; 25 | 26 | if(l1) l1 = l1->next; 27 | if(l2) l2 = l2->next; 28 | } 29 | if(carry) curr->next = new ListNode(carry); 30 | return ans->next; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /LeetCode/48 Rotate Image.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /* 4 | * clockwise rotate 5 | * first reverse up to down, then swap the symmetry 6 | * 1 2 3 7 8 9 7 4 1 7 | * 4 5 6 => 4 5 6 => 8 5 2 8 | * 7 8 9 1 2 3 9 6 3 9 | */ 10 | void rotate(vector > &matrix) { 11 | reverse(matrix.begin(), matrix.end()); 12 | for (int i = 0; i < matrix.size(); ++i) { 13 | for (int j = i + 1; j < matrix[i].size(); ++j) 14 | swap(matrix[i][j], matrix[j][i]); 15 | } 16 | } 17 | 18 | /* 19 | * anticlockwise rotate 20 | * first reverse left to right, then swap the symmetry 21 | * 1 2 3 3 2 1 3 6 9 22 | * 4 5 6 => 6 5 4 => 2 5 8 23 | * 7 8 9 9 8 7 1 4 7 24 | */ 25 | void anti_rotate(vector > &matrix) { 26 | for (auto vi : matrix) reverse(vi.begin(), vi.end()); 27 | for (int i = 0; i < matrix.size(); ++i) { 28 | for (int j = i + 1; j < matrix[i].size(); ++j) 29 | swap(matrix[i][j], matrix[j][i]); 30 | } 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/Patterns/inverted_hourglass.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class inverted_hourglass { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int osp = 0, star = n; 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = 1; j <= osp; j++) { 11 | System.out.print("\t"); 12 | } 13 | 14 | for (int j = 1; j <= star; j++) { 15 | if ((i > n / 2 + 1 && i < n) && (j > 1 && j < star)) { 16 | System.out.print("\t"); 17 | } else { 18 | System.out.print("*\t"); 19 | } 20 | } 21 | 22 | if (i < n / 2 + 1) { 23 | osp++; 24 | star -= 2; 25 | } else { 26 | osp--; 27 | star += 2; 28 | } 29 | 30 | System.out.println(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /insertionSort.cpp: -------------------------------------------------------------------------------- 1 | // Insertion sort in C++ 2 | 3 | #include 4 | using namespace std; 5 | 6 | // Function to print an array 7 | void printArray(int array[], int size) { 8 | for (int i = 0; i < size; i++) { 9 | cout << array[i] << " "; 10 | } 11 | cout << endl; 12 | } 13 | 14 | void insertionSort(int array[], int size) { 15 | for (int step = 1; step < size; step++) { 16 | int key = array[step]; 17 | int j = step - 1; 18 | 19 | // Compare key with each element on the left of it until an element smaller than 20 | // it is found. 21 | // For descending order, change keyarray[j]. 22 | while (key < array[j] && j >= 0) { 23 | array[j + 1] = array[j]; 24 | --j; 25 | } 26 | array[j + 1] = key; 27 | } 28 | } 29 | 30 | // Driver code 31 | int main() { 32 | int data[] = {9, 5, 1, 4, 3}; 33 | int size = sizeof(data) / sizeof(data[0]); 34 | insertionSort(data, size); 35 | cout << "Sorted array in ascending order:\n"; 36 | printArray(data, size); 37 | } 38 | -------------------------------------------------------------------------------- /Codeforces/1287A_Angry_Students.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int func1(int n){ 6 | char n1[n]; 7 | int tmp_cnt=0, cnt=0; 8 | // cout<>n1[j]; 12 | } 13 | for(int i = 0; i cnt) { 24 | cnt = tmp_cnt; 25 | } 26 | tmp_cnt = 0; 27 | } 28 | } 29 | cout << cnt << endl; 30 | return 0; 31 | } 32 | 33 | int main(){ 34 | int t=0; 35 | cin>>t; 36 | int n[t]; 37 | for (int i = 0; i < t; i++) 38 | { 39 | cin>>n[i]; 40 | func1(n[i]); 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /LeetCode/5. Longest Palindromic Substring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestPalindrome(string s) { 4 | if(s==""){ 5 | return s; 6 | } 7 | int len = s.length(); 8 | int dp[1001][1001]; 9 | for(int i=0;i productExceptSelf(vector& nums) { 4 | int pro=1; 5 | vector ans; 6 | int zerocount=0; 7 | for(int i=0;i1){ 19 | ans=vector (nums.size(),0); 20 | return ans; 21 | } 22 | else{ 23 | int p=1; 24 | int idx; 25 | for(int i=0;i (nums.size(),0); 32 | ans[idx]=p; 33 | } 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/subsetsOfArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class subsetsOfArray { 5 | 6 | public static void main(String[] args) throws Exception { 7 | // write your code here 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | int arr[] = new int[n]; 12 | 13 | for (int i = 0; i < n; i++) { 14 | arr[i] = sc.nextInt(); 15 | } 16 | 17 | int limit = (int) Math.pow(2, arr.length); 18 | 19 | for (int i = 0; i < limit; i++) { 20 | int temp = i; 21 | String val = ""; 22 | for (int j = arr.length - 1; j >= 0; j--) { 23 | int r = temp % 2; 24 | temp /= 2; 25 | 26 | if (r == 0) { 27 | val = "-\t" + val; 28 | } else { 29 | val = arr[j] + "\t" + val; 30 | } 31 | } 32 | System.out.println(val); 33 | } 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /LeetCode/Java/64 Minimum Path Sum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void print(int[][] grid){ 3 | for(int i=0;i num / 2) { 22 | if (i == (num / 2) + 1) { 23 | space = num / 2; 24 | count = num / 2; 25 | } else { 26 | count--; 27 | } 28 | 29 | } else { 30 | space = 0; 31 | count = num; 32 | } 33 | 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LeetCode/32 Longest Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestValidParentheses(string s) { 4 | 5 | int n=s.length(); 6 | int left=0,right=0; 7 | int mx=0,i=0; 8 | for(i;i=left) 19 | { 20 | left=0; 21 | right=0; 22 | } 23 | } 24 | i=n-1; 25 | left=0,right=0; 26 | for(i;i>=0;i--) 27 | { 28 | 29 | if(s[i]=='(') 30 | left++; 31 | else 32 | right++; 33 | if(left==right) 34 | mx=max(mx,2*left); 35 | else if(left>=right) 36 | { 37 | left=0; 38 | right=0; 39 | } 40 | } 41 | return mx; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /LeetCode/234 Palindrome Linked List.cpp: -------------------------------------------------------------------------------- 1 | // Problem statement: check if the linked list is palindrome or not 2 | 3 | class Solution { 4 | public: 5 | bool isPalindrome(ListNode* head) { 6 | ListNode *slow = head,*fast=head,*prev, *temp; 7 | //FINDING MIDDLE NODE 8 | while(fast!=NULL && fast->next!=NULL){ 9 | slow = slow->next; 10 | fast = fast->next->next; 11 | } 12 | // REVERSING ELEMENTS AFTER MIDDLE NODE 13 | prev = slow; 14 | slow= slow->next; 15 | prev->next = NULL; 16 | while(slow){ 17 | temp= slow->next; 18 | slow->next = prev; 19 | prev = slow; 20 | slow = temp; 21 | } 22 | // COMPARING FROM TWO ENDS 23 | fast = head; 24 | slow = prev; 25 | while(slow){ 26 | if(fast->val!=slow->val){ 27 | return false; 28 | } 29 | else{ 30 | fast = fast->next; 31 | slow = slow->next; 32 | } 33 | } 34 | return true; 35 | } 36 | }; -------------------------------------------------------------------------------- /Range_Sum_Query.cpp: -------------------------------------------------------------------------------- 1 | #define ll long long 2 | class NumArray { 3 | 4 | vector bit; 5 | vector arr; 6 | 7 | ll query(ll idx){ 8 | ll ans = 0; 9 | while(idx > 0){ 10 | ans+=bit[idx]; 11 | idx = ( idx - (idx&-idx) ); 12 | } 13 | return ans; 14 | } 15 | 16 | void upd(ll idx, ll val){ 17 | while(idx& nums) { 25 | bit.clear(); 26 | bit.resize(nums.size() + 2); 27 | 28 | arr = nums; 29 | 30 | for(int i = 0;i& prices) { 4 | // int n=prices.size(); 5 | // this soln for any genral case when k is anythin 6 | // int dp[3][n+1]; 7 | // for(int i=0;i<3;i++) 8 | // dp[i][0]=0; 9 | // for(int j=0;j 0 || n2 > 0 || carry > 0) { 22 | int d1 = n1 % 10; 23 | int d2 = n2 % 10; 24 | int sum = d1 + d2 + carry; 25 | int ad_val = sum % b; 26 | 27 | res = res + (ad_val * multiplier); 28 | 29 | carry = sum / b; 30 | multiplier *= 10; 31 | n1 /= 10; 32 | n2 /= 10; 33 | } 34 | 35 | return res; 36 | } 37 | } -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/inverseOfAnArray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class inverseOfAnArray { 5 | public static void display(int[] a) { 6 | StringBuilder sb = new StringBuilder(); 7 | 8 | for (int val : a) { 9 | sb.append(val + "\n"); 10 | } 11 | System.out.println(sb); 12 | } 13 | 14 | public static int[] inverse(int[] a) { 15 | // write your code here 16 | int ans[] = new int[a.length]; 17 | 18 | for (int i = 0; i < a.length; i++) { 19 | int index = a[i]; 20 | ans[index] = i; 21 | } 22 | 23 | return ans; 24 | } 25 | 26 | public static void main(String[] args) throws Exception { 27 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 28 | 29 | int n = Integer.parseInt(br.readLine()); 30 | int[] a = new int[n]; 31 | for (int i = 0; i < n; i++) { 32 | a[i] = Integer.parseInt(br.readLine()); 33 | } 34 | 35 | int[] inv = inverse(a); 36 | display(inv); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /LeetCode/88_compare_two_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }; 9 | 10 | bool areIdentical(struct Node *a, struct Node *b) 11 | { 12 | while (a != NULL && b != NULL) 13 | { 14 | if (a->data != b->data) 15 | return false; 16 | a = a->next; 17 | b = b->next; 18 | } 19 | 20 | return (a == NULL && b == NULL); 21 | } 22 | 23 | void push(struct Node **head_ref, int new_data) 24 | { 25 | struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); 26 | 27 | new_node->data = new_data; 28 | 29 | new_node->next = (*head_ref); 30 | 31 | (*head_ref) = new_node; 32 | } 33 | 34 | int main() 35 | { 36 | struct Node *a = NULL; 37 | struct Node *b = NULL; 38 | push(&a, 1); 39 | push(&a, 2); 40 | push(&a, 3); 41 | push(&b, 1); 42 | push(&b, 2); 43 | push(&b, 3); 44 | 45 | if (areIdentical(a, b)) 46 | cout << "Identical"; 47 | else 48 | cout << "Not identical"; 49 | 50 | return 0; 51 | } 52 | 53 | //output 54 | /* 55 | Identical 56 | */ -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/brokenEconomy.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class brokenEconomy { 5 | public static void main(String[] args) throws Exception { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | int n = sc.nextInt(); 9 | int arr[] = new int[n]; 10 | 11 | for (int i = 0; i < arr.length; i++) { 12 | arr[i] = sc.nextInt(); 13 | } 14 | 15 | int d = sc.nextInt(); 16 | 17 | int low = 0; 18 | int high = arr.length - 1; 19 | 20 | int ceil = 0, floor = 0; 21 | while (low <= high) { 22 | int mid = (low + high) / 2; 23 | 24 | if (arr[mid] == d) { 25 | ceil = floor = arr[mid]; 26 | break; 27 | } else if (d < arr[mid]) { 28 | high = mid - 1; 29 | ceil = arr[high]; 30 | } else { 31 | low = mid + 1; 32 | floor = arr[low]; 33 | } 34 | } 35 | 36 | System.out.println(arr[low]); 37 | System.out.print(arr[high]); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /CodeChef/Java/COMM3 Three Way Communications.java: -------------------------------------------------------------------------------- 1 | /* package codechef; // don't place package name! */ 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | 7 | /* Name of the class has to be "Main" only if the class is public. */ 8 | class Codechef 9 | { 10 | public static void main (String[] args) throws java.lang.Exception 11 | { 12 | // your code goes here 13 | Scanner sc = new Scanner(System.in); 14 | int tcases = sc.nextInt(); 15 | while(tcases-->0) 16 | { 17 | int r = sc.nextInt(); 18 | int cx = sc.nextInt(); 19 | int cy = sc.nextInt(); 20 | int hx = sc.nextInt(); 21 | int hy = sc.nextInt(); 22 | int sx = sc.nextInt(); 23 | int sy = sc.nextInt(); 24 | int count = 0; 25 | if(Math.sqrt(Math.pow((cx-hx),2)+Math.pow((cy-hy),2))<=r) 26 | count++; 27 | if(Math.sqrt(Math.pow((cx-sx),2)+Math.pow((cy-sy),2))<=r) 28 | count++; 29 | if(Math.sqrt(Math.pow((sx-hx),2)+Math.pow((sy-hy),2))<=r) 30 | count++; 31 | if(count==0 || count==1) 32 | System.out.println("no"); 33 | else System.out.println("yes"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LeetCode/15 3Sum.cpp: -------------------------------------------------------------------------------- 1 | // Given an integer array nums, return all the triplets[nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. 2 | 3 | //Notice that the solution set must not contain duplicate triplets. 4 | 5 | vector> threeSum(vector &nums) 6 | { 7 | vector> vans; 8 | sort(nums.begin(), nums.end()); 9 | for (int i = 0; i != nums.size(); ++i) 10 | { 11 | if (i > 0 && nums[i] == nums[i - 1]) 12 | continue; 13 | int sum = -(nums[i]); 14 | int j = i + 1, k = nums.size() - 1; 15 | while (j < k) 16 | { 17 | if (nums[j] + nums[k] < sum) 18 | { 19 | ++j; 20 | } 21 | 22 | else if (nums[j] + nums[k] == sum) 23 | { 24 | if (!(j > i + 1 && nums[j] == nums[j - 1])) 25 | vans.push_back({nums[i], nums[j], nums[k]}); 26 | ++j; 27 | --k; 28 | } 29 | else 30 | { 31 | --k; 32 | } 33 | } 34 | } 35 | 36 | return vans; 37 | } 38 | -------------------------------------------------------------------------------- /LeetCode/1337 kWeakestRows.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kWeakestRows(vector>& arr, int k) { 4 | 5 | int m = arr.size(), n = arr[0].size(); 6 | vector> vec; 7 | for(int i = 0;i n-1 || arr[i][mid+1] == 0){ 14 | pos1 = mid; 15 | break; 16 | } 17 | else{ 18 | pos1 = mid; 19 | low = mid+1; 20 | } 21 | } 22 | else high = mid-1; 23 | } 24 | vec.push_back({pos1, i}); 25 | } 26 | vector ans; 27 | sort(vec.begin(),vec.end()); 28 | int i = 1; 29 | for(auto it : vec){ 30 | ans.emplace_back(it.second); 31 | if(i == k) break; 32 | i++; 33 | } 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Codeforces/twosets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | long long int n; 5 | cin>>n; 6 | long long sum = (n*(n+1))/2; 7 | if(sum&1) 8 | cout<<"NO"<= arr[index]){ 19 | sum-=arr[index]; 20 | arr[index]*=(-1); 21 | index--; 22 | count++; 23 | } 24 | else 25 | index = sum-1; 26 | } 27 | cout<0) 36 | cout< nums, int target){ 4 | int l=0, h=nums.size()-1, mid, ans=-1; 5 | while(l<=h){ 6 | mid=l+(h-l)/2; 7 | if(nums[mid]==target){ 8 | ans= mid; 9 | h=mid-1; 10 | } 11 | else if(nums[mid]>target) 12 | h=mid-1; 13 | else 14 | l=mid+1; 15 | } 16 | return ans; 17 | } 18 | 19 | int UB(vector nums, int target){ 20 | int l=0, h=nums.size()-1, mid, ans=-1; 21 | while(l<=h){ 22 | mid=l+(h-l)/2; 23 | if(nums[mid]==target){ 24 | ans= mid; 25 | l=mid+1; 26 | } 27 | else if(nums[mid] searchRange(vector& nums, int target) { 36 | int lb = LB(nums, target); 37 | int ub = UB(nums, target); 38 | return {lb, ub}; 39 | } 40 | }; -------------------------------------------------------------------------------- /LeetCode/983 Minimum Cost For Tickets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dp; 4 | int mincostTickets(vector& days, vector& costs) { 5 | return helper(0,0, days, costs); 6 | } 7 | 8 | int helper(int index,int validity, vector& days, vector& costs) { 9 | // if we reach nth index 10 | if(index>=days.size()) 11 | return 0; 12 | 13 | // if we buy a ticket with validity greater than current day 14 | if(validity >= days[index]) 15 | return helper(index+1, validity, days, costs); 16 | 17 | int mini = INT_MAX; 18 | // Taking 1 day ticket and adding cost to present cost 19 | mini = min(mini, costs[0]+helper(index+1, days[index], days, costs)); 20 | // Taking 7 day ticket and adding cost to present cost 21 | mini = min(mini, costs[1]+helper(index+1, days[index]+6, days, costs)); 22 | // Taking 30 day ticket and adding cost to present cost 23 | mini = min(mini, costs[2]+helper(index+1, days[index]+29, days, costs)); 24 | 25 | // storing the minimum in dp 26 | return mini; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/2409. Count Days Spent Together: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool grt(string a, string b){ 3 | return a>b; 4 | } 5 | public: 6 | int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) { 7 | if(leaveAlice dm{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 22 | int d1 = (init[3]-'0')*10+(init[4]-'0'), 23 | d2 = (last[3]-'0')*10+(last[4]-'0'), 24 | m1 = (init[0]-'0')*10+(init[1]-'0'), 25 | m2 = (last[0]-'0')*10+(last[1]-'0'); 26 | int ans=0; 27 | for(int i=m1;i<=m2;i++){ 28 | ans += dm[i]; 29 | } 30 | ans -= (d1-1+dm[m2]-d2); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Pepcoding/1. Basics of Programming/function and arrays/anyBaseSubtraction.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class anyBaseSubtraction { 4 | 5 | public static void main(String[] args) { 6 | Scanner scn = new Scanner(System.in); 7 | int b = scn.nextInt(); 8 | int n1 = scn.nextInt(); 9 | int n2 = scn.nextInt(); 10 | 11 | int d = getDifference(b, n1, n2); 12 | System.out.println(d); 13 | } 14 | 15 | public static int getDifference(int b, int n1, int n2) { 16 | // write your code here 17 | int result = 0, carry = 0, multiplier = 1; 18 | 19 | while (n2 > 0) { 20 | int d1 = n1 % 10; 21 | n1 /= 10; 22 | 23 | int d2 = n2 % 10; 24 | n2 /= 10; 25 | d2 += carry; 26 | 27 | int d = 0; 28 | if (d2 >= d1) { 29 | carry = 0; 30 | d = d2 - d1; 31 | } else { 32 | carry = -1; 33 | d = d2 + b - d1; 34 | } 35 | 36 | result = result + (d * multiplier); 37 | multiplier *= 10; 38 | } 39 | return result; 40 | } 41 | 42 | } --------------------------------------------------------------------------------