├── .vscode └── settings.json ├── Assets ├── Codes │ ├── Codechef │ │ ├── ATM.md │ │ ├── AlicevsBobFaceoff.md │ │ ├── AlternativeSquarePattern.md │ │ ├── BellaCiao.md │ │ ├── ChefAndBoardGames.md │ │ ├── ChefAndPatients.md │ │ ├── EnormousInputTest.md │ │ ├── K-BalanceString.md │ │ ├── LongestAndSubarray.md │ │ ├── MEXOR.md │ │ ├── MagicalDoors.md │ │ ├── PackagingCupcakes.md │ │ ├── ReverseStarPattern.md │ │ ├── SumIsEverywhere.md │ │ ├── TheBlockGame.md │ │ ├── TheLeadGame.md │ │ ├── ThreeBoxes.md │ │ ├── TriangleEverywhere.md │ │ ├── WhichMixtures.md │ │ ├── adjacencyHatred.md │ │ ├── airline.md │ │ ├── chefAndWaterCar.md │ │ ├── exactlyNPlus1Values.md │ │ ├── professorAndDirections.md │ │ ├── shufflin.md │ │ ├── summerHeat.md │ │ ├── totalScore.md │ │ ├── travelpass.md │ │ ├── yetAnotherSODProblems.md │ │ └── zebraCrossing.md │ ├── Codeforces │ │ ├── BoilerPlateCode.md │ │ ├── ChessTournament.md │ │ ├── MEXorMixup.md │ │ ├── Palindromifier.md │ │ ├── RelativelyPrimePairs.md │ │ ├── doNotBeDistracted.md │ │ ├── maxElementsInRange.md │ │ ├── maximumControl(easy).md │ │ ├── nastiaAndNearlyGoodNumbers.md │ │ ├── problemAboutEquation.md │ │ ├── theGreatHero.md │ │ └── trwoRegularPolygons.md │ ├── Hackerrank │ │ ├── Dynamic Array in C.md │ │ ├── Printing Pattern Using Loops.md │ │ ├── Small Triangles, Large Triangles.md │ │ ├── greedyFlorist.md │ │ ├── maxMin.md │ │ └── numberLineJumps.md │ ├── Leetcode │ │ ├── Add Two Numbers.md │ │ ├── BestTimeToBuyAndSellStock.md │ │ ├── BitwiseAND.md │ │ ├── ClimbingStaircase.md │ │ ├── CointainerWithMostWater.md │ │ ├── FindSmallestLetterGreaterThanTarget.md │ │ ├── HouseRobberIII.md │ │ ├── IsGraphBipartite.md │ │ ├── KthLargestElementInAnArray.md │ │ ├── LongestPalindromicSubsequence.md │ │ ├── LongestPalindromicSubstring.md │ │ ├── LongestSubstringWithoutRepeatingCharacters.md │ │ ├── LuckyNumbersInAMatrix.md │ │ ├── MajorityElement.md │ │ ├── MiddleOfLinkedList.md │ │ ├── MinimumMovesToEqualArrayElements.md │ │ ├── MoveZeroes.md │ │ ├── NextPermutation.md │ │ ├── ReshapeTheMatrix.md │ │ ├── ReverseLinkedList.md │ │ ├── SearchInRotatedSortedArray.md │ │ ├── SearchInsertPosition.md │ │ ├── SetMatrixZeroes.md │ │ ├── SubDomainVisitCount.md │ │ ├── TwoSum.md │ │ ├── ValidParentheses.md │ │ ├── ZigZag_Conversion.md │ │ ├── duplicateNumber.md │ │ ├── findTheDifference.md │ │ ├── isPalindrome.md │ │ ├── largestRectangleInAHistogram.md │ │ ├── maxConsecutiveOnes.md │ │ ├── mergeSortedArray.md │ │ ├── nQueenProblemSolution.md │ │ ├── numberOfIslands.md │ │ ├── plusOne.md │ │ ├── removeNthNodeFromEndOfList.md │ │ ├── rotateList.md │ │ ├── shuffleAnArray.md │ │ ├── swappingNodesInALinkedList.md │ │ └── trappingRainWater.md │ ├── Miscellaneous │ │ ├── A1WeakTypingChapter1.md │ │ ├── BTrafficControl.md │ │ ├── BXsAndOs.md │ │ ├── BitStrings(CSES).md │ │ ├── FirstElementToOccurKtimes.md │ │ ├── IncreasingArray(CSES).md │ │ ├── MissingNumber(CSES).md │ │ ├── Permutations(CSES).md │ │ ├── Repetitions(CSES).md │ │ ├── TrailingZeros(CSES).md │ │ ├── TwoKnights(CSES).md │ │ ├── TwoSets(CSES).md │ │ ├── WeirdAlgorithm(CSES).md │ │ ├── classGetSet.md │ │ ├── infixToPostfix.md │ │ ├── kadanesAlgorithm.md │ │ ├── kthSmallestElement.md │ │ ├── minimumNumberOfJumps.md │ │ ├── postfixEvaluation.md │ │ ├── queueReversal.md │ │ ├── reverseFirstKElementsOfQueue.md │ │ ├── splitHouses.md │ │ ├── stringStream.md │ │ ├── struct.md │ │ └── variableSizedArray.md │ └── boilerPlate.md ├── Images │ ├── HackerRank.svg │ ├── codeTerminal.svg │ ├── codechef.svg │ ├── codechefLogo.svg │ ├── codeforces.svg │ ├── codeforcesLogo.svg │ ├── dropdownButton.svg │ ├── hackerrankLogo.svg │ ├── hacktoberfestLogo.png │ ├── hacktoberfestLogo.svg │ ├── lazyCat.svg │ ├── lazyCodeLogo.svg │ ├── leetcode.svg │ ├── leetcodeLogo.svg │ ├── miscellaneous.svg │ └── scrollUp.svg ├── commandsGit.md └── introductionAndFeatures.md ├── CONTRIBUTING.md ├── LICENSE ├── Markups ├── homePageDemo.html ├── questionPageDemo.html └── solutionPageDemo.html ├── README.md ├── Scripts └── index.js ├── Styles ├── homePageDemo.css ├── index.css ├── questionPageDemo.css └── solutionPageDemo.css └── index.html /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "liveServer.settings.port": 5501 3 | } 4 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/ATM.md: -------------------------------------------------------------------------------- 1 | #### Name: ATM 2 | #### Language: JAVA 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/problems/HS08TEST 5 | 6 | #### Question Statement 7 | Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US. Calculate Pooja's account balance after an attempted transaction. 8 | 9 | Input 10 | Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw. 11 | 12 | Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance. 13 | 14 | Output 15 | Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance. 16 | 17 | 18 | #### Solution: 19 | ``` 20 | import java.util.*; 21 | import java.lang.*; 22 | import java.io.*; 23 | 24 | /* Name of the class has to be "Main" only if the class is public. */ 25 | class Codechef 26 | { 27 | public static void main (String[] args) throws java.lang.Exception 28 | { 29 | try{ 30 | Scanner input = new Scanner(System.in); 31 | double x = input.nextDouble(); 32 | double y = input.nextDouble(); 33 | 34 | if(x%5==0 && y>=(x+0.50) && x>0){ 35 | System.out.printf("%.2f",y-(x+0.50)); 36 | } 37 | else{ 38 | System.out.printf("%.2f",y); 39 | } 40 | } 41 | catch(Exception e){ 42 | return; 43 | } 44 | 45 | } 46 | } 47 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/AlicevsBobFaceoff.md: -------------------------------------------------------------------------------- 1 | #### Name: Alice vs Bob Faceoff 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/LTIME100B/problems/ALBOFACE 5 | 6 | #### Question Statement 7 | Alice and Bob have got an integer N. They decide to play a game. Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, 8 | Alice makes the third one, and so on. The game continues until N becomes 0. The player who is unable to make a move loses. 9 | During each turn a player can make one of the following moves: 10 | Choose an integer X such that X can be expressed as 2Y, Y≥1. The chosen X must also be a factor of N. After choosing an integer X which satisfies the mentioned criteria, 11 | the player will divide N by X. 12 | If Move 1 is not possible , the player will subtract 1 from N. 13 | Predict the winner of the game if the game is played optimally by both the players. 14 | 15 | #### Solution: 16 | 17 | ``` 18 | #pragma GCC optimize("Ofast") 19 | #pragma GCC target("avx,avx2,fma") 20 | #pragma GCC optimization("unroll-loops") 21 | #include 22 | using namespace std; 23 | typedef long long ll; 24 | typedef long double ld; 25 | #define pb push_back 26 | const long double Pi = 3.141592653; 27 | const ll mod=1e9+7; 28 | long long INF =1000000000000000000; 29 | struct mycmp 30 | { 31 | bool operator()(paira,pairb)const 32 | { 33 | return (a.firstb.second); 34 | } 35 | }; 36 | int main() 37 | { 38 | ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 39 | int t=1;cin>>t; 40 | while(t--) 41 | { 42 | ll n;cin>>n; 43 | if(n==1) 44 | { 45 | cout<<"Alice\n"; 46 | } 47 | else 48 | { 49 | if(n&1LL) 50 | { 51 | n=n-1; 52 | ll cnt=0; 53 | while(n) 54 | { 55 | ll cnt1=0; 56 | while(n%2==0) 57 | n=n/2,++cnt1; 58 | n=n-1; 59 | if(cnt1>=2) 60 | ++cnt; 61 | } 62 | if(cnt>=2) 63 | cout<<"Bob\n"; 64 | else 65 | { 66 | if(cnt==0) 67 | cout<<"Alice\n"; 68 | else 69 | cout<<"Bob\n"; 70 | } 71 | } 72 | else 73 | { 74 | ll cnt=0; 75 | while(n) 76 | { 77 | ll cnt1=0; 78 | while(n%2==0) 79 | n=n/2,++cnt1; 80 | n=n-1; 81 | if(cnt1>=2) 82 | ++cnt; 83 | } 84 | //cout<=2) 86 | cout<<"Alice\n"; 87 | else 88 | { 89 | if(cnt==0) 90 | cout<<"Bob\n"; 91 | else 92 | cout<<"Alice\n"; 93 | } 94 | } 95 | } 96 | } 97 | } 98 | 99 | ``` 100 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/AlternativeSquarePattern.md: -------------------------------------------------------------------------------- 1 | #### Name: Alternative Square Pattern 2 | #### Language: JAVA 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/CCSTART2/problems/SQALPAT 5 | 6 | #### Question Statement 7 | You're given a number N. Print the first N lines of the below-given pattern. 8 | 9 | 1 2 3 4 5 10 | 10 9 8 7 6 11 | 11 12 13 14 15 12 | 20 19 18 17 16 13 | 21 22 23 24 25 14 | 30 29 28 27 26 15 | Input: 16 | First-line will contain the number N. 17 | Output: 18 | Print the first N lines of the given pattern. 19 | 20 | 21 | #### Solution: 22 | ``` 23 | import java.util.*; 24 | import java.lang.*; 25 | import java.io.*; 26 | 27 | /* Name of the class has to be "Main" only if the class is public. */ 28 | class Codechef 29 | { 30 | public static void main (String[] args) throws java.lang.Exception 31 | { 32 | try{ 33 | Scanner s=new Scanner(System.in); 34 | int n=s.nextInt(); 35 | int sumi = 0, sumj = 10; 36 | for(int i=1; i<=n; i++){ 37 | if(i%2!=0){ 38 | for(int j=sumi+1; j<=sumi+5; j++){ 39 | System.out.print(j+" "); 40 | } 41 | System.out.print("\n"); 42 | sumi = sumi + 10; 43 | }else{ 44 | for(int j=sumj; j>=sumj-4; j--){ 45 | System.out.print(j+" ");; 46 | } 47 | System.out.print("\n"); 48 | sumj = sumj + 10; 49 | } 50 | } 51 | } 52 | catch(Exception e){ 53 | return; 54 | } 55 | } 56 | } 57 | 58 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/BellaCiao.md: -------------------------------------------------------------------------------- 1 | #### Name: Bella ciao 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/JUNE21B/problems/CHFHEIST 5 | 6 | #### Question Statement 7 | 8 | Chef is planning a heist in the reserve bank of Chefland. They are planning to hijack the bank for D days and print the money. The initial rate of printing the currency is P dollars per day and they increase the production by Q dollars after every interval of d days. For example, after d days the rate is P+Q dollars per day, and after 2d days the rate is P+2Q dollars per day, and so on. Output the amount of money they will be able to print in the given period. 9 | 10 | 11 | #### Solution: 12 | ``` 13 | #include 14 | using namespace std; 15 | 16 | int main() { 17 | int t; 18 | cin>>t; 19 | while(t--){ 20 | long long D,d,p,q,x,count=0,y; 21 | cin>>D>>d>>p>>q; 22 | x=D/d; 23 | if(x%2==0){ 24 | count+=d*((x/2)*(2*p+(x-1)*q)); 25 | } 26 | else{ 27 | count+=d*(x*(p+((x-1)/2)*q)); 28 | } 29 | count+=(D%d)*(p+(x)*q); 30 | cout< 20 | 21 | #### Solution: 22 | ``` 23 | import java.util.*; 24 | import java.lang.*; 25 | import java.io.*; 26 | 27 | class Codechef 28 | { 29 | public static void main (String[] args) throws java.lang.Exception 30 | { 31 | try{ 32 | Scanner sc = new Scanner(System.in); 33 | int T = sc.nextInt(); 34 | for(int i=0;i 21 | 22 | #### Solution: 23 | ``` 24 | #include 25 | using namespace std; 26 | bool mycomp(pair& a,pair& b) { 27 | if(a.second==b.second){ 28 | return a.first < b.first; 29 | } 30 | else 31 | return a.second > b.second; 32 | } 33 | int main() { 34 | int t; 35 | cin>>t; 36 | while(t--){ 37 | long long n; 38 | cin>>n; 39 | pair arr[n]; 40 | 41 | for(long long i=0; i>arr[i].second; 44 | } 45 | 46 | sort(arr,arr+n,mycomp); 47 | long long a[n]; 48 | for(long long i=0; i 17 | 18 | ### Solution: 19 | 20 | ``` 21 | 22 | 23 | import java.util.Scanner; 24 | import java.io.IOException; 25 | 26 | 27 | 28 | // Remember that the class name should be "Main" and should be "public". 29 | public class Main { 30 | public static void main(String[] args) { 31 | // System.in and System.out are input and output streams, respectively. 32 | Scanner sc = new Scanner(System.in); 33 | int t = sc.nextInt(); 34 | int n = sc.nextInt(); 35 | int ans = 0; 36 | for (int i = 0; i < t; i++) { 37 | int x = sc.nextInt(); 38 | if (x % n == 0) { 39 | ans++; 40 | } 41 | 42 | } 43 | System.out.println(ans); 44 | } 45 | } 46 | 47 | ``` 48 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/K-BalanceString.md: -------------------------------------------------------------------------------- 1 | #### Name: K-Balance String 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/START12C/problems/KBALANCE/ 5 | 6 | #### Question Statement 7 | 8 | A binary string S of length N is called K-balanced string if it holds the following property: For each index i(1≤i≤N) such that Si= '1', there must exist an index j(1≤j≤N) such that Sj= '1' and ∣i−j∣=K, where ∣x∣ denotes the absolute value of x 9 | For example, if K=1 10 | - "00", "110", "011011" are 1-balanced strings, while "010" , "1001" are not; if K=2 11 | - "10101", "0101000101" are 2-balanced strings, while "0110" , "1001", "010" are not. 12 | 13 | Chef is given a binary string S of length N and an integer K. He can perform the following operation on the string: 14 | - Select an index of the string, and flip the character at that index. This means if the character was "0", it becomes "1", and vice versa. 15 | 16 | What is the minimum number of operations Chef will have to perform so that the string becomes K-balanced? 17 | #### Solution: 18 | ``` 19 | #include 20 | using namespace std; 21 | typedef long long int li; 22 | int main() { 23 | // your code goes here 24 | li t; 25 | cin>>t; 26 | while(t--) 27 | { 28 | li n,k,count=0; 29 | cin>>n>>k; 30 | string s; 31 | cin>>s; 32 | for(int i=0;i=0&&s[i-k]=='1')) 39 | i++; 40 | else if(i+k 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace std; 22 | 23 | //typedef: 24 | typedef long long ll; 25 | typedef long double ld; 26 | ll MOD = 998244353; 27 | ll modulo = 1000000007; 28 | double eps = 1e-12; 29 | 30 | //define: 31 | #define INF 2e18 32 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 33 | 34 | 35 | //function: 36 | 37 | //main: 38 | int main () 39 | { 40 | fast_cin (); 41 | ll t; 42 | cin >> t; 43 | while (t--) 44 | { 45 | ll n; 46 | cin >> n; 47 | if (n == 1) 48 | { 49 | cout << "1\n"; 50 | continue; 51 | } 52 | ll ans1 = 1; 53 | while (ans1 * 2 <= n) 54 | { 55 | ans1 = ans1 * 2; 56 | } 57 | ll ans2 = n - ans1 + 1; 58 | if (n == ans1) 59 | cout << ans1 / 2; 60 | else 61 | cout << max (ans2 , ans1 / 2); 62 | cout << endl; 63 | } 64 | return 0; 65 | } 66 | ``` 67 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/MEXOR.md: -------------------------------------------------------------------------------- 1 | #### Name: MEX-OR 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/OCT21C/problems/MEXOR 5 | #### Question Statement 6 | The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: 7 | 8 | The MEX of [2,2,1] is 0, because 0 does not belong to the array. 9 | The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. 10 | The MEX of [0,3,1,2] is 4 because 0,1,2 and 3 belong to the array, but 4 does not. 11 | Find the maximum possible MEX of an array of non-negative integers such that the bitwise OR of the elements in the array does not exceed X. 12 | #### Solution: 13 | 14 | ``` 15 | #pragma GCC optimize("Ofast") 16 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") 17 | #pragma GCC optimize("unroll-loops") 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | using namespace std; 26 | 27 | //typedef: 28 | typedef long long ll; 29 | typedef long double ld; 30 | ll MOD = 998244353; 31 | ll modulo = 1000000007; 32 | double eps = 1e-12; 33 | 34 | //define: 35 | #define INF 2e18 36 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 37 | 38 | 39 | 40 | //main: 41 | int main() 42 | { 43 | fast_cin(); 44 | ll t; 45 | cin >> t; 46 | while(t--) 47 | { 48 | ll x; 49 | cin >> x; 50 | if (x == 0) 51 | cout << "1"; 52 | else if (x == 1) 53 | cout << "2"; 54 | else 55 | { 56 | ll ans = 1; 57 | while (ans * 2 <= x) 58 | { 59 | ans *= 2; 60 | } 61 | if (ans == x) 62 | cout << x; 63 | else if (ans * 2 - 1 == x) 64 | cout << x + 1; 65 | else 66 | cout << ans; 67 | } 68 | 69 | cout << endl; 70 | } 71 | return 0; 72 | } 73 | 74 | ``` 75 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/MagicalDoors.md: -------------------------------------------------------------------------------- 1 | #### Name: Magical Doors 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/START13B/problems/MAGDOORS 5 | 6 | #### Question Statement 7 | 8 | Chef wants to cross a hallway of N doors. These N doors are represented as a string. Each door is initially either open or close, represented by 1 or 0 respectively. Chef is required to go through all the doors one by one in the order that they appear, starting from the leftmost door and moving only rightwards at each step. 9 | 10 | To make the journey easier, Chef has a magic wand, using which Chef can flip the status of all the doors at once. Determine the minimum number of times Chef has to use this wand to cross the hallway of doors. 11 | 12 | For example, the doors are 10011. Chef will start from the left and enter the first door. After passing through that door, the magic wand is waved. This flips the string to 01100. Now Chef passes through the next two doors in one go. Again, just before reaching the 4th door, the magic wand is waved. Now that the string is in its original state, Chef passes through the last two doors as well. The minimum number of times the magic wand needed to be used here was 2 13 | #### Solution: 14 | ``` 15 | #include 16 | #include 17 | using namespace std; 18 | typedef long long int li; 19 | int main() { 20 | // your code goes here 21 | li t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | li count=0; 26 | string s,s2=""; 27 | cin>>s; 28 | for(li i=0;i 15 | 16 | #### Solution: 17 | ``` 18 | import java.util.*; 19 | import java.lang.*; 20 | import java.io.*; 21 | 22 | class Codechef 23 | { 24 | public static void main (String[] args) throws java.lang.Exception 25 | { 26 | try{ 27 | Scanner sc= new Scanner(System.in); 28 | int t=sc.nextInt(); 29 | 30 | while(t>0){ 31 | int n=sc.nextInt(); 32 | int a = (n/2)+1; 33 | System.out.println(a); 34 | t--; 35 | } 36 | } 37 | catch(Exception e){ 38 | return; 39 | } 40 | } 41 | } 42 | 43 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/ReverseStarPattern.md: -------------------------------------------------------------------------------- 1 | #### Name: Reverse Star Pattern 2 | #### Language: JAVA 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/CCSTART2/problems/REVSTRPT 5 | 6 | #### Question Statement 7 | You're given a number N. Print the first N lines of the below-given pattern. 8 | 9 | * 10 | ** 11 | *** 12 | **** 13 | ***** 14 | Input: 15 | First-line will contain the number N. 16 | Output: 17 | Print the first N lines of the given pattern. 18 | 19 | Constraints 20 | 1≤N≤200 21 | 22 | 23 | 24 | #### Solution: 25 | ``` 26 | import java.util.*; 27 | import java.lang.*; 28 | import java.io.*; 29 | 30 | /* Name of the class has to be "Main" only if the class is public. */ 31 | class Codechef 32 | { 33 | public static void main (String[] args) throws java.lang.Exception 34 | { 35 | try{ 36 | Scanner sc = new Scanner(System.in); 37 | int n = sc.nextInt(); 38 | for (int i= 0; i 17 | 18 | #### Solution: 19 | ``` 20 | import java.util.*; 21 | import java.lang.*; 22 | import java.io.*; 23 | 24 | /* Name of the class has to be "Main" only if the class is public. */ 25 | class Codechef 26 | { 27 | public static void main (String[] args) throws java.lang.Exception 28 | { 29 | try{ 30 | Scanner sc=new Scanner(System.in); 31 | long N=sc.nextInt(); 32 | long even=0; 33 | long odd=0; 34 | for(long i=1;i<=2*N;i++){ 35 | if(i%2==0){ 36 | even+=i; 37 | } 38 | else{ 39 | odd+=i; 40 | } 41 | } 42 | System.out.println(odd+" "+even); 43 | } 44 | catch(Exception e){ 45 | return; 46 | } 47 | } 48 | } 49 | 50 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/TheBlockGame.md: -------------------------------------------------------------------------------- 1 | #### Name: The Block Game 2 | #### Language: JAVA 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/problems/PALL01 5 | 6 | #### Question Statement 7 | The citizens of Byteland regularly play a game. They have blocks each denoting some integer from 0 to 9. These are arranged together in a random manner without seeing to form different numbers keeping in mind that the first block is never a 0. Once they form a number they read in the reverse order to check if the number and its reverse is the same. If both are same then the player wins. We call such numbers palindrome. 8 | 9 | Ash happens to see this game and wants to simulate the same in the computer. As the first step he wants to take an input from the user and check if the number is a palindrome and declare if the user wins or not. 10 | 11 | Input 12 | The first line of the input contains T, the number of test cases. This is followed by T lines containing an integer N. 13 | 14 | Output 15 | For each input output "wins" if the number is a palindrome and "loses" if not, in a new line. 16 | 17 | 18 | 19 | #### Solution: 20 | ``` 21 | import java.util.*; 22 | import java.lang.*; 23 | import java.io.*; 24 | 25 | /* Name of the class has to be "Main" only if the class is public. */ 26 | class Codechef 27 | { 28 | public static void main (String[] args) throws java.lang.Exception 29 | { 30 | try{ 31 | Scanner sc = new Scanner(System.in); 32 | int T = sc.nextInt(); 33 | for(int i=0;i 44 | 45 | #### Solution: 46 | ``` 47 | import java.util.*; 48 | import java.lang.*; 49 | import java.io.*; 50 | 51 | class Codechef 52 | { 53 | public static void main (String[] args) throws java.lang.Exception 54 | { 55 | try{ 56 | Scanner sc=new Scanner(System.in); 57 | int n= sc.nextInt(); 58 | int maxlead=-1; 59 | int Wfinal=-1; 60 | int cs=0; 61 | int ct=0; 62 | for(int i=0;ict){ 69 | int lead=cs-ct; 70 | if(lead>maxlead){ 71 | maxlead=lead; 72 | Wfinal=1; 73 | } 74 | } 75 | else{ 76 | int lead=ct-cs; 77 | if(lead>maxlead){ 78 | maxlead=lead; 79 | Wfinal=2; 80 | } 81 | } 82 | } 83 | System.out.println(Wfinal+" "+maxlead); 84 | } 85 | catch(Exception e){ 86 | return; 87 | } 88 | } 89 | } 90 | 91 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/ThreeBoxes.md: -------------------------------------------------------------------------------- 1 | #### Name: Three Boxes 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/OCT21C/problems/THREEBOX 5 | 6 | #### Question Statement 7 | Chef has 3 boxes of sizes A, B, and C respectively. He puts the boxes in bags of size D (A≤B≤C≤D). Find the minimum number of bags Chef needs so that he can put each box in a bag. A bag can contain more than one box if the sum of sizes of boxes in the bag does not exceed the size of the bag. 8 | 9 | 10 | #### Solution: 11 | 12 | ``` 13 | #pragma GCC optimize("Ofast") 14 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") 15 | #pragma GCC optimize("unroll-loops") 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | //typedef: 26 | typedef long long ll; 27 | typedef long double ld; 28 | ll MOD = 998244353; 29 | ll modulo = 1000000007; 30 | double eps = 1e-12; 31 | 32 | //define: 33 | #define INF 2e18 34 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 35 | 36 | 37 | //function: 38 | 39 | //main: 40 | int main() 41 | { 42 | fast_cin(); 43 | ll t; 44 | cin >> t; 45 | while(t--) 46 | { 47 | int A , B , C , D; 48 | cin >> A >> B >> C >> D; 49 | if (A + B + C <= D) 50 | cout << "1"; 51 | else if ((A + B <= D && C <= D) || (A + C <= D && B <= D) || (B + C <= D && A <= D)) 52 | cout << "2"; 53 | else 54 | cout << "3"; 55 | cout << endl; 56 | } 57 | return 0; 58 | } 59 | 60 | ``` 61 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/TriangleEverywhere.md: -------------------------------------------------------------------------------- 1 | #### Name: Triangle Everywhere 2 | #### Language: JAVA 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/CCSTART2/problems/EXTRICHK 5 | 6 | #### Question Statement 7 | You're given the length of three sides a, b, and c respectively. Now If these three sides can form an Equilateral Triangle then print 1, if these three sides can form an Isosceles Triangle then print 2, if these three sides can form a Scalene Triangle then print 3, otherwise print −1. 8 | 9 | Input: 10 | First-line will contain three numbers a, b, and c separated by space. 11 | Output: 12 | Print the answer in a new line. 13 | 14 | 15 | #### Solution: 16 | ``` 17 | /* package codechef; // don't place package name! */ 18 | 19 | import java.util.*; 20 | import java.lang.*; 21 | import java.io.*; 22 | 23 | /* Name of the class has to be "Main" only if the class is public. */ 24 | class Codechef 25 | { 26 | public static void main (String[] args) throws java.lang.Exception 27 | { 28 | try{ 29 | Scanner s=new Scanner(System.in); 30 | int a=s.nextInt(); 31 | int b=s.nextInt(); 32 | int c=s.nextInt(); 33 | 34 | if(a+b>c&&b+c>a&&a+c>b){ 35 | if(a==b&&b==c){ 36 | System.out.println(1); 37 | } 38 | else if(a!=b&&b!=c&&c!=a){ 39 | System.out.println(3); 40 | } 41 | else{ 42 | System.out.println(2); 43 | } 44 | } 45 | else{ 46 | System.out.println(-1); 47 | } 48 | } 49 | catch(Exception e){ 50 | return; 51 | 52 | } 53 | } 54 | } 55 | 56 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codechef/WhichMixtures.md: -------------------------------------------------------------------------------- 1 | #### Name: Which Mixtures 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/OCT21C/problems/MIXTURE/ 5 | 6 | #### Question Statement 7 | Chef has A units of solid and B units of liquid. He combines them to create a mixture. What kind of mixture does Chef produce: a solution, a solid, or a liquid? 8 | 9 | A mixture is called : 10 | 11 | 1) A solution if A>0 and B>0, 12 | 13 | 2) A solid if B=0, or 14 | 15 | 3) A liquid if A=0. 16 | 17 | #### Solution: 18 | 19 | ``` 20 | #pragma GCC optimize("Ofast") 21 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") 22 | #pragma GCC optimize("unroll-loops") 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | using namespace std; 31 | 32 | //typedef: 33 | typedef long long ll; 34 | typedef long double ld; 35 | ll MOD = 998244353; 36 | ll modulo = 1000000007; 37 | double eps = 1e-12; 38 | 39 | //define: 40 | #define INF 2e18 41 | #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 42 | 43 | 44 | 45 | //main: 46 | int main() 47 | { 48 | fast_cin(); 49 | ll t; 50 | cin >> t; 51 | while(t--) 52 | { 53 | int A , B; 54 | cin >> A >> B; 55 | if (A > 0 && B > 0) 56 | cout << "Solution"; 57 | else if (A == 0 && B > 0) 58 | cout << "Liquid"; 59 | else 60 | cout << "Solid"; 61 | cout << endl; 62 | } 63 | return 0; 64 | } 65 | 66 | 67 | ``` 68 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/adjacencyHatred.md: -------------------------------------------------------------------------------- 1 | #### Name: Adjacency Hatred 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/COOK133C/problems/ADJHATE 5 | 6 | #### Question Statement 7 | 8 | An array is called lovely if the sum of absolute differences of each adjacent pair of elements is odd; formally, the array S of size m is lovely if ∑m−1i=1 |Si−Si+1| is odd. 9 | 10 | You are given an array A of N integers. You need to reorder the array in any manner such that the array becomes lovely. If there is no reordering operation that makes the array lovely, output -1. 11 | #### Solution: 12 | ``` 13 | #include 14 | #include 15 | using namespace std; 16 | typedef long long int li; 17 | int main() { 18 | // your code goes here 19 | int t; 20 | cin>>t; 21 | while(t--) 22 | { 23 | int n; 24 | cin>>n; 25 | vector odd,even; 26 | li arr[n]; 27 | for(int i=0;i>arr[i]; 30 | if(arr[i]%2==0) 31 | even.push_back(arr[i]); 32 | else 33 | odd.push_back(arr[i]); 34 | } 35 | if(even.size()==0||odd.size()==0) 36 | { 37 | cout<<"-1"<<"\n"; 38 | } 39 | else 40 | { 41 | int i; 42 | for(i=0;i 12 | 13 | #### Solution: 14 | ``` 15 | #include 16 | 17 | int main(void) { 18 | // your code goes here 19 | int t; 20 | scanf("%d",&t); 21 | while(t--) 22 | { 23 | int a,b,c,d,e; 24 | scanf("%d %d %d %d %d",&a,&b,&c,&d,&e); 25 | if(a+b<=d && c<=e) 26 | { 27 | printf("YES\n"); 28 | } 29 | else if(a+c<=d && b<=e) 30 | { 31 | printf("YES\n"); 32 | } 33 | else if(c+b<=d && a<=e) 34 | { 35 | printf("YES\n"); 36 | } 37 | else 38 | { 39 | printf("No\n"); 40 | } 41 | } 42 | return 0; 43 | } 44 | 45 | 46 | ``` 47 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/chefAndWaterCar.md: -------------------------------------------------------------------------------- 1 | #### Name: Chef and Water Car 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/LTIME101B/problems/CHEFCAR 5 | 6 | #### Question Statement 7 | 8 | Chef is a great mechanic. As the cost of petrol is increasing day by day he decided to build a water car to take his girlfriend Natasha on a date. Water car has great build quality but is able to travel only 1 Km on every 1 liter of water. To show off his water car to Natasha he took her to the formula racing track which is an (N−1) km long road with checkpoints numbered 1 to N from left to right. The distance between two adjacent checkpoints is 1Km. 9 | 10 | Initially, Chef is at checkpoint number 1 and the tank of the water car is empty. At every checkpoint, there is a water station, but the cost of filling 1 liter of water at a checkpoint is equal to the checkpoint number. Chef can not store more than V liters of water in the tank of a car. Chef is interested in both minimum as well as the maximum cost of filling that can happen to reach the checkpoint number N 11 | 12 | You shouldn't have any water left after reaching Nth checkpoint. Also, you are not allowed to pour out water. 13 | #### Solution: 14 | ``` 15 | #include 16 | using namespace std; 17 | typedef long long int li; 18 | int main() { 19 | // your code goes here 20 | li t; 21 | cin>>t; 22 | while(t--) 23 | { 24 | li n,v,cost=0; 25 | cin>>n>>v; 26 | if(v==1) 27 | { 28 | cout<<(n*(n-1))/2<<" "<<(n*(n-1))/2<<"\n"; 29 | } 30 | else if(v>=n-1) 31 | { 32 | cout<<(n*(n-1))/2<<" "< 16 | #include 17 | using namespace std; 18 | typedef unsigned long long int li; 19 | int main() { 20 | // your code goes here 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | li n,i,num; 26 | cin>>n; 27 | if(n==1) 28 | cout<<"1 1\n"; 29 | else 30 | { 31 | for(i=1;i 16 | using namespace std; 17 | 18 | int main() { 19 | // your code goes here 20 | int t; 21 | cin>>t; 22 | while(t--) 23 | { 24 | int n; 25 | bool flag=false; 26 | cin>>n; 27 | string s; 28 | cin>>s; 29 | for(int i=0;i 13 | 14 | #### Solution: 15 | ``` 16 | #include 17 | using namespace std; 18 | 19 | int main() { 20 | // your code goes here 21 | int t; 22 | scanf("%d",&t); 23 | while(t--) 24 | { 25 | int n,s=0,codd=0,ceven=0; 26 | scanf("%d",&n); 27 | int arr[n]; 28 | for(int i=0;ib) 48 | { 49 | return a; 50 | } 51 | else 52 | { 53 | return b; 54 | } 55 | } 56 | ``` 57 | -------------------------------------------------------------------------------- /Assets/Codes/Codechef/summerHeat.md: -------------------------------------------------------------------------------- 1 | #### Name: Summer Heat 2 | #### Language: C++ 3 | #### Platform: Codechef 4 | #### Question: https://www.codechef.com/JUNE21B/problems/COCONUT 5 | 6 | #### Question Statement 7 | Chefland has 2 different types of coconut, type A and type B. Type A contains only xa milliliters of coconut water and type B contains only xb grams of coconut pulp. Chef's nutritionist has advised him to consume Xa milliliters of coconut water and Xb grams of coconut pulp every week in the summer. Find the total number of coconuts (type A + type B) that Chef should buy each week to keep himself active in the hot weather. 8 | 9 | 10 | #### Solution: 11 | ``` 12 | #include 13 | using namespace std; 14 | 15 | int main() { 16 | long long t; 17 | cin>>t; 18 | while(t--){ 19 | int A,B,rA,rB; 20 | cin>>A>>B>>rA>>rB; 21 | cout<<(rA/A)+(rB/B)< 12 | 13 | #### Solution: 14 | ``` 15 | #include 16 | using namespace std; 17 | int main() { 18 | int t; 19 | cin>>t; 20 | while(t--){ 21 | long long n,k; 22 | cin>>n>>k; 23 | long long A[k]; 24 | for(long long i=0;i>A[i]; 26 | } 27 | for(long long i=0;i>sp; 30 | long long ans=0; 31 | for(long long j=0;j 16 | 17 | #### Solution: 18 | ``` 19 | #include 20 | 21 | int main(void) { 22 | // your code goes here 23 | int t; 24 | scanf("%d",&t); 25 | char str[100]; 26 | while(t--) 27 | { 28 | int n , a , b; 29 | scanf("%d %d %d",&n,&a,&b); 30 | scanf("%s",str); 31 | int sum=0,c1=0,c2=0; 32 | for(int i=0;i 14 | using namespace std; 15 | typedef long long int li; 16 | int main() { 17 | // your code goes here 18 | li t; 19 | cin>>t; 20 | while(t--) 21 | { 22 | li l,r,count=0,left,right; 23 | cin>>l>>r; 24 | if(r-l+1>=3) 25 | { 26 | while(true) 27 | { 28 | if(l%3==0) 29 | { 30 | left=l; 31 | break; 32 | } 33 | l++; 34 | } 35 | while(true) 36 | { 37 | if(r%3==0) 38 | { 39 | right=r; 40 | break; 41 | } 42 | r--; 43 | } 44 | cout<<(right-left)/3+1<<"\n"; 45 | } 46 | else 47 | { 48 | while(l<=r) 49 | { 50 | if(l%3==0) 51 | count++; 52 | l++; 53 | } 54 | cout< 22 | #include 23 | typedef long long int ll; 24 | using namespace std; 25 | int32_t main() 26 | { 27 | ll t; 28 | cin >> t; 29 | while (t--) 30 | { 31 | ll n, k, f = 0; 32 | string s; 33 | cin >> n >> k >> s; 34 | char end; 35 | if (k % 2 == 0) 36 | { 37 | end = s[0]; 38 | } 39 | else if (s[0] == '0') 40 | { 41 | end = '1'; 42 | } 43 | else 44 | end = '0'; 45 | ll x; 46 | for (ll i = n - 1; i >= 0; i--) 47 | { 48 | if (s[i] == end) 49 | { 50 | x = i; 51 | break; 52 | } 53 | } 54 | char c = s[0]; 55 | ll j = 0; 56 | for (ll i = 1; i <= x; i++) 57 | { 58 | if (c != s[i]) 59 | { 60 | j++; 61 | c = s[i]; 62 | } 63 | } 64 | if (j >= k) 65 | { 66 | cout << x + 1 << endl; 67 | } 68 | else 69 | cout << "-1\n"; 70 | } 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/BoilerPlateCode.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Problem Title 4 | 5 | ## Language: `language` 6 | 7 | ### Question Statement [link](link for the problem) 8 | 9 |   10 | 11 | ### **Question Statement** 12 | 13 | 14 | 15 | **Input** 16 | 17 | 18 | 19 | **Output** 20 | 21 | 22 | 23 | ### **Solution** 24 | 25 | ``` 26 | Code goes here 27 | ``` 28 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/MEXorMixup.md: -------------------------------------------------------------------------------- 1 | ## MEX or MixUp 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 |
✔️ C++
10 | 11 | 17 | 18 | ### 📖 Statement 19 | 20 | Alice gave Bob two integers a and b (a>0 and b≥0). Being a curious boy, Bob wrote down an array of non-negative integers with MEX value of all elements equal to a and XOR value of all elements equal to b. 21 | 22 | What is the shortest possible length of the array Bob wrote? 23 | 24 | Recall that the MEX (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the XOR of an array is the bitwise XOR of all the elements of the array. 25 | 26 | Input 27 | The input consists of multiple test cases. The first line contains an integer t (1≤t≤5⋅104) — the number of test cases. The description of the test cases follows. 28 | 29 | The only line of each test case contains two integers a and b (1≤a≤3⋅105; 0≤b≤3⋅105) — the MEX and XOR of the array, respectively. 30 | 31 | ### ✍️ Solution 32 | 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | typedef long long ll; 38 | 39 | int main() 40 | { 41 | 42 | ll t; 43 | cin >> t; 44 | while (t--) 45 | { 46 | ll a, b; //b expected xor 47 | cin >> a >> b; 48 | ll res = 0; 49 | ll ans = a; 50 | for (int i = 1; i < a; i++) 51 | { 52 | res = res ^ i; 53 | } 54 | ll n = res ^ b; //the num n^b=res 55 | if (res == b) //do nothing result and xor same 56 | { 57 | } 58 | else if (n == a) // cannot be included need two more numbers 59 | { 60 | a += 2; 61 | } 62 | else //if na we need to add one more number 63 | { 64 | a++; 65 | } 66 | 67 | cout << a << endl; 68 | } 69 | return 0; 70 | 71 | } 72 | 73 | ### 🤓 Explanation 74 | 75 | 1. We need to add all the numbers from 0 to a-1, to fulfill the mex condition(a elements). 76 | 2. We take xor of numbers from 0 to a-1 and store result in res 77 | 3. If res=b, then there is no need to add any other number return a(size of elements from 0 to n-1) 78 | 4. Now res!= b, we must add other element (n) to the list suct that xor of all elemns is b 79 | 5. So n xor res should be b 80 | 6. By definition n^res=b so b^res=n 81 | 7. If n== a, we can't add a in the set, we need two numbers whose xor is a, that can be added to the set, so return a+2 82 | 8. If n!=a we can add that number in the set, so resturn a+1 83 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/Palindromifier.md: -------------------------------------------------------------------------------- 1 | Question Statement : https://codeforces.com/contest/1421/problem/C 2 | 3 | Language used : Java 4 | 5 | # C. Palindromifier 6 | 7 | **Question Statement : 8 | 9 | Ringo found a string s of length n in his yellow submarine. 10 | The string contains only lowercase letters from the English alphabet. 11 | As Ringo and his friends love palindromes, he would like to turn the string s into a palindrome by applying two types of operations to the string. 12 | 13 | The first operation allows him to choose i (2≤i≤n−1) and to append the substring s2s3…si (i−1 characters) reversed to the front of s. 14 | 15 | The second operation allows him to choose i (2≤i≤n−1) and to append the substring sisi+1…sn−1 (n−i characters) reversed to the end of s. 16 | 17 | Note that characters in the string in this problem are indexed from 1. 18 | 19 | For example suppose s=abcdef. If he performs the first operation with i=3 then he appends cb to the front of s and the result will be cbabcdef. Performing the second operation on the resulted string with i=5 will yield cbabcdefedc. 20 | 21 | Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most 30 times. The length of the resulting palindrome must not exceed 106 22 | 23 | It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints. 24 | 25 | ## Input 26 | The only line contains the string S (3≤|s|≤105) of lowercase letters from the English alphabet. 27 | 28 | ## Output 29 | The first line should contain k (0≤k≤30) — the number of operations performed. 30 | 31 | Each of the following k lines should describe an operation in form L i or R i. 32 | L represents the first operation, R represents the second operation, i represents the index chosen. 33 | 34 | The length of the resulting palindrome must not exceed 106. 35 | 36 | 37 | ## Examples 38 | 1. 39 | > input 40 | abac 41 | 42 | > output 43 | 2 44 | R 2 45 | R 5 46 | 47 | 2. 48 | > input 49 | acccc 50 | 51 | > output 52 | 2 53 | L 4 54 | > L 2 55 | 56 | 3. 57 | >input 58 | hannah 59 | 60 | > output 61 | 0 62 | 63 | Note 64 | For the first example the following operations are performed: 65 | 66 | abac → abacab → abacaba 67 | 68 | The second sample performs the following operations: acccc → cccacccc → ccccacccc 69 | 70 | The third example is already a palindrome so no operations are required. 71 | 72 | 73 | ## Solution 74 | import java.util.Scanner; 75 | 76 | public class Palindromifier { 77 | 78 | public static void main(String[] args) { 79 | 80 | String s; 81 | Scanner in=new Scanner(System.in); 82 | s=in.next(); 83 | int n=s.length(); 84 | StringBuilder sb=new StringBuilder(s); 85 | if((sb.toString()).equals(sb.reverse().toString())){ 86 | System.out.println("0"); 87 | } 88 | else{ 89 | System.out.println("3"); 90 | System.out.println("R "+(n-1)); 91 | System.out.println("L "+n); 92 | System.out.println("L 2"); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/RelativelyPrimePairs.md: -------------------------------------------------------------------------------- 1 | 2 | # Relatively Prime Pairs 3 | 4 | ## Language: `C++` 5 | 6 | ### Question Statement. 7 | [link](https://codeforces.com/problemset/problem/1051/B?f0a28=1) 8 | 9 |   10 | 11 | ### **Question Statement** 12 | 13 | You are given a set of all integers from 𝑙 to 𝑟 inclusive, 𝑙<𝑟, (𝑟−𝑙+1)≤3⋅105 and (𝑟−𝑙) is always odd. 14 | 15 | You want to split these numbers into exactly 𝑟−𝑙+12 pairs in such a way that for each pair (𝑖,𝑗) the greatest common divisor of 𝑖 and 𝑗 is equal to 1. Each number should appear in exactly one of the pairs. 16 | 17 | Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them. 18 | 19 | **Input** 20 | 21 | The only line contains two integers 𝑙 and 𝑟 (1≤𝑙<𝑟≤1018, 𝑟−𝑙+1≤3⋅105, (𝑟−𝑙) is odd). 22 | 23 | 24 | **Output** 25 | 26 | If any solution exists, print "YES" in the first line. Each of the next 𝑟−𝑙+12 lines should contain some pair of integers. GCD of numbers in each pair should be equal to 1. All (𝑟−𝑙+1) numbers should be pairwise distinct and should have values from 𝑙 to 𝑟 inclusive. 27 | 28 | If there are multiple solutions, print any of them. 29 | 30 | If there exists no solution, print "NO". 31 | 32 | ### **Solution** 33 | 34 | ``` 35 | #include 36 | #include 37 | #include 38 | #include 39 | using namespace std; 40 | typedef long long ll; 41 | typedef unsigned long long ull; 42 | typedef long double ld; 43 | typedef vector vi; 44 | typedef pair ii; 45 | typedef vector vii; 46 | typedef vector vvi; 47 | typedef vector vvii; 48 | #define INF INT_MAX 49 | #define MOD 1000000007 50 | #define all(x) x.begin(), x.end() 51 | 52 | ll gcd(ll a, ll b) 53 | { 54 | 55 | if (b == 0) 56 | { 57 | return a; 58 | } 59 | else 60 | { 61 | return gcd(b, a % b); 62 | } 63 | } 64 | int main() 65 | { 66 | ios::sync_with_stdio(false); 67 | cin.tie(NULL); 68 | ll l, r; 69 | cin >> l >> r; 70 | vector even; 71 | vector odd; 72 | for (ll i = l; i <= r; i++) 73 | { 74 | if (i % 2 == 0) 75 | { 76 | even.push_back(i); 77 | } 78 | else 79 | { 80 | odd.push_back(i); 81 | } 82 | } 83 | int itr_odd = 0, itr_even = 0; 84 | map dict; 85 | while (itr_odd < (r - l + 1) / 2 && itr_even < (r - l + 1) / 2) 86 | { 87 | if (gcd(odd[itr_odd], even[itr_even]) == 1) 88 | { 89 | dict[odd[itr_odd]] = even[itr_even]; 90 | itr_odd++; 91 | itr_even++; 92 | } 93 | else 94 | { 95 | itr_even++; 96 | } 97 | if (itr_odd == (l - r - 1) / 2 || itr_even == (l - r - 1) / 2) 98 | { 99 | break; 100 | } 101 | } 102 | cout << "YES" << endl; 103 | for (auto i : dict) 104 | { 105 | cout << i.first << " " << i.second << endl; 106 | } 107 | return 0; 108 | } 109 | 110 | ``` 111 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/doNotBeDistracted.md: -------------------------------------------------------------------------------- 1 | # Do not be distracted 2 | 3 | ## Language: `C++` 4 | 5 | ### Question Statement [link](https://codeforces.com/contest/1520/problem/A) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | olycarp has 26 tasks. Each task is designated by a capital letter of the Latin alphabet. 12 | 13 | The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task. 14 | 15 | Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice. 16 | 17 | For example, if Polycarp solved tasks in the following order: "DDBBCCCBBEZ", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: "BAB", "AABBCCDDEEBZZ" and "AAAAZAAAAA". 18 | 19 | If Polycarp solved the tasks as follows: "FFGZZZY", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: "BA", "AFFFCC" and "YYYYY". 20 | 21 | Help Polycarp find out if his teacher might be suspicious. 22 | 23 | **Input** 24 | The first line contains an integer t (1≤t≤1000). Then t test cases follow. 25 | 26 | The first line of each test case contains one integer n (1≤n≤50) — the number of days during which Polycarp solved tasks. 27 | 28 | The second line contains a string of length n, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks. 29 | 30 | **Output** 31 | For each test case output: 32 | 33 | "YES", if the teacher cannot be suspicious; 34 | "NO", otherwise. 35 | You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). 36 | 37 | ### **Solution** 38 | 39 | ```cpp 40 | #include 41 | using namespace std; 42 | 43 | int main() 44 | { 45 | int t,index=0, nindex = 0,n; 46 | 47 | cin>>t; 48 | string istr; 49 | for (int i=0; i>n; 51 | cin>>istr; 52 | bool lock=true; 53 | for(int j=0; j -1){ 56 | cout << "NO" << endl; 57 | lock = false; 58 | break; 59 | } 60 | } 61 | if(lock){ 62 | cout << "YES" << endl; 63 | } 64 | } 65 | 66 | return 0; 67 | } 68 | ``` 69 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/maximumControl(easy).md: -------------------------------------------------------------------------------- 1 | # Maximum Control (easy) 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](https://codeforces.com/contest/958/problem/B1) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them. 12 | 13 | The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel. 14 | 15 | How much work is there left to be done: that is, how many remote planets are there? 16 | 17 | **Input** 18 | The first line of the input contains an integer N (2 ≤ N ≤ 1000) – the number of planets in the galaxy. 19 | 20 | The next N - 1 lines describe the hyperspace tunnels between the planets. Each of the N - 1 lines contains two space-separated integers u and v (1 ≤ u, v ≤ N) indicating that there is a bidirectional hyperspace tunnel between the planets u and v. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets. 21 | 22 | **Output** 23 | A single integer denoting the number of remote planets. 24 | 25 | ### **Solution** 26 | 27 | ```cpp 28 | #include 29 | using namespace std; 30 | 31 | int main() 32 | { 33 | int t,res = 0; 34 | cin>>t; 35 | vector vals; 36 | for(int i=0; i>a>>b; 39 | vals.push_back(a); 40 | vals.push_back(b); 41 | } 42 | for(int j=0; j 36 | using namespace std; 37 | 38 | int main() 39 | { 40 | int t; 41 | cin>>t; 42 | for(int i=0; i>A>>B; 46 | if(B == 2){ 47 | B = 2*B; 48 | } 49 | if(B == 1){ 50 | cout << "NO" << endl; 51 | s = false; 52 | } 53 | if(s){ 54 | cout << "YES" << endl; 55 | cout << setprecision(19) << A << " " << A*(B-1) << " " << A+(A*(B-1)) << endl; 56 | } 57 | } 58 | return 0; 59 | } 60 | ``` 61 | -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/problemAboutEquation.md: -------------------------------------------------------------------------------- 1 | # Problem About Equation 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](https://codeforces.com/contest/174/problem/A) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | A group of n merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the n mugs must be the same. 12 | 13 | Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has a1 milliliters of the drink, the second one has a2 milliliters and so on. The bottle has b milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled. 14 | 15 | Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously: 16 | 17 | - there were b milliliters poured in total. That is, the bottle need to be emptied; 18 | - after the process is over, the volumes of the drink in the mugs should be equal. 19 | 20 | **Input** 21 | The first line contains a pair of integers n, b (2 ≤ n ≤ 100, 1 ≤ b ≤ 100), where n is the total number of friends in the group and b is the current volume of drink in the bottle. The second line contains a sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the current volume of drink in the i-th mug. 22 | 23 | **Output** 24 | Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print n float numbers c1, c2, ..., cn, where ci is the volume of the drink to add in the i-th mug. Print the numbers with no less than 6 digits after the decimal point, print each ci on a single line. Polycarpus proved that if a solution exists then it is unique. 25 | 26 | Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma. 27 | 28 | ### **Solution** 29 | 30 | ```cpp 31 | #include 32 | using namespace std; 33 | 34 | int main() 35 | { 36 | double n,b, sum = 0; 37 | 38 | cin>>n>>b; 39 | double arr[(int)n]; 40 | for(int i = 0; i>arr[i]; 42 | sum= sum+arr[i]; 43 | } 44 | int cond=0; 45 | for(int j=0; j= 0){ 47 | cond+=1; 48 | } 49 | } 50 | if(cond == n){ 51 | for(int k=0; k 47 | using namespace std; 48 | #define ll long long int 49 | #define MOD 1000000007 50 | #define FastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 51 | 52 | void solve() { 53 | ll hatt, hdef, n; 54 | cin >> hatt >> hdef >> n; 55 | vector att(n); 56 | vector def(n); 57 | for (int i = 0 ; i < n ; i++) cin >> att[i]; 58 | for (int i = 0 ; i < n ; i++) cin >> def[i]; 59 | ll damage = 0; 60 | for (int i = 0 ; i < n ; i++) { 61 | damage += ((def[i] + hatt - 1) / hatt) * att[i]; 62 | } 63 | for (int i = 0 ; i < n ; i++) { 64 | if (hdef - (damage - att[i]) > 0) { 65 | cout << "YES\n"; 66 | return; 67 | } 68 | } 69 | cout << "NO\n"; 70 | } 71 | 72 | int main() { 73 | FastIO 74 | int tests; 75 | cin >> tests; 76 | while (tests--) { 77 | solve(); 78 | } 79 | return 0; 80 | } 81 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Codeforces/trwoRegularPolygons.md: -------------------------------------------------------------------------------- 1 | # Two Regular Polygons 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](https://codeforces.com/contest/1312/problem/A) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | (_note :_ This question is followed by some figures. For detailed question click on the above link) \ 12 | You are given two integers n and m (m < n). Consider a convex regular polygon of n vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). 13 | 14 | Your task is to say if it is possible to build another convex regular polygon with m vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon. 15 | 16 | You have to answer t independent test cases. 17 | 18 | **Input** 19 | The first line of the input contains one integer t (1≤t≤104) — the number of test cases. 20 | 21 | The next t lines describe test cases. Each test case is given as two space-separated integers n and m (3≤m< n ≤ 100) — the number of vertices in the initial polygon and the number of vertices in the polygon you want to build. 22 | 23 | **Output** 24 | For each test case, print the answer — "YES" (without quotes), if it is possible to build another convex regular polygon with m vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and "NO" otherwise. 25 | 26 | ### **Solution** 27 | 28 | ```cpp 29 | #include 30 | 31 | using namespace std; 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | for(int i=0; i>outer>>inner; 40 | if(outer%inner == 0){ 41 | cout << "YES" << endl; 42 | } 43 | else{ 44 | cout << "NO" << endl; 45 | } 46 | } 47 | return 0; 48 | } 49 | ``` 50 | -------------------------------------------------------------------------------- /Assets/Codes/Hackerrank/Printing Pattern Using Loops.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python Java✔️ C Other
14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Print a pattern of numbers from 1 to n as shown below. Each of the numbers is separated by a single space. 25 | 26 | 4 4 4 4 4 4 4 27 | 4 3 3 3 3 3 4 28 | 4 3 2 2 2 3 4 29 | 4 3 2 1 2 3 4 30 | 4 3 2 2 2 3 4 31 | 4 3 3 3 3 3 4 32 | 4 4 4 4 4 4 4 33 | Input Format 34 | 35 | The input will contain a single integer n. 36 | 37 | Constraints 38 | 39 | 40 | Sample Input 0 41 | 42 | 2 43 | 44 | Sample Output 0 45 | 46 | 2 2 2 47 | 2 1 2 48 | 2 2 2 49 | 50 | Sample Input 1 51 | 52 | 5 53 | 54 | Sample Output 1 55 | 56 | 5 5 5 5 5 5 5 5 5 57 | 5 4 4 4 4 4 4 4 5 58 | 5 4 3 3 3 3 3 4 5 59 | 5 4 3 2 2 2 3 4 5 60 | 5 4 3 2 1 2 3 4 5 61 | 5 4 3 2 2 2 3 4 5 62 | 5 4 3 3 3 3 3 4 5 63 | 5 4 4 4 4 4 4 4 5 64 | 5 5 5 5 5 5 5 5 5 65 | 66 | 67 | Sample Input 2 68 | 69 | 7 70 | 71 | Sample Output 2 72 | 73 | 7 7 7 7 7 7 7 7 7 7 7 7 7 74 | 7 6 6 6 6 6 6 6 6 6 6 6 7 75 | 7 6 5 5 5 5 5 5 5 5 5 6 7 76 | 7 6 5 4 4 4 4 4 4 4 5 6 7 77 | 7 6 5 4 3 3 3 3 3 4 5 6 7 78 | 7 6 5 4 3 2 2 2 3 4 5 6 7 79 | 7 6 5 4 3 2 1 2 3 4 5 6 7 80 | 7 6 5 4 3 2 2 2 3 4 5 6 7 81 | 7 6 5 4 3 3 3 3 3 4 5 6 7 82 | 7 6 5 4 4 4 4 4 4 4 5 6 7 83 | 7 6 5 5 5 5 5 5 5 5 5 6 7 84 | 7 6 6 6 6 6 6 6 6 6 6 6 7 85 | 7 7 7 7 7 7 7 7 7 7 7 7 7 86 | 87 | ### ✍️ Solution 88 | 89 | #include 90 | #include 91 | #include 92 | #include 93 | 94 | int main() 95 | { 96 | 97 | int n; 98 | scanf("%d", &n); 99 | // Complete the code to print the pattern. 100 | int len = 2*n-1; 101 | int min1,min2,min; 102 | // Complete the code to print the pattern. 103 | // for rows 104 | for (int i=1; i <=len; i++) { 105 | // for col 106 | for (int j=1; j<=len; j++) { 107 | // min diff btn vertical sides 108 | min1 = i<=len-i ? i -1 : len-i; 109 | // min diff btn horizontal sides 110 | min2 = j<=len-j ? j -1: len-j; 111 | // min diff btn vertical & horizontal sides 112 | min = min1<=min2 ? min1 : min2; 113 | // print 114 | printf("%d ",n-min); 115 | } 116 | printf("\n"); 117 | } 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /Assets/Codes/Hackerrank/Small Triangles, Large Triangles.md: -------------------------------------------------------------------------------- 1 | # Small Triangles, Large Triangles 2 | 3 | ## Language: `C` 4 | 5 | ### Question Statement [https://www.hackerrank.com/challenges/small-triangles-large-triangles/problem] 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | You are given n triangles, specifically, their sides a, b and c. Print them in the same style but sorted by their areas from the smallest one to the largest one. It is guaranteed that all the areas are different. 12 | 13 | The best way to calculate a area of a triangle with sides a, b and c is Heron's formula. 14 | 15 | Input Format 16 | 17 | The first line of each test file contains a single integer n. n lines follow with three space-separated integers, a, b and c. 18 | 19 | Output Format 20 | 21 | Print exactly n lines. On each line print 3 space-separated integers, the a, b and c of the corresponding triangle. 22 | 23 | Sample Input 0 24 | 25 | 3
26 | 7 24 25
27 | 5 12 13
28 | 3 4 5
29 | 30 | Sample Output 0 31 | 32 | 3 4 5
33 | 5 12 13
34 | 7 24 25
35 | 36 | Explanation 0 37 | 38 | The square of the first triangle is 84. The square of the second triangle is 30. The square of the third triangle is 6. So the sorted order is the reverse one. 39 | ### **Solution** 40 | 41 | #include 42 | #include 43 | #include 44 | 45 | struct triangle 46 | { 47 | int a; 48 | int b; 49 | int c; 50 | }; 51 | 52 | typedef struct triangle triangle; 53 | 54 | void swap(triangle* a,triangle* b) 55 | { 56 | triangle temp=*a; 57 | *a=*b; 58 | *b=temp; 59 | } 60 | int cmp(triangle tr) 61 | { 62 | int a=tr.a; 63 | int b=tr.b; 64 | int c=tr.c; 65 | return (a+b+c)*(a-b+c)*(a+b-c)*(-a+b+c); 66 | } 67 | void sort_by_area(triangle* tr, int n) { 68 | /** 69 | * Sort an array a of the length n 70 | */ 71 | for(int i=0;icmp(tr[j+1])) 77 | swap(&tr[j],&tr[j+1]); 78 | swapped=1; 79 | } 80 | if(swapped==0) 81 | break; 82 | } 83 | 84 | } 85 | 86 | 87 | int main() 88 | { 89 | int n; 90 | scanf("%d", &n); 91 | triangle *tr = malloc(n * sizeof(triangle)); 92 | for (int i = 0; i < n; i++) { 93 | scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c); 94 | } 95 | sort_by_area(tr, n); 96 | for (int i = 0; i < n; i++) { 97 | printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c); 98 | } 99 | return 0; 100 | } 101 | 102 | ```C Language 103 | 104 | 105 | ``` 106 | -------------------------------------------------------------------------------- /Assets/Codes/Hackerrank/greedyFlorist.md: -------------------------------------------------------------------------------- 1 | # Greedy Florist 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](https://www.hackerrank.com/challenges/greedy-florist/problem) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | A group of friends want to buy a bouquet of flowers. The florist wants to maximize his number of new customers and the money he makes. To do this, he decides he'll multiply the price of each flower by the number of that customer's previously purchased flowers plus . The first flower will be original price, , the next will be and so on. 12 | 13 | Given the size of the group of friends, the number of flowers they want to purchase and the original prices of the flowers, determine the minimum cost to purchase all of the flowers. The number of flowers they want equals the length of the array. 14 | 15 | Example 16 | 17 | The length of , so they want to buy flowers total. Each will buy one of the flowers priced at the original price. Having each purchased flower, the first flower in the list, , will now cost . The total cost is . 18 | 19 | Function Description 20 | 21 | Complete the getMinimumCost function in the editor below. 22 | 23 | getMinimumCost has the following parameter(s): 24 | 25 | int c[n]: the original price of each flower 26 | int k: the number of friends 27 | 28 | **Input** 29 | The first line contains two space-separated integers and , the number of flowers and the number of friends. 30 | The second line contains space-separated positive integers , the original price of each flower. 31 | 32 | **Output** 33 | int: the minimum cost to purchase all flowers 34 | 35 | ### **Solution** 36 | 37 | ```cpp 38 | #include 39 | 40 | using namespace std; 41 | 42 | vector split_string(string); 43 | 44 | // Complete the getMinimumCost function below. 45 | int getMinimumCost(int k, vector c) { 46 | sort(c.begin(),c.end()); 47 | int n=0, sum=0; 48 | 49 | for(int i=1; i<=c.size(); i++) 50 | { 51 | if((i-1)%k == 0) 52 | { 53 | n++; 54 | } 55 | 56 | if(i-1 == 0) 57 | { 58 | n--; 59 | } 60 | 61 | sum = sum + (n+1)*c[c.size()-i]; 62 | 63 | } 64 | 65 | return sum; 66 | } 67 | 68 | int main() 69 | { 70 | ofstream fout(getenv("OUTPUT_PATH")); 71 | 72 | string nk_temp; 73 | getline(cin, nk_temp); 74 | 75 | vector nk = split_string(nk_temp); 76 | 77 | int n = stoi(nk[0]); 78 | 79 | int k = stoi(nk[1]); 80 | 81 | string c_temp_temp; 82 | getline(cin, c_temp_temp); 83 | 84 | vector c_temp = split_string(c_temp_temp); 85 | 86 | vector c(n); 87 | 88 | for (int i = 0; i < n; i++) { 89 | int c_item = stoi(c_temp[i]); 90 | 91 | c[i] = c_item; 92 | } 93 | 94 | int minimumCost = getMinimumCost(k, c); 95 | 96 | fout << minimumCost << "\n"; 97 | 98 | fout.close(); 99 | 100 | return 0; 101 | } 102 | 103 | vector split_string(string input_string) { 104 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 105 | return x == y and x == ' '; 106 | }); 107 | 108 | input_string.erase(new_end, input_string.end()); 109 | 110 | while (input_string[input_string.length() - 1] == ' ') { 111 | input_string.pop_back(); 112 | } 113 | 114 | vector splits; 115 | char delimiter = ' '; 116 | 117 | size_t i = 0; 118 | size_t pos = input_string.find(delimiter); 119 | 120 | while (pos != string::npos) { 121 | splits.push_back(input_string.substr(i, pos - i)); 122 | 123 | i = pos + 1; 124 | pos = input_string.find(delimiter, i); 125 | } 126 | 127 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 128 | 129 | return splits; 130 | } 131 | 132 | ``` 133 | -------------------------------------------------------------------------------- /Assets/Codes/Hackerrank/maxMin.md: -------------------------------------------------------------------------------- 1 | # Max Min 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](link for the problem) 6 | 7 |   8 | 9 | ### **Question Statement** 10 | 11 | You will be given a list of integers, , and a single integer . You must create an array of length from elements of such that its unfairness is minimized. Call that array . Unfairness of an array is calculated as 12 | 13 | Where: 14 | 15 | - max denotes the largest integer in 16 | - min denotes the smallest integer in 17 | 18 | Example 19 | 20 | Pick any two elements, say . 21 | 22 | Testing for all pairs, the solution provides the minimum unfairness. 23 | 24 | Note: Integers in may not be unique. 25 | 26 | Function Description 27 | 28 | Complete the maxMin function in the editor below. 29 | maxMin has the following parameter(s): 30 | 31 | int k: the number of elements to select 32 | int arr[n]:: an array of integers 33 | 34 | **Input** 35 | The first line contains an integer , the number of elements in array . 36 | The second line contains an integer . 37 | Each of the next lines contains an integer where . 38 | 39 | **Output** 40 | int: the minimum possible unfairness 41 | 42 | ### **Solution** 43 | 44 | ```cpp 45 | #include 46 | #include 47 | 48 | using namespace std; 49 | 50 | // Complete the maxMin function below. 51 | int maxMin(int k, vector arr) { 52 | vector result; 53 | sort(arr.begin(),arr.end()); 54 | 55 | for(int i=0; i> n; 71 | cin.ignore(numeric_limits::max(), '\n'); 72 | 73 | int k; 74 | cin >> k; 75 | cin.ignore(numeric_limits::max(), '\n'); 76 | 77 | vector arr(n); 78 | 79 | for (int i = 0; i < n; i++) { 80 | int arr_item; 81 | cin >> arr_item; 82 | cin.ignore(numeric_limits::max(), '\n'); 83 | 84 | arr[i] = arr_item; 85 | } 86 | 87 | int result = maxMin(k, arr); 88 | 89 | fout << result << "\n"; 90 | 91 | fout.close(); 92 | 93 | return 0; 94 | } 95 | 96 | ``` 97 | -------------------------------------------------------------------------------- /Assets/Codes/Hackerrank/numberLineJumps.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Problem Title 4 | 5 | ## Language: `language` 6 | 7 | ### Question Statement [link](https://www.hackerrank.com/challenges/kangaroo/problem) 8 | 9 |   10 | 11 | ### **Question Statement** 12 | 13 | You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity). 14 | 15 | The first kangaroo starts at location and moves at a rate of meters per jump. 16 | The second kangaroo starts at location and moves at a rate of meters per jump. 17 | You have to figure out a way to get both kangaroos at the same location at the same time as part of the show. If it is possible, return YES, otherwise return NO. 18 | 19 | Example 20 | 21 | After one jump, they are both at , (, ), so the answer is YES. 22 | 23 | Function Description 24 | 25 | Complete the function kangaroo in the editor below. 26 | 27 | kangaroo has the following parameter(s): 28 | 29 | int x1, int v1: starting position and jump distance for kangaroo 1 30 | int x2, int v2: starting position and jump distance for kangaroo 2 31 | 32 | **Input** 33 | A single line of four space-separated integers denoting the respective values of x1, v1, x2, and v2. 34 | 35 | **Output** 36 | string: either YES or NO 37 | 38 | ### **Solution** 39 | 40 | ```cpp 41 | #include 42 | 43 | using namespace std; 44 | 45 | vector split_string(string); 46 | 47 | // Complete the kangaroo function below. 48 | string kangaroo(int x1, int v1, int x2, int v2) { 49 | string answer; 50 | int v12 = v1-v2, x12 = x1-x2; 51 | if(v12<=0 && x12<0) 52 | { 53 | answer = "NO"; 54 | } 55 | else if(v12>=0 && x12>0) 56 | { 57 | answer = "NO"; 58 | } 59 | else 60 | { 61 | if(x12 % v12 != 0) 62 | { 63 | answer = "NO"; 64 | } 65 | else 66 | { 67 | answer = "YES"; 68 | } 69 | } 70 | 71 | return answer; 72 | 73 | 74 | 75 | } 76 | 77 | int main() 78 | { 79 | ofstream fout(getenv("OUTPUT_PATH")); 80 | 81 | string x1V1X2V2_temp; 82 | getline(cin, x1V1X2V2_temp); 83 | 84 | vector x1V1X2V2 = split_string(x1V1X2V2_temp); 85 | 86 | int x1 = stoi(x1V1X2V2[0]); 87 | 88 | int v1 = stoi(x1V1X2V2[1]); 89 | 90 | int x2 = stoi(x1V1X2V2[2]); 91 | 92 | int v2 = stoi(x1V1X2V2[3]); 93 | 94 | string result = kangaroo(x1, v1, x2, v2); 95 | 96 | fout << result << "\n"; 97 | 98 | fout.close(); 99 | 100 | return 0; 101 | } 102 | 103 | vector split_string(string input_string) { 104 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 105 | return x == y and x == ' '; 106 | }); 107 | 108 | input_string.erase(new_end, input_string.end()); 109 | 110 | while (input_string[input_string.length() - 1] == ' ') { 111 | input_string.pop_back(); 112 | } 113 | 114 | vector splits; 115 | char delimiter = ' '; 116 | 117 | size_t i = 0; 118 | size_t pos = input_string.find(delimiter); 119 | 120 | while (pos != string::npos) { 121 | splits.push_back(input_string.substr(i, pos - i)); 122 | 123 | i = pos + 1; 124 | pos = input_string.find(delimiter, i); 125 | } 126 | 127 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 128 | 129 | return splits; 130 | } 131 | 132 | ``` 133 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/BestTimeToBuyAndSellStock.md: -------------------------------------------------------------------------------- 1 | ## Best Time To Buy And Sell Stock 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++✔️ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) 18 | 19 | ### 📖 Statement 20 | 21 | You are given an array prices where prices[i] is the price of a given stock on the ith day. 22 | 23 | You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 24 | 25 | Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. 26 | 27 | 28 | ### ✍️ Solution 29 | 30 | ```Python 31 | class Solution: 32 | def maxProfit(self, prices: List[int]) -> int: 33 | left = 0 34 | right = left+1 35 | buy= 10**5 36 | sell = -1 37 | maxprof = 0 38 | while right 6 | 7 | ✔️ C++ 8 | Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Bitwise AND of Numbers Range 25 | 26 | ### ✍️ Solution 27 | ``` 28 | class Solution { 29 | public: 30 | int rangeBitwiseAnd(int m, int n) { 31 | int count=0; 32 | while(m != n){ 33 | m>>=1; 34 | n>>=1; 35 | count++; 36 | } 37 | return m< 6 | 7 | ✔️ C++ 8 | Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | You are climbing a staircase. It takes n steps to reach the top. 25 | 26 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 27 | 28 | ### ✍️ Solution 29 | 30 | class Solution { 31 | public: 32 | int climbStairs(int n) { 33 | if(n==1) 34 | return 1; 35 | if(n==2) 36 | return 2; 37 | int a=1; 38 | int b=2; 39 | for(int i=3;i<=n;i++) 40 | { 41 | int c=a+b; 42 | a=b; 43 | b=c; 44 | } 45 | 46 | return b; 47 | } 48 | 49 | }; 50 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/CointainerWithMostWater.md: -------------------------------------------------------------------------------- 1 | ## Container With Most Water 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++✔️ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/container-with-most-water/) 18 | 19 | ### 📖 Statement 20 | 21 | Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. 22 | 23 | **Notice** that you may not slant the container. 24 | 25 | ### ✍️ Solution 26 | 27 | ```Python 28 | class Solution: 29 | def maxArea(self, H: List[int]) -> int: 30 | ans, i, j = 0, 0, len(H)-1 31 | while (i < j): 32 | if H[i] <= H[j]: 33 | res = H[i] * (j - i) 34 | i += 1 35 | else: 36 | res = H[j] * (j - i) 37 | j -= 1 38 | if res > ans: 39 | ans = res 40 | return ans 41 | ``` 42 | 43 | ### 🤓 Explanation 44 | 45 | 46 | We have to maximize the area between two vertical line. The area is calculated by considering the length of the shorter line and the breadth as the difference between both the lines. We use `two-pointers` method to solve this problem. WE take one pointer at the beginning of the array and other at the end. We compare the heights between these two points and move the pointer towards the right or left of the line whose height is smaller. At every iterations we calculate the area between both these pointers and save the maximum area between them. 47 | 48 | 49 | **Time Complexity**: O(n) -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/FindSmallestLetterGreaterThanTarget.md: -------------------------------------------------------------------------------- 1 | ## Find Smallest Letter Greater Than Target 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python✔️ Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/find-smallest-letter-greater-than-target/) 18 | 19 | ### 📖 Statement 20 | 21 | Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target. 22 | 23 | Note that the letters wrap around. 24 | 25 | For example, if target == `'z'` and letters == `['a', 'b']`, the answer is `'a'`. 26 | 27 | 28 | ### Example 29 | ```c++ 30 | Example 1: 31 | 32 | Input: letters = ["c","f","j"], target = "a" 33 | Output: "c" 34 | Example 2: 35 | 36 | Input: letters = ["c","f","j"], target = "c" 37 | Output: "f" 38 | ``` 39 | 40 | ### ✍️ Solution 41 | 42 | ```Java 43 | class Solution { 44 | public char nextGreatestLetter(char[] letters, char target) { 45 | 46 | int start = 0; 47 | int end = letters.length - 1; 48 | 49 | while(start <= end){ 50 | 51 | int mid = (start + end)/2; 52 | //if target element is less than middle element 53 | if(target < letters[mid] ){ 54 | end = mid -1; 55 | } 56 | //target element is greater than mid element 57 | else { 58 | start = mid +1; 59 | } 60 | 61 | } 62 | return letters[start%letters.length]; 63 | 64 | } 65 | 66 | 67 | } 68 | ``` 69 | 70 | **Time Complexity**: `O(log n)` -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/HouseRobberIII.md: -------------------------------------------------------------------------------- 1 | ## House Robber III 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | ### Question Platform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/house-robber-iii/) 18 | 19 | ### 📖 Statement 20 | 21 | The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night. 22 | Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police. 23 | 24 | 25 | 26 | ### ✍️ Solution 27 | 28 | ```C++ 29 | class Solution { 30 | public: 31 | pairdfs(TreeNode *root) 32 | { 33 | 34 | if(!root) return {0,0}; 35 | 36 | auto l= dfs(root->left); 37 | auto r= dfs(root->right); 38 | pairans; 39 | 40 | ans.first= max(l.first, l.second) + max(r.first, r.second); 41 | 42 | ans.second= root->val + l.first+ r.first; 43 | 44 | return ans; 45 | } 46 | int rob(TreeNode* root) { 47 | auto f= dfs(root); 48 | return max(f.first, f.second); 49 | } 50 | }; 51 | ``` 52 | 53 | **Time Complexity**: O(n) 54 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/IsGraphBipartite.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | Is Graph Bipartite? 3 | 4 | ### 💻 Language(s) 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
C++✔️ Java Python C Other
15 | 16 | ### Question Plaform 17 | 18 | ✔️[LeetCode](https://leetcode.com/problems/is-graph-bipartite/) 19 | 20 | ### 📖 Statement 21 | 22 | There is an undirected graph with n nodes, where each node is numbered between 0 and n - 1. You are given a 2D array graph, where graph[u] is an array of nodes that node u is adjacent to. More formally, for each v in graph[u], there is an undirected edge between node u and node v. The graph has the following properties: 23 | 24 | There are no self-edges (graph[u] does not contain u). 25 | There are no parallel edges (graph[u] does not contain duplicate values). 26 | If v is in graph[u], then u is in graph[v] (the graph is undirected). 27 | The graph may not be connected, meaning there may be two nodes u and v such that there is no path between them. 28 | A graph is bipartite if the nodes can be partitioned into two independent sets A and B such that every edge in the graph connects a node in set A and a node in set B. 29 | 30 | Return true if and only if it is bipartite 31 | 32 | 33 | ### ✍️ Solution 34 | 35 | ```Java 36 | class Solution { 37 | public boolean isBipartite(int[][] graph, int[] vis, int src) { 38 | LinkedList que = new LinkedList<>(); 39 | que.addLast(src); 40 | 41 | int color = 0; // red 42 | boolean isCycle = false; 43 | 44 | while (que.size() != 0) { 45 | int size = que.size(); 46 | while (size-- > 0) { 47 | int rvtx = que.removeFirst(); 48 | if (vis[rvtx] != -1) { 49 | isCycle = true; 50 | if (vis[rvtx] != color) 51 | return false; 52 | 53 | continue; 54 | } 55 | 56 | vis[rvtx] = color; 57 | for (int v : graph[rvtx]) { 58 | if (vis[v] == -1) { 59 | que.addLast(v); 60 | } 61 | } 62 | } 63 | color = (color + 1) % 2; 64 | } 65 | return true; 66 | } 67 | 68 | public boolean isBipartite(int[][] graph) { 69 | int n = graph.length; 70 | int[] vis = new int[n]; 71 | Arrays.fill(vis, -1); 72 | 73 | for (int i = 0; i < n; i++) { 74 | if (vis[i] == -1 && !isBipartite(graph, vis, i)) 75 | return false; 76 | } 77 | return true; 78 | } 79 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/KthLargestElementInAnArray.md: -------------------------------------------------------------------------------- 1 | ## Kth Largest Element in an Array 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python✔️ Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/kth-largest-element-in-an-array/) 18 | 19 | ### 📖 Statement 20 | 21 | Given an integer array nums and an integer k, return the kth largest element in the array. 22 | 23 | Note that it is the kth largest element in the sorted order, not the kth distinct element. 24 | 25 | ### ✍️ Solution 26 | 27 | ```Java 28 | class Solution { 29 | 30 | public void swap(int[] arr, int i, int j) { 31 | int temp = arr[i]; 32 | arr[i] = arr[j]; 33 | arr[j] = temp; 34 | } 35 | 36 | public void downHeapify(int[] arr, int pi, int li) { 37 | int maxIdx = pi; 38 | int lci = 2 * pi + 1; 39 | int rci = 2 * pi + 2; 40 | if (lci <= li && arr[lci] > arr[maxIdx]) { 41 | maxIdx = lci; 42 | } 43 | if (rci <= li && arr[rci] > arr[maxIdx]) { 44 | maxIdx = rci; 45 | } 46 | if (maxIdx != pi) { 47 | swap(arr, maxIdx, pi); 48 | downHeapify(arr, maxIdx, li); 49 | } 50 | } 51 | 52 | public int findKthLargest(int[] nums, int k) { 53 | int li = nums.length - 1; 54 | for (int i = li; i >= 0; i--) { 55 | downHeapify(nums, i, li); 56 | } 57 | while(k-- > 1) { 58 | swap(nums, 0, li--); 59 | downHeapify(nums, 0, li); 60 | } 61 | return nums[0]; 62 | } 63 | } 64 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/LongestPalindromicSubsequence.md: -------------------------------------------------------------------------------- 1 | ## Longest Palindromic Subsequence 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python✔️ Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/longest-palindromic-subsequence/) 18 | 19 | ### 📖 Statement 20 | 21 | Given a string s, find the longest palindromic subsequence's length in s. 22 | 23 | A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. 24 | 25 | 26 | ### ✍️ Solution 27 | 28 | ```Java 29 | public int longestPalindromeSubseq(String s) { 30 | int n = s.length(); 31 | int [][] dp = new int[n][n]; 32 | for(int gap = 0; gap < n; gap++){ 33 | for(int i = 0, j = gap; i < n && j < n; i++, j++){ 34 | if(gap == 0) dp[i][j] = 1; 35 | else{ 36 | char ch1 = s.charAt(i); 37 | char ch2 = s.charAt(j); 38 | if(ch1 == ch2){ 39 | dp[i][j] = dp[i+1][j-1]+2; 40 | }else{ 41 | dp[i][j] = Math.max(dp[i][j-1], dp[i+1][j]); 42 | } 43 | } 44 | } 45 | } 46 | 47 | 48 | return dp[0][n-1]; 49 | } 50 | ``` 51 | 52 | **Time Complexity**: O(n^2) 53 | **Space Complexity**: O(n^2) -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/LongestPalindromicSubstring.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Given a string s, return the longest palindromic substring in s. 25 | 26 | ### ✍️ Solution 27 | 28 | ``` 29 | class Solution { 30 | public: 31 | 32 | 33 | int t[1001][1001]; 34 | string lcs(string a, string b, int n){ 35 | int maxi=0; 36 | string res; 37 | vector v; 38 | for(int i=0;i<=n;i++){ 39 | for(int j=0;j<=n;j++){ 40 | if(i==0 || j==0) t[i][j]; 41 | else if(a[i-1]==b[j-1]){ 42 | t[i][j] = 1+t[i-1][j-1]; 43 | if(t[i][j]>maxi){ 44 | string temp = a.substr(i-t[i][j], t[i][j]); 45 | string rev = string(temp.rbegin(),temp.rend()); 46 | if(rev==temp){ 47 | maxi = t[i][j]; 48 | res = temp; 49 | } 50 | } 51 | }else{ 52 | t[i][j] = 0; 53 | } 54 | } 55 | } 56 | return res; 57 | 58 | } 59 | 60 | string longestPalindrome(string s) { 61 | string a = s; 62 | string str=""; 63 | int n = s.length(); 64 | reverse(s.begin(),s.end()); 65 | 66 | return lcs(a,s,n); 67 | 68 | 69 | } 70 | }; 71 | ``` 72 | 73 | ### 🤓 Explanation 74 | 75 | DP 76 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/LongestSubstringWithoutRepeatingCharacters.md: -------------------------------------------------------------------------------- 1 | ## Longest Substring Without Repeating Characters 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++✔️ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/longest-substring-without-repeating-characters/) 18 | 19 | ### 📖 Statement 20 | 21 | Given a string s, find the length of the longest substring without repeating characters. 22 | 23 | ### ✍️ Solution 24 | 25 | ```Python 26 | class Solution: 27 | def lengthOfLongestSubstring(self, s: str) -> int: 28 | left = 0 29 | right = 0 30 | ma = 0 31 | subset = set() 32 | while rightma: 43 | ma=right-left 44 | return ma 45 | ``` 46 | 47 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/LuckyNumbersInAMatrix.md: -------------------------------------------------------------------------------- 1 | 2 | Name: isPalindrome 3 | Language: Java 4 | Platform: Leetcode 5 | Question: 6 | 7 | https://leetcode.com/problems/lucky-numbers-in-a-matrix/ 8 | 9 | Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order. 10 | A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column. 11 | 12 | Example 1: 13 | 14 | Input: matrix = [[3,7,8],[9,11,13],[15,16,17]] 15 | Output: [15] 16 | Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column 17 | Example 2: 18 | 19 | Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]] 20 | Output: [12] 21 | Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column. 22 | Example 3: 23 | 24 | Input: matrix = [[7,8],[1,2]] 25 | Output: [7] 26 | Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column. 27 | Example 4: 28 | 29 | Input: matrix = [[3,6],[7,1],[5,2],[4,8]] 30 | Output: [] 31 | Explanation: There is no lucky number. 32 | 33 | class Solution { 34 | public List luckyNumbers (int[][] matrix) { 35 | 36 | ArrayList list = new ArrayList<>(); 37 | 38 | for (int i = 0; i < matrix.length; ++i) { 39 | 40 | int minCol = minInCol(matrix,i); 41 | int maxRow = matrix[i][minCol]; 42 | if (maxInRow(matrix,minCol,maxRow)) { 43 | list.add(maxRow); 44 | } 45 | } 46 | 47 | return list; 48 | } 49 | 50 | static int minInCol(int[][] matrix,int i){ 51 | 52 | int k = 0,minCol = matrix[i][k]; 53 | 54 | for (int j = 1; j < matrix[i].length; ++j) { 55 | 56 | if (matrix[i][j] < minCol) { 57 | minCol = matrix[i][j]; 58 | k = j; 59 | } 60 | } 61 | 62 | return k; 63 | } 64 | 65 | static boolean maxInRow(int[][] matrix,int minCol,int maxRow){ 66 | 67 | for (int i = 0; i < matrix.length; ++i) { 68 | if (matrix[i][minCol] > maxRow) { 69 | return false; 70 | } 71 | } 72 | 73 | return true; 74 | } 75 | } -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/MajorityElement.md: -------------------------------------------------------------------------------- 1 | ## Majority Element 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/majority-element/) 18 | 19 | ### 📖 Problem Statement 20 | 21 | Given an array nums of size n, return the majority element. 22 | 23 | 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. 24 | 25 | ### ✍️ Solution 26 | 27 | ```C++ 28 | class Solution { 29 | public: 30 | int countInRange(vector &nums, int num, int lo, int hi){ 31 | int count= 0; 32 | for(int i = lo;i<=hi;i++){ 33 | if(nums[i]==num){ 34 | count++; 35 | } 36 | } 37 | return count; 38 | } 39 | int majorityElementRec(vector &nums,int lo, int hi){ 40 | if(lo==hi){ 41 | return nums[lo]; 42 | } 43 | 44 | int mid = (hi-lo)/2 + lo; 45 | int left = majorityElementRec(nums,lo,mid); 46 | int right = majorityElementRec(nums, mid+1, hi); 47 | 48 | if(left==right){ 49 | return left; 50 | } 51 | 52 | int leftCount = countInRange(nums, left,lo,hi); 53 | int rightCount = countInRange(nums, right, lo,hi); 54 | return leftCount > rightCount ? left : right; 55 | } 56 | int majorityElement(vector& nums) { 57 | return majorityElementRec(nums, 0, nums.size()-1); 58 | } 59 | }; 60 | ``` 61 | 62 | ### Explaination 63 | We use Divide and Conquer approach to solve this problem. We take note of two indices lo and hi to pass the slice of array.If we know the majority element in the left and right halves of array, we can find the majority element
64 | 65 | Here, we apply a classical divide & conquer approach that recurses on the left and right halves of an array until an answer can be trivially achieved for a length-1 array. Note that because actually passing copies of subarrays costs time and space, we instead pass lo and hi indices that describe the relevant slice of the overall array. In this case, the majority element for a length-1 slice is trivially its only element, so the recursion stops there. If the current slice is longer than length-1, we must combine the answers for the slice's left and right halves. If they agree on the majority element, then the majority element for the overall slice is obviously the same[1]. If they disagree, only one of them can be "right", so we need to count the occurrences of the left and right majority elements to determine which subslice's answer is globally correct. 66 | 67 | **Time Complexity**: O(nlogn) where n is the length of string 68 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/MiddleOfLinkedList.md: -------------------------------------------------------------------------------- 1 | ## Middle of the Linked List 2 | 3 | ### 💻 Language(s)-C++ 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | ### Question Plaform-Leetcode 16 | Problem Link-https://leetcode.com/problems/middle-of-the-linked-list/ 17 | 18 | 19 | 20 | ### 📖 Statement 21 | 22 | Given the head of a singly linked list, return the middle node of the linked list. 23 | If there are two middle nodes, return the second middle node. 24 | 25 | ### ✍️ Solution 26 | 27 | 28 | class Solution { 29 | public: 30 | ListNode* middleNode(ListNode* head) { 31 | ListNode* slow = head; 32 | ListNode* fast = head; 33 | while (fast != NULL && fast->next != NULL) { 34 | slow = slow->next; 35 | fast = fast->next->next; 36 | } 37 | return slow; 38 | } 39 | }; 40 | 41 | ### 🤓 Explanation 42 | 43 | When traversing the list with a pointer slow, make another pointer fast that traverses twice as fast. When fast reaches the end of the list, 44 | slow must be in the middle. 45 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/MinimumMovesToEqualArrayElements.md: -------------------------------------------------------------------------------- 1 | ## Minimum moves to equal array elements 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/) 18 | 19 | ### 📖 Statement 20 | 21 | Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. 22 | 23 | In one move, you can increment n - 1 elements of the array by 1. 24 | ``` 25 | Example 1: 26 | 27 | Input: nums = [1,2,3] 28 | Output: 3 29 | 30 | Explanation: Only three moves are needed (remember each move increments two elements): 31 | [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] 32 | 33 | Example 2: 34 | 35 | Input: nums = [1,1,1] 36 | Output: 0 37 | ``` 38 | ### ✍️ Solution 39 | ``` 40 | class Solution { 41 | public: 42 | int minMoves(vector& nums) { 43 | int minimum_value = INT_MAX, sum_elements = 0, n = nums.size(); 44 | for(auto num : nums){ 45 | minimum_value = min(minimum_value, num); 46 | sum_elements += num; 47 | } 48 | int result = sum_elements - n*minimum_value; 49 | return result; 50 | } 51 | }; 52 | ``` 53 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/MoveZeroes.md: -------------------------------------------------------------------------------- 1 | ## Move Zeroes 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/move-zeroes/) 18 | 19 | ### 📖 Statement 20 | 21 | Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. 22 | 23 | Note that you must do this in-place without making a copy of the array. 24 | ``` 25 | Example 1: 26 | 27 | Input: nums = [0,1,0,3,12] 28 | Output: [1,3,12,0,0] 29 | 30 | Example 2: 31 | 32 | Input: nums = [0] 33 | Output: [0] 34 | ``` 35 | 36 | ### ✍️ Solution 37 | ``` 38 | class Solution { 39 | public: 40 | void moveZeroes(vector& nums) { 41 | int l = 0, r = 0, n = nums.size(); 42 | while(l < n && r < n){ 43 | if (nums[r] != 0){ 44 | swap(nums[l], nums[r]); 45 | l++; 46 | } 47 | r++; 48 | } 49 | } 50 | }; 51 | ``` 52 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/NextPermutation.md: -------------------------------------------------------------------------------- 1 | #### Language: JAVA 2 | 3 | #### Question Statement: 4 | Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. 5 | 6 | If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). 7 | 8 | The replacement must be in place and use only constant extra memory. 9 |
10 | 11 | #### Leetcode Link: 12 | https://leetcode.com/problems/next-permutation/ 13 | 14 | 15 | #### Solution: 16 | ``` 17 | public class Solution { 18 | public void nextPermutation(int[] nums) { 19 | int i = nums.length - 2; 20 | while (i >= 0 && nums[i + 1] <= nums[i]) { 21 | i--; 22 | } 23 | if (i >= 0) { 24 | int j = nums.length - 1; 25 | while (nums[j] <= nums[i]) { 26 | j--; 27 | } 28 | swap(nums, i, j); 29 | } 30 | reverse(nums, i + 1); 31 | } 32 | 33 | private void reverse(int[] nums, int start) { 34 | int i = start, j = nums.length - 1; 35 | while (i < j) { 36 | swap(nums, i, j); 37 | i++; 38 | j--; 39 | } 40 | } 41 | 42 | private void swap(int[] nums, int i, int j) { 43 | int temp = nums[i]; 44 | nums[i] = nums[j]; 45 | nums[j] = temp; 46 | } 47 | } 48 | ``` 49 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/ReshapeTheMatrix.md: -------------------------------------------------------------------------------- 1 | 2 | Name: Reshape the matrix 3 | Language: Java 4 | Platform: Leetcode 5 | Question: 6 | 7 | https://leetcode.com/problems/reshape-the-matrix/ 8 | 9 | In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data. 10 | You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix. 11 | The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were. 12 | If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix. 13 | 14 | Example 1: 15 | Input: mat = [[1,2],[3,4]], r = 1, c = 4 16 | Output: [[1,2,3,4]] 17 | Example 2: 18 | 19 | Input: mat = [[1,2],[3,4]], r = 2, c = 4 20 | Output: [[1,2],[3,4]] */ 21 | 22 | class Solution { 23 | public int[][] matrixReshape(int[][] mat, int r, int c) { 24 | 25 | int row = mat.length,col = mat[0].length,size = row * col; 26 | 27 | if(size != r*c || row == r) return mat; 28 | 29 | int[][] ans = new int[r][c]; 30 | 31 | int r1 = 0,c1 = 0; 32 | 33 | for(int i =0;i 6 | 7 | ✔️ C++ 8 | Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | ### Plaform - Leetcode 16 | 17 | Problem Link:https://leetcode.com/problems/reverse-linked-list/ 18 | 19 | 20 | ### 📖 Statement 21 | 22 | Given the head of a singly linked list, reverse the list, and return the reversed list. 23 | 24 | ### ✍️ Solution 25 | 26 | 27 | class Solution { 28 | public: 29 | 30 | ListNode* reverseList(ListNode* head) { 31 | 32 | if(head==NULL||head->next=NULL) 33 | return head; 34 | ListNode *curr=head,*prev=NULL,*next=head->next; 35 | 36 | while(curr!=NULL){ 37 | curr->next=prev; 38 | prev=curr; 39 | curr=next; 40 | if(next!=NULL){ 41 | next=next->next; 42 | } 43 | 44 | } 45 | return prev; 46 | } 47 | }; 48 | 49 | ### 🤓 Explanation 50 | 51 | Take 3 pointers curr,prev and next and run a iterative while loop till curr is not equal to NULL.Make the next of curr point to prev.Move prev and next one step forward.At conclusion ,the reversed linked list head will be at position where the prev is pointing that's why we return the prev pointer. 52 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/SearchInRotatedSortedArray.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Search in Rotated Sorted Array 25 | 26 | ### ✍️ Solution 27 | ``` 28 | class Solution { 29 | public: 30 | int search(vector& nums, int target) { 31 | int n = nums.size(); 32 | int l =0; 33 | int r= n-1; 34 | if(n==0){ 35 | if(nums[0]==target){ 36 | return 0; 37 | } 38 | return -1; 39 | 40 | } 41 | while(l<=r){ 42 | int mid = l+(r-l)/2; 43 | if(nums[mid] == target){ 44 | return mid; 45 | } 46 | if(nums[r]>nums[l]){ 47 | if(nums[mid]>target){ 48 | r=mid-1; 49 | }else{ 50 | l=mid+1; 51 | } 52 | }else if(nums[mid]>=nums[l]){ 53 | if(nums[mid]>target && nums[l]<=target){ 54 | r = mid-1; 55 | }else{ 56 | l = mid+1; 57 | } 58 | }else{ 59 | if(nums[mid]=target){ 60 | l=mid+1; 61 | }else{ 62 | r=mid-1; 63 | } 64 | } 65 | } 66 | return -1; 67 | } 68 | }; 69 | 70 | ``` 71 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/SearchInsertPosition.md: -------------------------------------------------------------------------------- 1 | ## Search Insert Position 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python✔️ Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/search-insert-position/) 18 | 19 | ### 📖 Statement 20 | 21 | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 22 | 23 | You must write an algorithm with `O(log n)` runtime complexity. 24 | 25 | 26 | ### Example 27 | ```language 28 | Example 1: 29 | 30 | Input: nums = [1,3,5,6], target = 5 31 | Output: 2 32 | Example 2: 33 | 34 | Input: nums = [1,3,5,6], target = 2 35 | Output: 1 36 | ``` 37 | 38 | ### ✍️ Solution 39 | 40 | ```Java 41 | class Solution { 42 | public int searchInsert(int[] nums, int target) { 43 | int start =0; 44 | int end = nums.length -1; 45 | while(start<=end){ 46 | int mid = (start+end)/2; 47 | if(target < nums[mid]){ 48 | end = mid - 1; 49 | } 50 | else if(target > nums[mid]){ 51 | start = mid + 1; 52 | } 53 | else{ 54 | return mid; 55 | } 56 | } 57 | return start; 58 | } 59 | } 60 | ``` 61 | 62 | **Time Complexity**: `O(log n)` 63 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/SetMatrixZeroes.md: -------------------------------------------------------------------------------- 1 | ## Set Matrix Zeroes 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++ Python✔️ Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/set-matrix-zeroes/) 18 | 19 | ### 📖 Statement 20 | 21 | Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's, and return the matrix. 22 | 23 | You must do it in place. 24 | 25 | 26 | ### ✍️ Solution 27 | 28 | ```Java 29 | public void setZeroes(int[][] matrix) { 30 | 31 | boolean fr = false, fc = false; 32 | 33 | for(int i = 0; i < matrix.length; i++){ 34 | for(int j = 0; j < matrix[0].length; j++){ 35 | if(matrix[i][j] == 0){ 36 | matrix[0][j] = 0; 37 | matrix[i][0] = 0; 38 | if(i == 0) fr = true; 39 | if(j == 0) fc = true; 40 | } 41 | } 42 | } 43 | 44 | for(int i = 1; i < matrix.length; i++){ 45 | for(int j = 1; j < matrix[0].length; j++){ 46 | if(matrix[0][j] == 0 || matrix[i][0] == 0){ 47 | matrix[i][j] = 0; 48 | } 49 | } 50 | } 51 | 52 | if(fc){ 53 | for(int i = 0; i < matrix.length; i++){ 54 | matrix[i][0] = 0; 55 | } 56 | } 57 | 58 | if(fr){ 59 | for(int j = 0; j < matrix[0].length; j++){ 60 | matrix[0][j] = 0; 61 | } 62 | } 63 | 64 | return; 65 | } 66 | ``` 67 | 68 | **Time Complexity**: O(n^2) 69 | **Space Complexity**: O(1) -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/SubDomainVisitCount.md: -------------------------------------------------------------------------------- 1 | ## Subdomain Visit Count 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++✔️ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/subdomain-visit-count/) 18 | 19 | ### 📖 Statement 20 | A website domain `"discuss.leetcode.com"` consists of various subdomains. At the top level, we have `"com"`, at the next level, we have `"leetcode.com"` and at the lowest level, `"discuss.leetcode.com"`. When we visit a domain like `"discuss.leetcode.com"`, we will also visit the parent domains `"leetcode.com"` and `"com"` implicitly. 21 | 22 | A count-paired domain is a domain that has one of the two formats `"rep d1.d2.d3"` or `"rep d1.d2"` where rep is the number of visits to the domain and d1.d2.d3 is the domain itself. 23 | 24 | For example, `"9001 discuss.leetcode.com"` is a count-paired domain that indicates that `discuss.leetcode.com` was visited 9001 times. 25 | 26 | Given an array of count-paired domains cpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order. 27 | 28 | 29 | ### ✍️ Solution 30 | 31 | ```Python 32 | class Solution: 33 | def subdomainVisits(self, cpdomains: List[str]) -> List[str]: 34 | di=dict() 35 | for sen in cpdomains: 36 | num = int(sen.split(" ")[0]) 37 | dom = sen.split(" ")[1] 38 | li=dom.split(".") 39 | for i in range(len(li)): 40 | tot=".".join(li[i:len(li)]) 41 | di[tot]=di.get(tot,0)+num 42 | fin=list() 43 | for key,val in di.items(): 44 | fin.append(str(val)+" "+key) 45 | return fin 46 | ``` 47 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/TwoSum.md: -------------------------------------------------------------------------------- 1 | 2 | #### Name: Two Sum 3 | #### Language: C++ 4 | #### Platform: Leetcode 5 | #### Question: https://leetcode.com/problems/two-sum/ 6 | 7 | #### Question Statement 8 | 9 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 10 | 11 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 12 | 13 | You can return the answer in any order. 14 | 15 | 16 | #### Solution: 17 | ``` 18 | class Solution { 19 | public: 20 | vector twoSum(vector& nums, int target) { 21 | vector ans; 22 | int n=nums.size(); 23 | for(int i=0;i 6 | 7 | ✔️ C++ 8 | Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Valid Parentheses 25 | 26 | ### ✍️ Solution 27 | ``` 28 | class Solution { 29 | public: 30 | bool arepair(char a, char b){ 31 | if(a=='(' && b==')') return true; 32 | else if(a=='{' && b=='}') return true; 33 | else if(a=='[' && b==']') return true; 34 | 35 | return false; 36 | } 37 | 38 | bool isValid(string x) { 39 | stack s; 40 | for(auto a: x){ 41 | if(a=='(' || a=='[' || a=='{'){ 42 | s.push(a); 43 | }else if(a==')' || a==']' || a=='}'){ 44 | if(s.empty() || !arepair(s.top(),a) ){ 45 | return false; 46 | }else{ 47 | s.pop(); 48 | } 49 | } 50 | } 51 | return s.empty()?true:false; 52 | } 53 | }; 54 | ``` 55 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/ZigZag_Conversion.md: -------------------------------------------------------------------------------- 1 | ## Zig-Zag Conversion 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
C++✔️ Python Java C Other
14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/zigzag-conversion/) 18 | 19 | ### 📖 Problem Statement 20 | 21 | Given a string s, find the longest palindromic subsequence's length in s. 22 | 23 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 24 | ``` 25 | P A H N 26 | A P L S I I G 27 | Y I R 28 | ``` 29 | 30 | ### ✍️ Solution 31 | 32 | ```Python 33 | def convert(s: str, numRows: int) -> str: 34 | if numRows == 1: 35 | return s 36 | count = 0 37 | result = [[] for _ in range(numRows)] 38 | flag = 1 39 | for i in range(len(s)): 40 | if count < numRows: 41 | result[count].append(s[i]) 42 | if flag: 43 | count+=1 44 | else: 45 | count-=1 46 | if count==numRows: 47 | count-=2 48 | flag=0 49 | if count==0: 50 | flag=1 51 | return ''.join([i for j in result for i in j]) 52 | ``` 53 | 54 | ### Explaination 55 | To solve this problem, we iterate though the string from left to right so we can determine which character belongs to which row in the ZigZag pattern.
56 | There is an edge case though, if the number of rows is 1, the whole string will be printed in a single row. Therefore we just return the original string there.
57 | We make an empty multidimensional list with number of empty lists being equal to number of rows. Say if we have 3 rows then it looks like: 58 | ``` 59 | [[], [], []] 60 | ``` 61 | Iterate through the string appending each character to the appropriate row (list). When we reach the uppermost or the lowermost row, we change a flag variable to 62 | !flag to change the direction in which we proceed, continuing the iteration through the string. 63 | 64 | **Time Complexity**: O(n) where n is the length of string 65 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/duplicateNumber.md: -------------------------------------------------------------------------------- 1 | #### Find the Duplicate Number 2 | 3 | #### Language: C++ 4 | 5 | #### Question Statement: 6 | 7 | Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. 8 | 9 | There is only one repeated number in nums, return this repeated number. 10 | 11 | You must solve the problem without modifying the array nums and uses only constant extra space. 12 | 13 |
14 | 15 | #### Leetcode Link: 16 | 17 | https://leetcode.com/problems/find-the-duplicate-number/ 18 | 19 | #### Solution: 20 | 21 | ``` 22 | class Solution { 23 | public: 24 | vector findDuplicates(vector& arr) { 25 | 26 | vector ans; 27 | 28 | for(int i=0;i 6 | 7 | C++ 8 | ✔️ Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | ### Question Plaform 16 | 17 | ✔️[LeetCode](https://leetcode.com/problems/find-the-difference/) 18 | 19 | ### 📖 Statement 20 | 21 | You are given two strings s and t. 22 | 23 | String t is generated by random shuffling string s and then add one more letter at a random position. 24 | 25 | Return the letter that was added to t. 26 | 27 | 28 | ### ✍️ Solution 29 | 30 | ```Python 31 | class Solution: 32 | def findTheDifference(self, s: str, t: str) -> str: 33 | t=list(t) 34 | s=list(s) 35 | for t1 in t: 36 | try: 37 | s.remove(t1) 38 | except: 39 | return t1 40 | ``` 41 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/isPalindrome.md: -------------------------------------------------------------------------------- 1 | 2 | Name: isPalindrome 3 | Language: Java 4 | Platform: Leetcode 5 | Question: 6 | 7 | Given an integer x, return true if x is palindrome integer. 8 | An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not. 9 | 10 | Example 1: 11 | 12 | Input: x = 121 13 | Output: true 14 | 15 | Example 2: 16 | 17 | Input: x = -121 18 | Output: false 19 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 20 | 21 | 22 | public class isPalindrome { 23 | public boolean palindrome(int x) { 24 | if(x<0) 25 | return false; 26 | 27 | int arraySize=0; 28 | int y = x; 29 | while(y>0){ 30 | y=y/10; 31 | ++arraySize; 32 | } 33 | int[] palArray = new int[arraySize]; 34 | 35 | y = x; 36 | int currentIndex=arraySize-1; 37 | while(y>0){ 38 | palArray[currentIndex] = y%10; 39 | y/=10; 40 | --currentIndex; 41 | } 42 | 43 | int startIndex = 0; 44 | int endIndex = arraySize-1; 45 | 46 | 47 | while(startIndex 7 | 8 | ✔️ C++ 9 | 10 | 11 | 12 | 13 | Leetcode Link: https://leetcode.com/problems/largest-rectangle-in-histogram/ 14 | 15 | ### 📖 Statement 16 | 17 | Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. 18 | 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | #include 24 | #include 25 | using namespace std; 26 | 27 | int get_max_area(vector a){ 28 | int n = a.size(), ans =0, i=0; 29 | stack st; 30 | a.push_back(0); 31 | while(i a[i]){ 33 | int t = st.top(); 34 | int h = a[t]; 35 | st.pop(); 36 | if(st.empty()){ 37 | ans = max(ans, h * i); 38 | }else{ 39 | int len = i - st.top() -1; 40 | ans = max(ans, h * len); 41 | } 42 | } 43 | st.push(i); 44 | i++; 45 | } 46 | return ans; 47 | } 48 | int main(int argc, char *argv[]) 49 | { 50 | vector a = {2,1,5,6,2,3}; 51 | cout< 6 | 7 | C++ 8 | ✔️ Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | ### 📖 Statement 16 | 17 | Given a binary array nums, return the maximum number of consecutive 1's in the array. 18 | 19 | ### ✍️ Solution 20 | 21 | ``` 22 | class Solution: 23 | def findMaxConsecutiveOnes(self, nums: List[int]) -> int: 24 | count = 0 25 | mxCount = 0 26 | for num in nums: 27 | if num==1: 28 | count+=1 29 | mxCount = max(count, mxCount) 30 | else: 31 | count=0 32 | return mxCount 33 | ``` 34 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/mergeSortedArray.md: -------------------------------------------------------------------------------- 1 | 2 | #### Name: Merge Sorted Array 3 | #### Language: Javascript 4 | #### Platform: Leetcode 5 | #### Question: https://leetcode.com/problems/merge-sorted-array/ 6 | 7 | #### Question Statement 8 | 9 | You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. 10 | 11 | Merge nums1 and nums2 into a single array sorted in non-decreasing order. 12 | 13 | Implement the Solution class: 14 | 15 | - Use three pointers, one to track the position to be inserted i.e. last element of arr1. 16 | - Remaining two will start iteration from last elements of both arr1 and arr2. 17 | - Compare and update the larger element in the positions. 18 | - Lastly, if any elements left in arr2, add them to arr1. 19 | 20 | 21 | #### Solution: 22 | ``` 23 | var mergeSortedArray = function(arr1,arr2,m,n) { 24 | let k = arr1.length - 1 25 | m-- 26 | n-- 27 | 28 | while(m>= 0 && n >= 0) { 29 | if(arr2[n] > arr1[m]) { 30 | arr1[k] = arr2[n] 31 | k-- 32 | n-- 33 | } else { 34 | arr1[k] = arr1[m] 35 | m-- 36 | k-- 37 | } 38 | 39 | } 40 | 41 | return arr1 42 | }; 43 | 44 | /** 45 | * Function mergeSortedArray will be called as such: 46 | * mergeSortedArray([1,2,3,0,0,0],[2,5,6],3,3); 47 | */ 48 | ``` 49 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/nQueenProblemSolution.md: -------------------------------------------------------------------------------- 1 | N-Queens Problem 2 | N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack each other by being in the same row, column or diagonal. 3 | 4 | It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n =3. So first we will consider the 4 queens problem and then generate it to n - queens problem. 5 | 6 | Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4. 7 | 8 | Language: C++ 9 | 10 | Question Platform: Leetcode 11 | 12 | Solution of the nQueensProblem 13 | 14 | #include 15 | using namespace std; 16 | bool isSafe(int** arr, int x, int y, int n){ 17 | for(int row=0; row=0 &&col>=0){ 25 | if(arr[row][col]== 1){ 26 | return false; 27 | } 28 | row--; 29 | col--; 30 | } 31 | row=x; 32 | col= y; 33 | 34 | while(row>=0 &&col=n){ 46 | return true; 47 | } 48 | for(int col=0; col>n; 64 | 65 | int **arr = new int *[n]; 66 | for(int i=0; i=0 && j>=0 && i < r && j < c; 69 | } 70 | 71 | void dfs(vector> &grid, int i, int j) { 72 | grid[i][j] = '0'; 73 | int r = grid.size(), c = grid[0].size(); 74 | for(int k=0; k<4; k++) { 75 | if(isSafe(i+row[k], j+col[k], r, c) && grid[i+row[k]][j+col[k]] == '1') { 76 | dfs(grid, i+row[k], j+col[k]); 77 | } 78 | } 79 | } 80 | 81 | int numIslands(vector>& grid) { 82 | int r = grid.size(), c = grid[0].size(); 83 | int count = 0; 84 | for(int i=0; i 6 | 7 | C++ 8 | ✔️ Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | ### 📖 Statement 16 | 17 | You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. 18 | 19 | Increment the large integer by one and return the resulting array of digits. 20 | 21 | ### ✍️ Solution 22 | 23 | ``` 24 | class Solution: 25 | def plusOne(self, digits: List[int]) -> List[int]: 26 | ans=0 27 | n=len(digits) 28 | res=[] 29 | for i,L in enumerate(digits): 30 | ans+=L*(10**(n-1-i)) 31 | ans=ans+1 32 | for num in str(ans): 33 | res.append(int(num)) 34 | return res 35 | ``` 36 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/removeNthNodeFromEndOfList.md: -------------------------------------------------------------------------------- 1 | #### Remove Nth Node From End of List 2 | #### Language: C++ 3 | #### Platform: LeetCode 4 | #### Question Statement: 5 | Given the head of a linked list, remove the nth node from the end of the list and return its head. 6 | 7 | Example 1: 8 | 9 | Input: head = [1,2,3,4,5], n = 2 10 | 11 | Output: [1,2,3,5] 12 | 13 | Example 2: 14 | 15 | Input: head = [1], n = 1 16 | 17 | Output: [] 18 | 19 | Example 3: 20 | 21 | Input: head = [1,2], n = 1 22 | 23 | Output: [1] 24 | 25 | Constraints: 26 | 27 | - The number of nodes in the list is sz. 28 | - 1 <= sz <= 30 29 | - 0 <= Node.val <= 100 30 | - 1 <= n <= sz 31 | 32 |
33 | 34 | #### LeetCode Link: 35 | https://leetcode.com/problems/remove-nth-node-from-end-of-list/submissions/ 36 | 37 | 38 | #### Solution: 39 | ``` 40 | /** 41 | * Definition for singly-linked list. 42 | * struct ListNode { 43 | * int val; 44 | * ListNode *next; 45 | * ListNode() : val(0), next(nullptr) {} 46 | * ListNode(int x) : val(x), next(nullptr) {} 47 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 48 | * }; 49 | */ 50 | class Solution { 51 | public: 52 | ListNode* removeNthFromEnd(ListNode* head, int n) { 53 | ListNode* start = new ListNode(); 54 | start->next=head; 55 | ListNode* slow=start; 56 | ListNode* fast=start; 57 | 58 | for(int i=1;i<=n;i++){ 59 | fast=fast->next; 60 | } 61 | while(fast->next!=NULL){ 62 | slow=slow->next; 63 | fast=fast->next; 64 | } 65 | slow->next=slow->next->next; 66 | return start->next; 67 | } 68 | }; 69 | ``` 70 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/rotateList.md: -------------------------------------------------------------------------------- 1 | #### Rotate List 2 | #### Language: C++ 3 | #### Platform: LeetCode 4 | #### Question Statement: 5 | Given the head of a linked list, rotate the list to the right by k places. 6 | 7 | Example 1: 8 | 9 | Input: head = [1,2,3,4,5], k = 2 10 | 11 | Output: [4,5,1,2,3] 12 | 13 | Explanation: 14 | 15 | ##### Rotate 1:
5 -> 1 -> 2 -> 3 -> 4 16 | ##### Rotate 2:
4 -> 5 -> 1 -> 2 -> 3 17 | 18 | Constraints: 19 | 20 | - The number of nodes in the list is in the range [0, 500]. 21 | - -100 <= Node.val <= 100 22 | - 0 <= k <= 2 * 109 23 |
24 | 25 | #### LeetCode Link: 26 | https://leetcode.com/problems/rotate-list/ 27 | 28 | 29 | #### Solution: 30 | ``` 31 | /** 32 | * Definition for singly-linked list. 33 | * struct ListNode { 34 | * int val; 35 | * ListNode *next; 36 | * ListNode() : val(0), next(nullptr) {} 37 | * ListNode(int x) : val(x), next(nullptr) {} 38 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 39 | * }; 40 | */ 41 | class Solution { 42 | public: 43 | ListNode* rotateRight(ListNode* head, int k) { 44 | if (head==NULL || k==0){ 45 | return head; 46 | } 47 | int len=1; 48 | ListNode* node=head; 49 | while (node->next!=NULL){ 50 | node = node->next; 51 | len++; 52 | } 53 | ListNode* p=head; 54 | int n=k%len; 55 | for(int i=0; inext; 57 | } 58 | node->next = head; 59 | head=p->next; 60 | p->next=NULL; 61 | return head; 62 | } 63 | }; 64 | ``` 65 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/shuffleAnArray.md: -------------------------------------------------------------------------------- 1 | 2 | #### Name: Shuffle an Array 3 | #### Language: C++ 4 | #### Platform: Leetcode 5 | #### Question: https://leetcode.com/problems/shuffle-an-array/ 6 | 7 | #### Question Statement 8 | 9 | Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling. 10 | 11 | Implement the Solution class: 12 | 13 | - Solution(int[] nums) Initializes the object with the integer array nums. 14 | - int[] reset() Resets the array to its original configuration and returns it. 15 | - int[] shuffle() Returns a random shuffling of the array. 16 | 17 | 18 | #### Solution: 19 | ```class Solution { 20 | private: 21 | vector n; 22 | public: 23 | Solution(vector& nums) { 24 | n=nums; 25 | } 26 | 27 | /** Resets the array to its original configuration and return it. */ 28 | vector reset() { 29 | return n; 30 | } 31 | 32 | /** Returns a random shuffling of the array. */ 33 | vector shuffle() { 34 | vector s=n; 35 | for (int i = s.size() - 1; i >= 0; i--) { 36 | int r = rand() % (i+1); 37 | swap(s[i], s[r]); 38 | } 39 | 40 | return s; 41 | } 42 | }; 43 | 44 | /** 45 | * Your Solution object will be instantiated and called as such: 46 | * Solution* obj = new Solution(nums); 47 | * vector param_1 = obj->reset(); 48 | * vector param_2 = obj->shuffle(); 49 | */ 50 | ``` 51 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/swappingNodesInALinkedList.md: -------------------------------------------------------------------------------- 1 | #### Swapping Nodes in a Linked List 2 | #### Language: C++ 3 | #### Platform: LeetCode 4 | #### Question Statement: 5 | You are given the head of a linked list, and an integer k. 6 | 7 | Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed). 8 | 9 | Example 1: 10 | 11 | Input: head = [1,2,3,4,5], k = 2 12 | 13 | Output: [1,4,3,2,5] 14 | 15 | Example 2: 16 | 17 | Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5 18 | 19 | Output: [7,9,6,6,8,7,3,0,9,5] 20 | 21 | Example 3: 22 | 23 | Input: head = [1], k = 1 24 | 25 | Output: [1] 26 | 27 | Constraints: 28 | 29 | - The number of nodes in the list is n. 30 | - 1 <= k <= n <= 105 31 | - 0 <= Node.val <= 100 32 | 33 |
34 | 35 | #### LeetCode Link: 36 | https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ 37 | 38 | 39 | #### Solution: 40 | ``` 41 | /** 42 | * Definition for singly-linked list. 43 | * struct ListNode { 44 | * int val; 45 | * ListNode *next; 46 | * ListNode() : val(0), next(nullptr) {} 47 | * ListNode(int x) : val(x), next(nullptr) {} 48 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 49 | * }; 50 | */ 51 | class Solution { 52 | public: 53 | void swap(int i,int j,vector&v){ 54 | int temp=v[i]; 55 | v[i]=v[j]; 56 | v[j]=temp; 57 | } 58 | ListNode* swapNodes(ListNode* head, int k) { 59 | ListNode* temp=head; 60 | ListNode* rebuild=head; 61 | vectorv; 62 | int count=0; 63 | while(temp){ 64 | v.push_back(temp->val); 65 | temp=temp->next; 66 | count++; 67 | } 68 | swap(k-1,count-k,v); 69 | int i=0; 70 | while(rebuild){ 71 | rebuild->val=v[i]; 72 | i++; 73 | rebuild=rebuild->next; 74 | } 75 | return head; 76 | } 77 | }; 78 | ``` 79 | -------------------------------------------------------------------------------- /Assets/Codes/Leetcode/trappingRainWater.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | Trapping Rain Water 3 | 4 | ### 💻 Language(s) 5 | 6 | 7 | 8 | 9 | 10 |
✔️ C++
11 | 12 | Leetcode link: https://leetcode.com/problems/trapping-rain-water/ 13 | 14 | ### 📖 Statement 15 | Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. 16 | 17 | 18 | ### ✍️ Solution 19 | 20 | #include 21 | #include "vector" 22 | #include "stack" 23 | #include 24 | using namespace std; 25 | 26 | int rain_water(vector a){ 27 | stack st; 28 | int n = a.size(); 29 | int ans = 0; 30 | for(int i = 0; i a = {0,1,0,2,1,0,1,3,2,1,2,1}; cout< 22 | using namespace std; 23 | 24 | long long checktype(string &st, int n) 25 | { 26 | long long i = 0, count = 0; 27 | stack curr; 28 | for (i = 0; i < n; i++) 29 | { 30 | if (st[i] != 'F') 31 | { 32 | if (curr.empty()) 33 | curr.push(st[i]); 34 | else 35 | { 36 | char c = curr.top(); 37 | if (st[i] != c) 38 | { 39 | count++; 40 | curr.pop(); 41 | curr.push(st[i]); 42 | } 43 | } 44 | } 45 | } 46 | return count; 47 | } 48 | 49 | int main() 50 | { 51 | int t, n, j, temp = 1; 52 | cin >> t; 53 | 54 | while (t--) 55 | { 56 | cin >> n; 57 | string st; 58 | cin.ignore(); 59 | getline(cin, st); 60 | cout << "Case #" << temp << ": " << checktype(st, n) << endl; 61 | temp++; 62 | } 63 | } 64 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/BTrafficControl.md: -------------------------------------------------------------------------------- 1 | # Problem B: Traffic Control 2 | 3 | ## Language: `c++` 4 | 5 | ### Question Statement [link](https://www.facebook.com/codingcompetitions/hacker-cup/2021/round-1/problems/B) 6 | ### **Question Statement** 7 | 8 | In the rapidly growing towns of Silicon Valley, traffic congestion is becoming a major problem. The governor has contracted Travis, the rock star traffic engineer, to come up with some solutions. 9 | A town consists of N horizontal streets and M vertical avenues, making up NxM intersections. Each intersection has some wait time, which is an integer number of seconds between 1 and 1000, inclusive. The town's wait times can thus be represented as a grid with N rows and M columns of integers. 10 | A route within the grid consists of a sequence of one or more intersections, such that each pair of consecutive intersections are either horizontally or vertically adjacent to one another. The duration of a route is the sum of all of its intersections' wait times. 11 | Travis is tasked with assigning valid wait times for all N*M intersections. However, he must be careful in his choices. Routes too slow will lead to congestion, and routes too fast will lead to accidents. After careful calculations, Travis has determined that that an optimal grid must simultaneously satisfy the following two benchmark conditions based off the four corners of the town: 12 | The duration of the fastest route starting in the top-left intersection and ending in the bottom-right one is exactly AA seconds, and 13 | the duration of the fastest route starting in the top-right intersection and ending in the bottom-left one is exactly BB seconds. 14 | Please help Travis generate any such valid grid, or determine that no such grid exists. 15 | 16 | **Input** 17 | Input begins with an integer T, the number of towns that Travis is contracted for. For each town, there is a single line containing the 4 space-separated integers N, M, A, and B. 18 | 19 | **Output** 20 | For the ith town, first print a line containing "Case #i: " followed by either the string "Possible" or "Impossible". If possible, this should be followed NN rows of M space-separated integers each, comprising your chosen grid of intersection wait times in seconds. 21 | 22 | ### **Solution** 23 | 24 | ```cpp 25 | #include 26 | using namespace std; 27 | 28 | void printmt(int n, int m, int a, int b) 29 | { 30 | int i, j; 31 | for (i = 0; i < n; i++) 32 | { 33 | for (j = 0; j < m; j++) 34 | { 35 | if (i == 0 && j == 0 ) 36 | cout << a - n - m + 2<<" "; 37 | else if(i == n - 1 && j == 0) 38 | cout << b - n - m +2<<" "; 39 | else 40 | cout<<"1 "; 41 | } 42 | cout << endl; 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | int t, n, j, temp = 1; 49 | cin >> t; 50 | 51 | while (t--) 52 | { 53 | int m, a, b; 54 | cin >> n >> m >> a >> b; 55 | if ((n + m - 1) > a || (n + m - 1) > b) 56 | cout << "Case #" << temp << ": Impossible" << endl; 57 | else 58 | { 59 | cout << "Case #" << temp << ": Possible" << endl; 60 | printmt(n, m, a, b); 61 | } 62 | temp++; 63 | } 64 | } 65 | ``` 66 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/BitStrings(CSES).md: -------------------------------------------------------------------------------- 1 | ## Bit Strings 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 |
✔️ C++
10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | Your task is to calculate the number of bit strings of length n. 17 | 18 | For example, if n=3, the correct answer is 8, because the possible bit strings are 000, 001, 010, 011, 100, 101, 110, and 111. 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | using namespace std; 24 | #define ll long long 25 | const int mod = 1e9+7; 26 | 27 | signed main() 28 | { 29 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 30 | ll n; 31 | cin>>n; 32 | //n digits of the strings can be filled with either 1 or 0 33 | // 2 options 34 | //therefore total ways is 2^n 35 | 36 | ll base=2, ans=1; 37 | 38 | //iterative binary exponentiation for 2^n 39 | while(n) 40 | { 41 | if(n%2) 42 | { 43 | ans*=base; 44 | ans%=mod; 45 | } 46 | base*=base; 47 | base%=mod; 48 | n/=2; 49 | } 50 | 51 | cout< 6 | 7 | ✔️ C++ 8 | Python 9 | Java 10 | C 11 | Other 12 | 13 | 14 | 15 | ### Question Plaform-GeeksForGeeks 16 | Problem Link - https://practice.geeksforgeeks.org/problems/first-element-to-occur-k-times5150/1# 17 | 18 | 19 | 20 | ### 📖 Statement 21 | 22 | Given an array of N integers. Find the first element that occurs K number of times. 23 | 24 | 25 | Example 1: 26 | 27 | Input : 28 | N = 7, K = 2 29 | A[] = {1, 7, 4, 3, 4, 8, 7} 30 | Output : 31 | 4 32 | Explanation: 33 | Both 7 and 4 occur 2 times. 34 | But 4 is first that occurs 2 times. 35 | 36 | ### ✍️ Solution 37 | 38 | class Solution{ 39 | public: 40 | int firstElementKTime(int a[], int n, int k) 41 | { 42 | unordered_map m; 43 | for(int i = 0;i 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | You are given an array of n integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element. 17 | 18 | On each move, you may increase the value of any element by one. What is the minimum number of moves required? 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | using namespace std; 24 | #define ll long long 25 | int main() 26 | { 27 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 28 | ll n; 29 | cin>>n; 30 | ll max_so_far=0,ans=0; 31 | for(int i=0; i>a; 35 | 36 | ans += max(0LL, max_so_far-a); 37 | 38 | max_so_far=max(max_so_far, a); 39 | } 40 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | You are given all numbers between 1,2,…,n except one. Your task is to find the missing number. 17 | 18 | ### ✍️ Solution 19 | 20 | #include 21 | using namespace std; 22 | #define int long long 23 | 24 | signed main() 25 | { 26 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 27 | int n; 28 | cin>>n; 29 | int s=(n*(n+1))/2; 30 | for(int i=0; i>a; 33 | s-=a; 34 | } 35 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | A permutation of integers 1,2,…,n is called beautiful if there are no adjacent elements whose difference is 1. 17 | 18 | Given n, construct a beautiful permutation if such a permutation exists. 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | using namespace std; 24 | #define ll long long 25 | 26 | int main() 27 | { 28 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 29 | ll n; 30 | cin>>n; 31 | if(n==1) //corner case n==1 32 | cout<<1<<" "; 33 | if(n<=3) //corner cases n==2 or n==3 34 | { 35 | cout<<"NO SOLUTION"; 36 | } 37 | else 38 | { 39 | //just print all even numbers then all odd numbers 40 | for(ll j : {2, 1}){ 41 | for(ll i=j; i<=n; i+=2) 42 | { 43 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | You are given a DNA sequence: a string consisting of characters A, C, G, and T. Your task is to find the longest repetition in the sequence. This is a maximum-length substring containing only one type of character. 17 | 18 | ### ✍️ Solution 19 | 20 | #include 21 | using namespace std; 22 | #define int long long 23 | 24 | signed main() 25 | { 26 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 27 | string s; 28 | cin>>s; 29 | char l='.'; 30 | int cnt=0, ans=0; 31 | for(auto ch : s) 32 | { 33 | if(ch==l) 34 | { 35 | cnt++; 36 | } 37 | else 38 | { 39 | ans=max(ans, cnt); 40 | cnt=1; 41 | } 42 | l=ch; 43 | } 44 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | Your task is to calculate the number of trailing zeros in the factorial n!. 17 | 18 | For example, 20!=2432902008176640000 and it has 4 trailing zeros. 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | using namespace std; 24 | #define ll long long 25 | 26 | int main() 27 | { 28 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 29 | ll n, ans=0LL, r=1LL; 30 | cin>>n; 31 | //trailing zeros 32 | // = power of 10 in n! 33 | // = min(power of 2 in n!, power of 5 in n!) 34 | // = power of 5 in n! [since number of 5 < 2 always] 35 | 36 | while(n/r > 0) 37 | { 38 | r*=5; //r will become all powers of 5 less than n 39 | ans += n/r; 40 | } 41 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | Your task is to count for k=1,2,…,n the number of ways two knights can be placed on a k×k chessboard so that they do not attack each other. 17 | 18 | ### ✍️ Solution 19 | 20 | #include 21 | using namespace std; 22 | #define int long long 23 | #define pb push_back 24 | signed main() 25 | { 26 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 27 | int n; 28 | cin>>n; 29 | std::vector v; 30 | v.pb(0); 31 | v.pb(6); 32 | v.pb(28); 33 | int x=n; 34 | int k; 35 | while((n-3)>0) 36 | { 37 | n--; 38 | k=(int)v.size()+1; 39 | v.pb(v.back() + (2*k-1)*(k-1)*k - 8*(k-2)); 40 | } 41 | for(int i=0; i 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | Your task is to divide the numbers 1,2,…,n into two sets of equal sum. 17 | 18 | ### ✍️ Solution 19 | 20 | #include 21 | using namespace std; 22 | #define ll long long 23 | #define pb push_back 24 | 25 | signed main() 26 | { 27 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 28 | ll n; 29 | cin>>n; 30 | if(n%4==1 or n%4==2){ 31 | cout<<"NO\n"; 32 | } 33 | else if(n%4 == 0) 34 | { 35 | cout<<"YES\n"; 36 | //2 sets of size n/2 37 | 38 | //set 1 39 | //combination of first and last n/4 numbers 40 | cout< a,b; 57 | //a for odd, b for even 58 | for(int i=1; i<=n; ++i) 59 | { 60 | if(i%2) 61 | a.pb(i); 62 | else b.pb(i); 63 | } 64 | //right now there is a difference of ((n-n/2)/2)*2 between the two sets 65 | 66 | for(int i=0; i<(n-n/2)/2; ++i) 67 | swap(a[i+1], b[i]); 68 | //doing this will create a gap of 2 for each swap 69 | //since a[i+1] and b[i] are consecutive 70 | 71 | //set 1 72 | cout<<(int)a.size()<<'\n'; 73 | for(auto i : a) 74 | cout< 6 | 7 | ✔️ C++ 8 | 9 | 10 | 11 | ### Question Plaform 12 | CSES 13 | 14 | ### 📖 Statement 15 | 16 | Consider an algorithm that takes as input a positive integer n. If n is even, the algorithm divides it by two, and if n is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until n is one. 17 | 18 | Your task is to simulate the execution of the algorithm for a given value of n. 19 | 20 | ### ✍️ Solution 21 | 22 | #include 23 | using namespace std; 24 | #define int long long 25 | 26 | signed main() 27 | { 28 | ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); 29 | int n; 30 | cin>>n; 31 | while(n!=1) 32 | { 33 | cout< 7 | 8 | ✔️ C++ 9 | 10 | 11 | 12 | ### Question Plaform 13 | ✔️HackerRank 14 | 15 | ### 📖 Statement 16 | 17 | You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions: 18 | get_age, set_age 19 | get_first_name, set_first_name 20 | get_last_name, set_last_name 21 | get_standard, set_standard 22 | Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this. 23 | 24 | ### ✍️ Solution 25 | 26 | ``` 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | /* 32 | Enter code for class Student here. 33 | Read statement for specification. 34 | */ 35 | class Student { 36 | private: 37 | int age, standard; 38 | string first_name, last_name; 39 | public: 40 | void set_age(int aAge){ 41 | age = aAge; 42 | } 43 | void set_standard(int aStandard){ 44 | standard = aStandard; 45 | } 46 | void set_first_name(string aFirst_name){ 47 | first_name = aFirst_name; 48 | } 49 | void set_last_name(string aLast_name){ 50 | last_name = aLast_name; 51 | } 52 | int get_age(){ 53 | return age; 54 | } 55 | int get_standard(){ 56 | return standard; 57 | } 58 | string get_first_name(){ 59 | return first_name; 60 | } 61 | string get_last_name(){ 62 | return last_name; 63 | } 64 | string to_string(){ 65 | stringstream ss("age standard first_name last_name"); 66 | char ch = ','; 67 | string a, b, c, d; 68 | ss >> a >> ch >> b >> ch >> c >> ch >> d; 69 | } 70 | }; 71 | 72 | int main() { 73 | int age, standard; 74 | string first_name, last_name; 75 | 76 | cin >> age >> first_name >> last_name >> standard; 77 | 78 | Student st; 79 | st.set_age(age); 80 | st.set_standard(standard); 81 | st.set_first_name(first_name); 82 | st.set_last_name(last_name); 83 | 84 | cout << st.get_age() << "\n"; 85 | cout << st.get_last_name() << ", " << st.get_first_name() << "\n"; 86 | cout << st.get_standard() << "\n"; 87 | cout << "\n"; 88 | 89 | cout << st.to_string(); 90 | return 0; 91 | } 92 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/infixToPostfix.md: -------------------------------------------------------------------------------- 1 | #### Infix to Postfix 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given an infix expression in the form of string str. Convert this infix expression to postfix expression. 6 | 7 | > *Infix expression*: The expression of the form a op b. When an operator is in-between every pair of operands. 8 | 9 | > *Postfix expression*: The expression of the form a b op. When an operator is followed for every pair of operands. 10 | 11 | Note: The order of precedence is: ^ greater than * equals to / greater than + equals to -. 12 | 13 | Example 1: 14 | 15 | Input: str = "a+b*(c^d-e)^(f+g*h)-i" 16 | 17 | Output: abcd^e-fgh*+^*+i- 18 | 19 | Explanation: 20 | After converting the infix expression 21 | into postfix expression, the resultant 22 | expression will be abcd^e-fgh*+^*+i- 23 | 24 | Example 2: 25 | 26 | Input: str = "A*(B+C)/D" 27 | 28 | Output: ABC+*D/ 29 | 30 | Explanation: 31 | After converting the infix expression 32 | into postfix expression, the resultant 33 | expression will be ABC+*D/ 34 | 35 | ##### Expected Time Complexity: O(|str|). 36 | ##### Expected Auxiliary Space: O(|str|). 37 | 38 | Constraints: 39 | 1 ≤ |str| ≤ 105 40 | 41 |
42 | 43 | #### geeksforgeeks Link: 44 | https://practice.geeksforgeeks.org/problems/infix-to-postfix-1587115620/1 45 | 46 | 47 | #### Solution: 48 | ``` 49 | class Solution 50 | { 51 | public: 52 | //Function to convert an infix expression to a postfix expression. 53 | 54 | int prec(char c) { 55 | if(c == '^') 56 | return 3; 57 | else if(c == '/' || c=='*') 58 | return 2; 59 | else if(c == '+' || c == '-') 60 | return 1; 61 | else 62 | return -1; 63 | } 64 | string infixToPostfix(string s) { 65 | 66 | stack st; //For stack operations, we are using C++ built in stack 67 | string result; 68 | 69 | for(int i = 0; i < s.length(); i++) { 70 | char c = s[i]; 71 | 72 | // If the scanned character is 73 | // an operand, add it to output string. 74 | if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) 75 | result += c; 76 | 77 | // If the scanned character is an 78 | // ‘(‘, push it to the stack. 79 | else if(c == '(') 80 | st.push('('); 81 | 82 | // If the scanned character is an ‘)’, 83 | // pop and to output string from the stack 84 | // until an ‘(‘ is encountered. 85 | else if(c == ')') { 86 | while(st.top() != '(') 87 | { 88 | result += st.top(); 89 | st.pop(); 90 | } 91 | st.pop(); 92 | } 93 | 94 | //If an operator is scanned 95 | else { 96 | while(!st.empty() && prec(s[i]) <= prec(st.top())) { 97 | result += st.top(); 98 | st.pop(); 99 | } 100 | st.push(c); 101 | } 102 | } 103 | 104 | // Pop all the remaining elements from the stack 105 | while(!st.empty()) { 106 | result += st.top(); 107 | st.pop(); 108 | } 109 | 110 | return result; 111 | } 112 | }; 113 | ``` 114 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/kadanesAlgorithm.md: -------------------------------------------------------------------------------- 1 | #### Kadane's Algorithm 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum. 6 | 7 | Example 1: 8 | 9 | Input: 10 | N = 5 11 | 12 | Arr[] = {1,2,3,-2,5} 13 | 14 | Output: 15 | 9 16 | 17 | Explanation: 18 | Max subarray sum is 9 19 | of elements (1, 2, 3, -2, 5) which 20 | is a contiguous subarray. 21 | 22 | Example 2: 23 | 24 | Input: 25 | N = 4 26 | 27 | Arr[] = {-1,-2,-3,-4} 28 | 29 | Output: 30 | -1 31 | 32 | Explanation: 33 | Max subarray sum is -1 34 | of element (-1) 35 | 36 |
37 | 38 | #### geeksforgeeks Link: 39 | https://practice.geeksforgeeks.org/problems/kadanes-algorithm-1587115620/1 40 | 41 | 42 | #### Solution: 43 | ``` 44 | class Solution{ 45 | public: 46 | // arr: input array 47 | // n: size of array 48 | //Function to find the sum of contiguous subarray with maximum sum. 49 | long long maxSubarraySum(int arr[], int n){ 50 | long long curr=0; 51 | long long maxTillNow=INT_MIN; 52 | for(int i=0;i 38 | 39 | #### geeksforgeeks Link: 40 | https://practice.geeksforgeeks.org/problems/kth-smallest-element5635/1 41 | 42 | 43 | #### Solution: 44 | ``` 45 | class Solution{ 46 | public: 47 | // arr : given array 48 | // l : starting index of the array i.e 0 49 | // r : ending index of the array i.e size-1 50 | // k : find kth smallest element and return using this function 51 | int kthSmallest(int arr[], int l, int r, int k) { 52 | int n=r-l+1; 53 | sort(arr,arr+n); 54 | 55 | return arr[k-1]; 56 | } 57 | }; 58 | ``` 59 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/minimumNumberOfJumps.md: -------------------------------------------------------------------------------- 1 | #### Minimum number of jumps 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given an array of N integers arr[] where each element represents the max number of steps that can be made forward from that element. Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element. 6 | 7 | Note: Return -1 if you can't reach the end of the array. 8 | 9 | Example 1: 10 | 11 | Input: 12 | N = 11 13 | 14 | arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} 15 | 16 | Output: 3 17 | 18 | Explanation: 19 | First jump from 1st element to 2nd 20 | element with value 3. Now, from here 21 | we jump to 5th element with value 9, 22 | and from here we will jump to last. 23 | 24 | Example 2: 25 | 26 | Input: 27 | N = 6 28 | 29 | arr = {1, 4, 3, 2, 6, 7} 30 | 31 | Output: 2 32 | 33 | Explanation: 34 | First we jump from the 1st to 2nd element 35 | and then jump to the last element. 36 | 37 |
38 | 39 | #### geeksforgeeks Link: 40 | https://practice.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1 41 | 42 | 43 | #### Solution: 44 | ``` 45 | class Solution{ 46 | public: 47 | int minJumps(int arr[], int n){ 48 | // Your code here 49 | if(arr[0]==0) 50 | return -1; 51 | if(n==1) 52 | return 0; 53 | int current=arr[0],maxindex=arr[0],count=1; 54 | for(int i=1;i=n-1) 63 | return count; 64 | } 65 | return -1; 66 | } 67 | }; 68 | ``` 69 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/postfixEvaluation.md: -------------------------------------------------------------------------------- 1 | #### Evaluation of Postfix Expression 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given string S representing a postfix expression, the task is to evaluate the expression and find the final value. Operators will only include the basic arithmetic operators like *, /, + and -. 6 | 7 | Example 1: 8 | 9 | Input: S = "231*+9-" 10 | 11 | Output: -4 12 | 13 | Explanation: 14 | After solving the given expression, 15 | we have -4 as result. 16 | 17 | Example 2: 18 | 19 | Input: S = "123+*8-" 20 | 21 | Output: -3 22 | 23 | Explanation: 24 | After solving the given postfix 25 | expression, we have -3 as result. 26 | 27 | ##### Expected Time Complexity: O(|S|) 28 | ##### Expected Auixilliary Space: O(|S|) 29 | 30 | Constraints: 31 | 1 ≤ |S| ≤ 105 32 | 33 |
34 | 35 | #### geeksforgeeks Link: 36 | https://practice.geeksforgeeks.org/problems/evaluation-of-postfix-expression1735/1# 37 | 38 | 39 | #### Solution: 40 | ``` 41 | class Solution 42 | { 43 | public: 44 | //Function to evaluate a postfix expression. 45 | int evaluatePostfix(string s) 46 | { 47 | stack st; 48 | int op1, op2; 49 | 50 | for (int i = 0; i <= s.length()-1; i++) 51 | { 52 | if (s[i] >= '0' && s[i] <= '9') 53 | { 54 | st.push(s[i] - '0'); 55 | } 56 | else 57 | { 58 | op2 = st.top(); 59 | st.pop(); 60 | op1 = st.top(); 61 | st.pop(); 62 | 63 | switch (s[i]) 64 | { 65 | case '+': 66 | st.push(op1 + op2); 67 | break; 68 | case '-': 69 | st.push(op1 - op2); 70 | break; 71 | case '*': 72 | st.push(op1 * op2); 73 | break; 74 | case '/': 75 | st.push(op1 / op2); 76 | break; 77 | case '^': 78 | st.push(pow(op1, op2)); 79 | break; 80 | } 81 | } 82 | } 83 | return st.top(); 84 | } 85 | }; 86 | ``` 87 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/queueReversal.md: -------------------------------------------------------------------------------- 1 | #### Queue Reversal 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given a Queue Q containing N elements. The task is to reverse the Queue. Your task is to complete the function rev(), that reverses the N elements of the queue. 6 | 7 | Example 1: 8 | 9 | Input: 10 | 6 11 | 12 | 4 3 1 10 2 6 13 | 14 | Output: 15 | 6 2 10 1 3 4 16 | 17 | Explanation: 18 | After reversing the given 19 | elements of the queue , the resultant 20 | queue will be 6 2 10 1 3 4. 21 | 22 | Example 2: 23 | 24 | Input: 25 | 4
26 | 4 3 2 1 27 | 28 | Output: 29 | 1 2 3 4 30 | 31 | Explanation: 32 | After reversing the given 33 | elements of the queue , the resultant 34 | queue will be 1 2 3 4. 35 | 36 | ##### Expected Time Complexity : O(n) 37 | ##### Expected Auxilliary Space : O(n) 38 | 39 | Constraints: 40 | 1 ≤ N ≤ 105
41 | 1 ≤ elements of Queue ≤ 105 42 | 43 |
44 | 45 | #### geeksforgeeks Link: 46 | https://practice.geeksforgeeks.org/problems/queue-reversal/1 47 | 48 | 49 | #### Solution: 50 | ``` 51 | //Function to reverse the queue. 52 | queue rev(queue q) 53 | { 54 | 55 | stack stack; 56 | while (!q.empty()) { 57 | stack.push(q.front()); 58 | q.pop(); 59 | } 60 | while (!stack.empty()) { 61 | q.push(stack.top()); 62 | stack.pop(); 63 | } 64 | return q; 65 | 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/reverseFirstKElementsOfQueue.md: -------------------------------------------------------------------------------- 1 | #### Reverse First K elements of Queue 2 | #### Language: C++ 3 | #### Platform: geeksforgeeks 4 | #### Question Statement: 5 | Given an integer K and a queue of integers, we need to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order. 6 | 7 | ##### Only following standard operations are allowed on queue. 8 | 9 | - enqueue(x) : Add an item x to rear of queue 10 | 11 | - dequeue() : Remove an item from front of queue 12 | 13 | - size() : Returns number of elements in queue. 14 | 15 | - front() : Finds front item. 16 | 17 | 18 | Example 1: 19 | 20 | Input: 21 | 5 3
22 | 1 2 3 4 5 23 | 24 | Output: 25 | 3 2 1 4 5 26 | 27 | Explanation: 28 | After reversing the given 29 | input from the 3rd position the resultant 30 | output will be 3 2 1 4 5. 31 | 32 | 33 | Example 2: 34 | 35 | Input: 36 | 4 4
37 | 4 3 2 1 38 | 39 | Output: 40 | 1 2 3 4 41 | 42 | Explanation: 43 | After reversing the given 44 | input from the 4th position the resultant 45 | output will be 1 2 3 4. 46 | 47 | ##### Expected TIme Complexity : O(n) 48 | ##### Expected Auxilliary Space : O(n) 49 | 50 | Constraints: 51 | 1 <= N <= 1000
52 | 1 <= K <= N 53 | 54 |
55 | 56 | #### geeksforgeeks Link: 57 | https://practice.geeksforgeeks.org/problems/reverse-first-k-elements-of-queue/1# 58 | 59 | 60 | #### Solution: 61 | ``` 62 | //Function to reverse first k elements of a queue. 63 | queue modifyQueue(queue q, int k) 64 | { 65 | 66 | stack Stack; 67 | 68 | if (q.empty() == true || k > q.size()) 69 | return q; 70 | if (k <= 0) 71 | return q; 72 | 73 | /* Push the first K elements 74 | into a Stack*/ 75 | for (int i = 0; i < k; i++) { 76 | Stack.push(q.front()); 77 | q.pop(); 78 | } 79 | 80 | /* Enqueue the contents of stack 81 | at the back of the queue*/ 82 | while (!Stack.empty()) { 83 | q.push(Stack.top()); 84 | Stack.pop(); 85 | } 86 | 87 | /* Remove the remaining elements and 88 | enqueue them at the end of the Queue*/ 89 | for (int i = 0; i < q.size() - k; i++) { 90 | q.push(q.front()); 91 | q.pop(); 92 | } 93 | 94 | return q; 95 | } 96 | ``` 97 | -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/splitHouses.md: -------------------------------------------------------------------------------- 1 | #### Split houses 2 | 3 | #### Language: C++ 4 | 5 | #### Question Statement: 6 | You live in a village. The village can be represented as a line that contains grids. Each grid can be denoted as a house that is marked as or a blank space that is marked as . 7 | 8 | A person lives in each house. A person can move to a grid if it is adjacent to that person. Therefore, the grid must be present on the left and right side of that person. 9 | 10 | Now, you are required to put some fences that can be marked as on some blank spaces so that the village can be divided into several pieces. A person cannot walk past a fence but can walk through a house. 11 | 12 | You are required to divide the house based on the following rules: 13 | 14 | A person cannot reach a house that does not belong to that specific person. 15 | The number of grids each person can reach is the same and it includes the grid in which the house is situated. 16 | In order to show that you are enthusiastic and if there are many answers, then you are required to print the one where most fences are placed. 17 | Your task is to decide whether there is a possible solution. Print the possible solution. 18 |
19 | 20 | #### HackerEarth Link: 21 | https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/split-house-547be0e9/ 22 | 23 | 24 | #### Solution: 25 | ``` 26 | #include 27 | #include 28 | using namespace std; 29 | int main() 30 | { 31 | int n,flag=0; 32 | string str; 33 | cin>>n>>str; 34 | int i; 35 | for(i=0;i 7 | 8 | ✔️ C++ 9 | 10 | 11 | 12 | ### Question Plaform 13 | ✔️HackerRank 14 | 15 | ### 📖 Statement 16 | 17 | stringstream is a stream class to operate on strings. It implements input/output operations on memory (string) based streams. stringstream can be helpful in different type of parsing. 18 | Complete the parseInts function in the editor below. 19 | parseInts has the following parameters: 20 | string str: a string of comma separated integers 21 | 22 | ### ✍️ Solution 23 | 24 | ``` 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | vector parseInts(string str) { 31 | // Complete this function 32 | stringstream ss(str); 33 | vector numberGrid; 34 | char comma; 35 | int number; 36 | while (ss >> number){ 37 | numberGrid.push_back(number); 38 | ss >> comma; 39 | } 40 | return numberGrid; 41 | } 42 | 43 | int main() { 44 | string str; 45 | cin >> str; 46 | vector integers = parseInts(str); 47 | for(int i = 0; i < integers.size(); i++) { 48 | cout << integers[i] << "\n"; 49 | } 50 | 51 | return 0; 52 | } 53 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/struct.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | Struct 3 | 4 | ### 💻 Language(s) 5 | 6 | 7 | 8 | 9 | 10 |
✔️ C++
11 | 12 | ### Question Plaform 13 | ✔️HackerRank 14 | 15 | ### 📖 Statement 16 | 17 | You have to create a struct, named Student, representing the student's details, as mentioned above, and store the data of a student. 18 | Input Format 19 | Input will consist of four lines. 20 | The first line will contain an integer, representing age. 21 | The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student. 22 | The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student. 23 | The fourth line will contain an integer, representing the standard of student. 24 | 25 | ### ✍️ Solution 26 | 27 | ``` 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | /* 36 | add code for struct here. 37 | */ 38 | struct Student{ 39 | int age; 40 | string first_name; 41 | string last_name; 42 | int standard; 43 | }; 44 | 45 | int main() { 46 | Student st; 47 | 48 | cin >> st.age >> st.first_name >> st.last_name >> st.standard; 49 | cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard; 50 | 51 | return 0; 52 | } 53 | ``` -------------------------------------------------------------------------------- /Assets/Codes/Miscellaneous/variableSizedArray.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | Variable Sized Array 3 | 4 | ### 💻 Language(s) 5 | 6 | 7 | 8 | 9 | 10 |
✔️ C++
11 | 12 | ### Question Plaform 13 | ✔️HackerRank 14 | 15 | ### 📖 Statement 16 | 17 | Consider an n-element array, , where each index i in the array contains a reference to an array of integers (where the value of varies from array to array). See the Explanation section below for a diagram. 18 | Given , you must answer queries. Each query is in the format i j, where denotes an index in array and denotes an index in the array located at . For each query, find and print the value of element in the array at location on a new line. 19 | 20 | ### ✍️ Solution 21 | 22 | ``` 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | 31 | int main() 32 | { 33 | int numberOfElements, queries, sizeOfElement, m, n; 34 | cin >> numberOfElements >> queries; 35 | std::vector< vector > numberGrid(numberOfElements); 36 | for (int i = 0; i < numberOfElements; i++){ 37 | cin >> sizeOfElement; 38 | numberGrid[i] = vector(sizeOfElement); 39 | for (int j = 0; j < sizeOfElement; j++){ 40 | cin >> numberGrid[i][j]; 41 | } 42 | } 43 | for (int i = 0; i < queries; i++){ 44 | cin >> m >> n; 45 | cout << numberGrid[m][n] << endl; 46 | } 47 | return 0; 48 | } 49 | ``` -------------------------------------------------------------------------------- /Assets/Codes/boilerPlate.md: -------------------------------------------------------------------------------- 1 | ## TITLE 2 | 3 | ### 💻 Language(s) 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
✔️ C++ Python Java C Other
14 | 15 | 21 | 22 | ### 📖 Statement 23 | 24 | Loreum Ipsm 25 | 26 | ### ✍️ Solution 27 | 28 | Loreum Ipsm 29 | 30 | ### 🤓 Explanation 31 | 32 | Optional 33 | -------------------------------------------------------------------------------- /Assets/Images/codeforces.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Assets/Images/dropdownButton.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Assets/Images/hacktoberfestLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnabiscreating/lazyCode/d1120d15ec5e4db4751d8b05429fafa14488622b/Assets/Images/hacktoberfestLogo.png -------------------------------------------------------------------------------- /Assets/Images/leetcode.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Assets/Images/scrollUp.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Assets/commandsGit.md: -------------------------------------------------------------------------------- 1 | 2 |

📝 Some useful GIT commands for beginners

3 | 4 | Configurations 5 | 6 | >`git config --global user.name “FirstName LastName”` 7 | 8 | >`git config --global user.email “your-email@email-provider.com”` 9 | 10 | >`git config --global colour.ui true` 11 | 12 | >`git config --list` 13 | 14 | Starting a Repository 15 | 16 | >`git init` 17 | 18 | >`git status` 19 | 20 | Staging Files 21 | 22 | >`git add [file-name]` 23 | 24 | >`git add [file-name] [another-file-name]` 25 | 26 | >`git add .` 27 | 28 | >`git add --all` 29 | 30 | >`git add -A` 31 | 32 | >`git rm --cached [file-name]` 33 | 34 | >`git reset [file-name]` 35 | 36 | Committing to a Repository 37 | 38 | > `git commit -m “Initial commit”` 39 | 40 | > `git commit --amend -m [enter your message]` 41 | 42 | Pulling and Pushing from and to Repositories 43 | 44 | >`git remote add origin [link]` 45 | 46 | >`git push -u origin master` 47 | 48 | >`git clone ` 49 | 50 | >`git pull` 51 | 52 | Branching 53 | 54 | >`git branch` 55 | 56 | >`git branch [branch-name]` 57 | 58 | >`git checkout [branch-name]` 59 | 60 | >`git merge [branch-name]` 61 | 62 | >`git checkout -b [branch-name]` 63 | -------------------------------------------------------------------------------- /Assets/introductionAndFeatures.md: -------------------------------------------------------------------------------- 1 | ## Hey, there fellow coders 👋, 2 | 3 | #### Welcome to `lazyCode`, an all-in-one destination for organizing your coding programs and to help you in enhancing your Data Structure and Algorithm (DSA) concepts while practicing coding skills. You can choose from or add files written in any programming language, compile and run codes from numerous competitive coding platforms such as `Codeforces`, `LeetCode` `HackerRank`, `CodeChef`, and many others under a single roof. 4 | 5 | --- 6 | 7 | ### Some salient features of our website: 8 | 9 | - **Organization** - The site helps to keep track of every program file that has been added. The dropdown gives you choice to filter question from the selected platform. :chart: 10 | - **Online compiler** - The site provides an online compiler to test and enhance your coding skills while being on the page itself. No more hassle of switching tabs to your installed IDE to run your code. :computer: 11 | - **Multi-Language Support** - The site provides you with a variety of programming languages to choose from so that you can be comfortable with a language that you are proficient with. :world_map: 12 | - **User friendly** - The simple yet engaging user interface helps you to completely focus on the programming problem that you are solving without any complicated distractions. :cherry_blossom: 13 | - **Diversity** - You are provided with a ton of programming and DSA problems from a variety of competitive coding platforms that you can solve. :books: 14 | 15 | - **Open to all** - The site is open to contribution from any coder around the globe. :earth_americas: 16 | 17 | --- 18 | -------------------------------------------------------------------------------- /Markups/homePageDemo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Ferb Fletcher's Webository | lazyCode 8 | 9 | 13 | 14 | 15 | 16 |
31 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Markups/questionPageDemo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | lazyCode | Codeforces Questions 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
34 | 35 | -------------------------------------------------------------------------------- /Markups/solutionPageDemo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | lazyCode | Solution 8 | 9 | 10 | 13 |
14 |
15 |

16 | Nulla porta viverra volutpat morbi cursus pharetra, nibh viverra enim. Cras in morbi massa facilisis id. Cursus a vel a sit et scelerisque sodales nisi. Ornare curabitur auctor sit suspendisse felis nec amet, lectus. Massa urna, eu ante sit placerat amet sit mi ut. Sed risus, fermentum sit amet. Egestas sed mauris sit vel. Vitae consequat nibh ultrices aliquam integer lobortis. Adipiscing ullamcorper gravida ipsum pretium convallis felis. Integer eget commodo arcu, dui, mi malesuada. 17 |

18 |
19 |
20 |
21 |                 
22 |                     
23 |                 
24 |             
25 |
26 |
27 | 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # :bookmark: lazyCode 2 | 3 | Hey there, my amigos,:wave: this open source project is different because we're taking your obsession with creating your Competitive coding / DSA GitHub repo to the next level by providing you with a full blown awesome beautiful amazing (okay, that's enough I guess) website template for your own website repository, where you could see your code shimmering like diamonds getting all of your undivided attention. 4 | But, hold on, what's that? 5 | What's the greatest place to seek for anything like that? Who will be the one to bring it to us? 6 | You, amigo, are the answer. Why wait for others to provide you with whatever you want if you have the imagination and the necessary skills? Go get it if you want something. (Enough with the motivational speeches, please. :trollface:) 7 | So, without further ado, let's get started. 8 | 9 | ## What more you should know :question: 10 | 11 | Everything will be done with HTML, CSS, and probably some javascript for good measure. Yes, react will be a better option for this, however, for the time being, this project wants young talented minds to apply their abilities and make substantial contributions to their first open source project while having fun, so why not keep things simple for now? After the hacktoberfest celebrations, we will move up a step and make this project with react while also adding some crazy functionality. 12 | 13 | During the hactoberfest season We're aiming to make a simple template so that you can clone and update your own codes with that hosting in your github pages for the duration of hactober fest, but as time goes on, we're aiming to make this a one-of-a-kind repository (web app) where you can all upload your unique codes under your wonderful names, share them, chat with others about codes, and have fun. 14 | 15 | Last but not least, here are the [designs](https://www.figma.com/file/nhmSygpLCznrCwPBmYEjdK/lazyCode?node-id=0%3A1) to get you started on this exciting trip :tada:. 16 | -------------------------------------------------------------------------------- /Scripts/index.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnabiscreating/lazyCode/d1120d15ec5e4db4751d8b05429fafa14488622b/Scripts/index.js -------------------------------------------------------------------------------- /Styles/homePageDemo.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@200;300;400;500;600;700;800;900&display=swap"); 2 | 3 | * { 4 | margin: 0; 5 | padding: 0; 6 | font-family: "Montserrat Alternates", sans-serif; 7 | color: white; 8 | letter-spacing: 2px; 9 | } 10 | 11 | nav { 12 | display: flex; 13 | justify-content: space-between; 14 | align-items: center; 15 | margin-bottom: 5%; 16 | } 17 | 18 | a { 19 | text-decoration: none; 20 | } 21 | 22 | h1 { 23 | width: 100px; 24 | font-weight: 900; 25 | font-size: 50px; 26 | line-height: 35px; 27 | letter-spacing: 0.1em; 28 | } 29 | 30 | nav h1 span { 31 | color: #8186b1; 32 | } 33 | 34 | h5 { 35 | margin-left: 5%; 36 | margin-bottom: 2%; 37 | width: 120px; 38 | } 39 | 40 | ul img { 41 | width: 25%; 42 | margin-left: 5%; 43 | } 44 | 45 | body { 46 | background-color: #1b1c26; 47 | margin: 3% 5% 5% 5%; 48 | } 49 | 50 | .platformButtons { 51 | width: 40%; 52 | } 53 | 54 | button { 55 | background: #292b3c; 56 | border-radius: 15px; 57 | width: 100%; 58 | height: 90px; 59 | border: none; 60 | margin-bottom: 6%; 61 | } 62 | 63 | article { 64 | display: flex; 65 | justify-content: space-between; 66 | align-items: center; 67 | } 68 | 69 | .introductionAndLazyCat { 70 | width: 50%; 71 | } 72 | 73 | p { 74 | font-family: Montserrat Alternates; 75 | font-style: normal; 76 | font-weight: bold; 77 | font-size: 20px; 78 | line-height: 35px; 79 | text-align: justify; 80 | letter-spacing: 0.1em; 81 | color: white; 82 | width: 100%; 83 | } 84 | 85 | .introductionAndLazyCat img { 86 | width: 100%; 87 | margin-top: 5%; 88 | } 89 | 90 | @media only screen and (min-width: 300px) and (max-width: 800px) { 91 | h1 { 92 | font-size: 28px; 93 | line-height: 20px; 94 | margin-right: 40%; 95 | } 96 | 97 | ul img { 98 | width: 25%; 99 | margin-left: 5%; 100 | } 101 | 102 | article { 103 | display: flex; 104 | justify-content: space-between; 105 | align-items: center; 106 | flex-wrap: wrap; 107 | } 108 | 109 | p { 110 | font-size: 18px; 111 | line-height: 25px; 112 | } 113 | 114 | .platformButtons { 115 | width: 100%; 116 | margin-top: 10%; 117 | } 118 | 119 | .introductionAndLazyCat { 120 | width: 100%; 121 | margin-top: 5%; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /Styles/questionPageDemo.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@200;300;400;500;600;700;800;900&display=swap"); 2 | 3 | * { 4 | margin: 0; 5 | padding: 0; 6 | font-family: "Montserrat Alternates", sans-serif; 7 | color: white; 8 | letter-spacing: 2px; 9 | } 10 | 11 | nav { 12 | display: flex; 13 | justify-content: space-between; 14 | align-items: center; 15 | margin-bottom: 5%; 16 | } 17 | 18 | h1 { 19 | width: 100px; 20 | font-weight: 900; 21 | font-size: 50px; 22 | line-height: 35px; 23 | letter-spacing: 0.1em; 24 | } 25 | 26 | nav h1 span { 27 | color: #8186b1; 28 | } 29 | 30 | body { 31 | background-color: #1b1c26; 32 | margin: 3% 5% 5% 5%; 33 | } 34 | 35 | main { 36 | display: flex; 37 | justify-content: space-around; 38 | flex-wrap: wrap; 39 | } 40 | 41 | main button { 42 | width: 400px; 43 | height: 70px; 44 | background: #292b3c; 45 | border-radius: 15px; 46 | border: none; 47 | margin-top: 5%; 48 | font-style: normal; 49 | font-weight: bold; 50 | font-size: 25px; 51 | line-height: 40px; 52 | letter-spacing: 0.1em; 53 | color: #ffffff; 54 | } 55 | 56 | main button span { 57 | font-style: normal; 58 | font-weight: bold; 59 | font-size: 35px; 60 | line-height: 40px; 61 | letter-spacing: 0.1em; 62 | color: #8186b1; 63 | } 64 | 65 | 66 | @media only screen and (min-width: 300px) and (max-width: 800px) { 67 | nav h1{ 68 | font-size: 28px; 69 | line-height: 20px; 70 | margin: 6% 40% 3% 4%; 71 | } 72 | 73 | main button { 74 | width: 340px; 75 | height: 60px; 76 | font-size: 20px; 77 | line-height: 40px; 78 | letter-spacing: 0.1em; 79 | color: #ffffff; 80 | } 81 | 82 | main button span { 83 | font-style: normal; 84 | font-weight: bold; 85 | font-size: 25px; 86 | line-height: 40px; 87 | letter-spacing: 0.1em; 88 | color: #8186b1; 89 | } 90 | } -------------------------------------------------------------------------------- /Styles/solutionPageDemo.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@200;300;400;500;600;700;800;900&display=swap"); 2 | 3 | * { 4 | margin: 0; 5 | padding: 0; 6 | font-family: "Montserrat Alternates", sans-serif; 7 | color: white; 8 | letter-spacing: 2px; 9 | } 10 | 11 | nav { 12 | display: flex; 13 | justify-content: space-between; 14 | align-items: center; 15 | margin-bottom: 5%; 16 | } 17 | 18 | a { 19 | text-decoration: none; 20 | } 21 | 22 | h1 { 23 | width: 100px; 24 | font-weight: 900; 25 | font-size: 50px; 26 | line-height: 35px; 27 | letter-spacing: 0.1em; 28 | } 29 | 30 | nav h1 span { 31 | color: #8186b1; 32 | } 33 | 34 | h5 { 35 | margin-left: 5%; 36 | margin-bottom: 2%; 37 | width: 120px; 38 | } 39 | 40 | ul img { 41 | width: 25%; 42 | margin-left: 5%; 43 | } 44 | 45 | body { 46 | background-color: #1b1c26; 47 | margin: 3% 5% 5% 5%; 48 | } --------------------------------------------------------------------------------