├── .DS_Store ├── .vscode ├── c_cpp_properties.json └── settings.json ├── Abhishek Khedekar └── README.md ├── Aditya Ashutosh └── Glossary_Blockchain_Basics.pdf ├── Aditya Khedkar └── test.txt ├── Aiman ├── Life_theUniverse_andEverything.cpp └── Readme.txt ├── Ashutosh ├── Counting valleys.cpp ├── SimilarWordsV2 │ ├── similarWordsV2.cpp │ ├── similarity.h │ ├── wordCount.txt │ └── wordData.txt ├── bubbleSort.cpp ├── findIfPalindrome └── tesitng2.cpp ├── Atharva Amrutkar ├── CodeForces │ └── chatroom.cpp ├── HackerEarth │ └── dislikes_and_party.cpp ├── Project_Euler_Math_Problems │ └── euler_Q1.cpp └── eulerQ_5.cpp ├── COVIDLQ.cpp ├── Geervani Reddy └── matrix program.txt ├── Kartik ├── Hackerrank questions │ ├── OrganisingContainers.cpp │ └── sherlock and squares ├── battleofvars │ └── the maths test solution └── file1 ├── Mayank └── Mayank │ ├── loop.py │ └── python.py ├── Ninad └── Hello.txt ├── Operations_on_a_Tuple.cpp ├── Operations_on_a_Tuple.undefined ├── PIYUSH BALI ├── .vscode │ ├── c_cpp_properties.json │ ├── launch.json │ ├── settings.json │ ├── tasks.exe │ └── tasks.json ├── A_plane_journey.cpp ├── Avoiding_visited_networked_paths.cpp ├── Changes_in_a_string.cpp ├── Cyclic_shifts.cpp ├── Dislikes_and_Party.cpp ├── Divisibility_Test_4.cpp ├── Divisiblity_by_160.cpp ├── Happy_segments.cpp ├── It's the sound of da POLICE.cpp ├── Number_of_divisors.cpp ├── Operations_on_a_Tuple.cpp ├── Operations_on_a_Tuple.cpp.prob ├── Operations_on_a_Tuple.exe ├── Printing_patterns.cpp ├── Removal_of_vertices.cpp ├── Template.cpp ├── a.exe ├── ans.cpp ├── book.pdf ├── cf.cpp ├── cf.exe ├── code.cpp ├── codes.cpp ├── codetreestick.cpp ├── codevita1.cpp ├── codevita2.cpp ├── codevita21.cpp ├── codevita22.cpp ├── codevita3.cpp ├── december2.cpp ├── edges.cpp ├── graphs.cpp ├── input.txt ├── output.txt ├── q11272020.cpp ├── savegf.cpp ├── test.exe └── tree.cpp ├── Pranav ├── cpp │ ├── 8queens.cpp │ ├── OrganizingContainersofBalls.cpp │ ├── PhoneBook.cpp │ ├── PrintingPatterns.cpp │ ├── Time-Conversion.cpp │ └── Typesofbeakers.cpp └── javascript │ └── Kalidoscope │ ├── ReadMe.txt │ ├── index.html │ ├── p5.js │ └── sketch.js ├── README.md ├── Rohit └── book.pdf ├── Sahil ├── Codeforces Div. 2, A │ ├── 10_Beautiful Matrix.cpp │ ├── 11_Arrival of the General.cpp │ ├── 1_Watermelon.cpp │ ├── 2_Way Too Long Words.cpp │ ├── 3_String Task.cpp │ ├── 4_Petya and Strings.cpp │ ├── 5_Helpful Maths.cpp │ ├── 6_Twins.cpp │ ├── 7_Chat room.cpp │ ├── 8_Lucky Division.cpp │ └── 9_Presents.cpp └── Templete.cpp ├── Sanket └── TestFile.txt ├── ShantanuK ├── BattleOfVars-1.java ├── BeautifulDaysAtMovies.java ├── BreakingRecords.java ├── Company.java ├── Employee.java ├── MarsExploration.java ├── PdfViewer.java ├── Socks.java ├── catmouse.java ├── chocolateFeast.java ├── countingValley.java ├── saveThePrisoner.java ├── utopianTree.java └── viralAdvertisment.java ├── Siddhesh ├── .DS_Store ├── Codeforces │ ├── DataCenter(Minimum Perimeter Given Area).cpp │ ├── ShovelsAndSwords.cpp │ └── a.out ├── Concepts │ ├── Binary Search │ │ ├── Backwards (Last occurring index of the element).cpp │ │ └── Forward (Lower bound- first occurring index of element).cpp │ ├── DP_Fibonacci.c │ ├── Kadane'sAlgorithm.cpp │ ├── Linked List │ │ ├── LinkedList_BasicFunctions.cpp │ │ └── TraverseLinkedList.cpp │ ├── LinkedList_BasicFunctions.cpp │ └── TraverseLinkedList.cpp ├── Hackerearth │ ├── Dislikes_And_Party(Handshakes).cpp │ └── PrintingPatterns.cpp ├── Hackerrank │ ├── BinaryReversal.cpp │ ├── CavityMap.cpp │ ├── NumberOfBlocks.cpp │ └── PlayWithBinaryString.cpp ├── Leetcode │ ├── AddBinary(String).cpp │ ├── AddTwoNumbersLinkedList.cpp │ ├── BackspaceStringCompare_0ms.cpp │ ├── BinaryTreePreorderTraversal.cpp │ ├── ClimbStairs(Using DP).cpp │ ├── CountAndSay.cpp │ ├── DesignCircularQueue.cpp │ ├── FindAllNumbersDisappearedInAnArray.cpp │ ├── FirstAndLastPositionOfElementInASortedArray.cpp │ ├── HeightChecker.cpp │ ├── ImplementStrStr.cpp │ ├── IntersectionOfTwoLinkedLists.cpp │ ├── LengthOfLastWord.cpp │ ├── LinkedListCycle.cpp │ ├── LinkedListCycleII.cpp │ ├── LongestCommonPrefix.cpp │ ├── MaximumDepthOfBinaryTree.cpp │ ├── MaximumSubarray(Kadane's Algorithm).cpp │ ├── MergeTwoSortedLinkedLists.cpp │ ├── MergeTwoSortedLists.cpp │ ├── MoveZeroesAtEnd.cpp │ ├── NumberOfIslands.cpp │ ├── OddEvenLinkedList.cpp │ ├── OpenTheLock.cpp │ ├── PalindromeLinkedList.cpp │ ├── Palindrome_0ms.cpp │ ├── PathSum.cpp │ ├── PlusOne.cpp │ ├── RemoveDuplicatesFromSortedArray.cpp │ ├── RemoveDuplicatesIIFromSortedLinkedList.cpp │ ├── RemoveDuplicatesILinkedList.cpp │ ├── RemoveElement.cpp │ ├── RemoveLinkedListElements.cpp │ ├── RemoveNthNodeFromEndOfLinkedList.cpp │ ├── ReplaceElementsWithGreatestElementOnRightSide.cpp │ ├── ReverseInteger_0ms.cpp │ ├── ReverseInteger_0ms.textClipping │ ├── ReverseLinkedList.cpp │ ├── RomanToInteger.cpp │ ├── RotateLinkedList.cpp │ ├── SearchInsertPosition.cpp │ ├── SingleNumber.cpp │ ├── SortArrayByParity.cpp │ ├── Sqrt(Using Binary Search).cpp │ ├── SwapNodesInPairsLinkedList.cpp │ ├── SymmetricTree.cpp │ ├── ThirdMaximumNumber.cpp │ ├── TwoSum.cpp │ ├── ValidMoumtainArray.cpp │ ├── ValidParentheses.cpp │ ├── _BinaryTreeInorderTraversal.cpp │ ├── _BinaryTreeLevelOrderTraversal.cpp │ ├── _BinaryTreePostOrderTraversal.cpp │ └── _PopulatingNextRightPointersInEachNode.cpp ├── Notes │ ├── FindAllNumbersDisappearedInAnArray.jpg │ ├── Screenshot_2020-06-02-21-18-49-48.jpg │ └── a.out ├── Playground │ ├── a.out │ ├── input.txt │ ├── output.txt │ └── playground.cpp ├── Template.cpp ├── a.out └── foobar_withgoogle │ ├── Level 1 │ └── braille.py │ └── Level 2 │ └── maximum_product_of_a_subset_of_an_array.py ├── Sourabh └── book.pdf ├── Tanishq ├── Vatsal Arya ├── CodeChef │ └── BytelandianGoldCoins.cpp ├── HackerEarth │ ├── BattleOfStalingrad.cpp │ ├── Bob's_Journey.cpp │ ├── EncodedStrings.cpp │ ├── New Text Document.txt │ ├── Palindromic_Numbers.cpp │ ├── UtkarshAndDistributingBooks.cpp │ ├── clmbing_stairs.cpp │ └── readme.txt └── readme.txt ├── Vishal Patil ├── 10_beautiful_matrix.c ├── 12_game_with_sticks.c ├── 13_games.c ├── 15_young_physicist.c ├── 16_puzzles.cpp ├── 17_expression.c ├── 1_watermelon.c ├── 20_even_or_odd.c ├── 2_way_too_long_words.c ├── 5_helpful_math.cpp ├── 6_twin.cpp ├── 8_lucky_divison.c └── readme.md ├── file.cpp └── shubham ├── det.c ├── det.exe └── readme.c /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/.DS_Store -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe", 14 | "cStandard": "c11", 15 | "cppStandard": "c++17", 16 | "intelliSenseMode": "msvc-x64" 17 | } 18 | ], 19 | "version": 4 20 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "competitive-programming-helper.firstTime": false 3 | } -------------------------------------------------------------------------------- /Abhishek Khedekar/README.md: -------------------------------------------------------------------------------- 1 | # competitive-coding 2 | This repo is competitive coding programmers of viit 3 | -------------------------------------------------------------------------------- /Aditya Ashutosh/Glossary_Blockchain_Basics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Aditya Ashutosh/Glossary_Blockchain_Basics.pdf -------------------------------------------------------------------------------- /Aditya Khedkar/test.txt: -------------------------------------------------------------------------------- 1 | this is a test file. -------------------------------------------------------------------------------- /Aiman/Life_theUniverse_andEverything.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int num; 7 | while (cin >> num) { 8 | if ( num == 42 ) 9 | break; 10 | cout << num << endl; 11 | } 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Aiman/Readme.txt: -------------------------------------------------------------------------------- 1 | This is folder of Aiman Shivani 2 | -------------------------------------------------------------------------------- /Ashutosh/Counting valleys.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countingValleys(int numOfSteps, char* path) 5 | { 6 | int altitude = 0, i, valleyInProgress = 0, numOfValleys = 0 ; //initialize all the variables as 0 7 | for(i=0 ; i>numOfSteps; 37 | char path[numOfSteps]; 38 | 39 | for(i=0 ; i>path[i] ; 42 | } 43 | 44 | numOfValleys = countingValleys(numOfSteps, path); 45 | cout<<"\n"; 46 | cout< 2 | #include "similarity.h" 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | int main() 9 | { int cont, num = readNum(); 10 | system("CLS"); 11 | 12 | 13 | do{ 14 | cont =0; 15 | system("CLS"); 16 | switch(chooseOperation()) 17 | { 18 | case 1: 19 | num = addWord(num) ; 20 | break; 21 | case 2: 22 | searchWord(num) ; 23 | break; 24 | case 3: 25 | printCurrentNumberOfWords(num); 26 | break; 27 | case 4: 28 | searchSimilarWords(num); 29 | break; 30 | case 5: 31 | printAllWords(num) ; 32 | break; 33 | 34 | } 35 | cout<<"\nPress 1 to continue\n"; 36 | cin>>cont; 37 | } 38 | while(cont == 1); 39 | return 0; 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /Ashutosh/SimilarWordsV2/wordCount.txt: -------------------------------------------------------------------------------- 1 | 7 -------------------------------------------------------------------------------- /Ashutosh/SimilarWordsV2/wordData.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Ashutosh/SimilarWordsV2/wordData.txt -------------------------------------------------------------------------------- /Ashutosh/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int checkIfSolvable(int* arr, int numOfElements) 5 | { int i, difference; 6 | 7 | for(i=0 ; i 0) 13 | difference --; 14 | 15 | if(difference < -2 || difference > 2) 16 | return 0; 17 | 18 | } 19 | 20 | return 1; 21 | 22 | } 23 | int minimumBribes(int* arr, int numOfElements) 24 | { 25 | int i,j , temp, numOfBribes = 0; 26 | for(i=0 ; i arr[j+1]) 31 | { 32 | temp = arr[j]; 33 | arr[j] = arr[j+1]; 34 | arr[j+1] = temp; 35 | numOfBribes++ ; 36 | 37 | 38 | } 39 | } 40 | } 41 | return numOfBribes ; 42 | } 43 | int main() 44 | { 45 | 46 | int numOfTestCases ; 47 | cin>>numOfTestCases ; 48 | int result[numOfTestCases] ; 49 | int numOfTestCasesUse = numOfTestCases ; 50 | 51 | while(numOfTestCases > 0) 52 | { 53 | 54 | int i, numOfElements ; 55 | cin>> numOfElements ; 56 | int array[numOfElements] ; 57 | for(i=0 ; i>array[i]; 60 | 61 | } 62 | 63 | result[numOfTestCases] = checkIfSolvable(array, numOfElements) ; 64 | if(result[numOfTestCases] == 0) 65 | { /*random thing*/ 66 | 67 | } 68 | 69 | else 70 | { 71 | result[numOfTestCases] = minimumBribes(array, numOfElements) ; 72 | 73 | } 74 | numOfTestCases --; 75 | 76 | 77 | } 78 | 79 | while(numOfTestCasesUse > 0) 80 | { 81 | if(result[numOfTestCasesUse] == 0) 82 | cout<<"Too chaotic\n"; 83 | else 84 | cout< 6 | #include 7 | using namespace std; 8 | 9 | inline void deletePosition(char* input, int position) 10 | { 11 | for(int i=position ; i< ( strlen(input) - 1) ; i++) 12 | { 13 | input[i] = input[i+1]; 14 | } 15 | 16 | } 17 | 18 | inline bool findIfPalindrome(char* input) 19 | { int len, originalLen, j, i=0; 20 | bool elementFound = false; 21 | 22 | len = strlen(input); 23 | int notFound=0; 24 | 25 | originalLen = len; 26 | 27 | while(i1) 28 | { 29 | 30 | if(notFound>1) 31 | return false; 32 | for(j=1 ; j>input; 70 | if(findIfPalindrome(input)) 71 | cout<<"Yes"; 72 | else 73 | cout<<"No"; 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Ashutosh/tesitng2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int findHourGlassSum(int arr[7][7], int r, int c) 6 | { 7 | int sum=0, i; 8 | for (i = c ; i < (c+3) ; i++) 9 | { sum = sum + arr[r][i]; 10 | } 11 | r++; 12 | c++; 13 | sum = sum + arr[r][c]; 14 | r++; 15 | c--; 16 | for (i = c ; i < (c+3) ; i++) 17 | { sum = sum + arr[r][i]; 18 | } 19 | return sum; 20 | } 21 | int main() 22 | { 23 | 24 | int arr[7][7], r, c, sum[16], i,z=0; 25 | for (r =1 ; r<7 ; r++) 26 | { for(c=1 ; c<7 ; c++) 27 | { cin >> arr[r][c]; 28 | } 29 | } 30 | 31 | 32 | /* for (r =1 ; r<7 ; r++) 33 | { for(c=1 ; c<7 ; c++) 34 | { cout << arr[r][c]; 35 | cout << " "; 36 | } 37 | cout <<"\n"; 38 | 39 | } 40 | */ 41 | for (r=1 ; r <=4 ; r++) 42 | { for (c=1 ; c<=4 ; c++) 43 | { 44 | 45 | sum[z] = findHourGlassSum(arr, r, c) ; 46 | z++; 47 | 48 | } 49 | } 50 | 51 | for(i=0 ; i<16 ; i++) 52 | { cout << sum[i]; 53 | cout << " "; 54 | } 55 | 56 | int max; 57 | max = sum[0]; 58 | for (i=1 ; i<16 ; i++) 59 | { if (sum[i]> max) 60 | max = sum[i]; 61 | } 62 | cout<<"The max value of sum is "< 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | string word="hello"; 12 | string type; 13 | cin>>type; 14 | int length=type.length(); 15 | int a=0; 16 | int i, count = 0; 17 | for(i=0; i 2 | int main(void) 3 | { 4 | int count=0,count1=0,h; 5 | int a[100][100]; 6 | long long int b,fac=1,fac1=1,x,ans; 7 | scanf("%lld",&b); 8 | for(int i=0;i<10;i++) 9 | { 10 | for(int j=0;j<10;j++) 11 | { 12 | scanf("%d",&a[i][j]); 13 | } 14 | } 15 | 16 | for(int i=0;i<10;i++) 17 | { 18 | for(int j=1;j<10;j++) 19 | { 20 | for(h=i+1;h<10;h++) 21 | { 22 | if(a[i][j]==a[h][0]) 23 | { 24 | for(long long int temp=1;temp<10;temp++) 25 | { 26 | if(a[h][temp]==a[i][0]) 27 | { 28 | a[h][temp]=-1000; 29 | count1++; 30 | break; 31 | } 32 | } 33 | if(count1>0) 34 | break; 35 | } 36 | } 37 | } 38 | } 39 | for(int i=0;i<10;i++) 40 | { 41 | for(int j=1;j<10;j++) 42 | { 43 | if(a[i][j]!=-1000) 44 | { 45 | count++; 46 | } 47 | } 48 | } 49 | ans=(b*(b-1))/2; 50 | ans-=count; 51 | printf("%lld",ans); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Atharva Amrutkar/Project_Euler_Math_Problems/euler_Q1.cpp: -------------------------------------------------------------------------------- 1 | /*Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, 2 | the first 10 terms will be: 3 | 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 4 | By considering the terms in the Fibonacci sequence whose values do not exceed four million, 5 | find the sum of the even-valued terms.*/ 6 | #include 7 | #include 8 | int main() 9 | { 10 | long long i,a=1,b=2; 11 | long long term,sum=0,n=4000000; 12 | while(a<=n) 13 | { 14 | term=a; 15 | a=b; 16 | b+=term; 17 | if(term%2==0) 18 | { 19 | sum+=term; 20 | } 21 | } 22 | printf("\nThe summation of even terms in fbonacci series less than 4M is \n sum=%lld",sum); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Atharva Amrutkar/eulerQ_5.cpp: -------------------------------------------------------------------------------- 1 | /*2520 is the smallest number that can be divided by each of the numbers from 1 to 10 2 | without any remainder. 3 | What is the smallest positive number that is evenly divisible by 4 | all of the numbers from 1 to 20?*/ 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | long long lcm(long long n) 10 | { 11 | long long ans = 1; 12 | for (long long i = 1; i <= n; i++) 13 | ans = (ans * i)/(__gcd(ans, i)); 14 | return ans; 15 | } 16 | int main() 17 | { 18 | long long n = 20; 19 | cout << lcm(n); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /COVIDLQ.cpp: -------------------------------------------------------------------------------- 1 | /*Due to the COVID pandemic, people have been advised to stay at least 6 feet away from any other person. Now, people are lining up in a queue at the local shop and it is your duty to check whether they are all following this advice. 2 | 3 | There are a total of N spots (numbered 1 through N) where people can stand in front of the local shop. The distance between each pair of adjacent spots is 1 foot. Each spot may be either empty or occupied; you are given a sequence A1,A2,…,AN, where for each valid i, Ai=0 means that the i-th spot is empty, while Ai=1 means that there is a person standing at this spot. It is guaranteed that the queue is not completely empty. 4 | 5 | For example, if N=11 and the sequence A is (0,1,0,0,0,0,0,1,0,0,1), then this is a queue in which people are not following the advice because there are two people at a distance of just 3 feet from each other. 6 | 7 | You need to determine whether the people outside the local shop are following the social distancing advice or not. As long as some two people are standing at a distance smaller than 6 feet from each other, it is bad and you should report it, since social distancing is not being followed. 8 | 9 | Input 10 | The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. 11 | The first line of each test case contains a single integer N. 12 | The next line contains N space-separated integers A1,A2,…,AN. 13 | Output 14 | For each test case, print a single line containing the string "YES" if social distancing is being followed or "NO" otherwise (without quotes). 15 | 16 | Constraints 17 | 1≤T≤100 18 | 1≤N≤100 19 | 0≤Ai≤1 for each valid i 20 | at least one spot is occupied 21 | Subtasks 22 | Subtask #1 (100 points): original constraints 23 | 24 | Example Input 25 | 3 26 | 3 27 | 1 0 1 28 | 7 29 | 1 0 0 0 0 0 1 30 | 11 31 | 0 1 0 0 0 0 0 1 0 0 1 32 | Example Output 33 | NO 34 | YES 35 | NO 36 | Explanation 37 | Example case 1: The first and third spots are occupied and the distance between them is 2 feet. 38 | 39 | Example case 2: The first and seventh spots are occupied and the distance between them is 6 feet.*/ 40 | 41 | 42 | #include 43 | using namespace std; 44 | 45 | int main() { 46 | int test; 47 | cin>>test; 48 | while(test--){ 49 | int n; 50 | cin>>n; 51 | int a[n]; 52 | for(int i=0;i>a[i]; 54 | } 55 | bool res=true; 56 | for(int i=0;i= 10) 10 | System.out.println("INVALID INPUT:"); 11 | }while(N < 1 || N > 366); 12 | Matrix obj = new Matrix(N); 13 | System.out.println("Enter the elements of single dimensional array:"); 14 | obj.inputSingleDimArray(); 15 | obj.SortMatrix(); 16 | obj.inputElementsToTwoDimMatrix(); 17 | obj.displayMatrix(); 18 | in.close(); 19 | } 20 | } 21 | 22 | class Matrix{ 23 | Matrix(int N){ 24 | order = N; 25 | single_dim_array = new int[order]; 26 | Mat = new int[order][order]; 27 | } 28 | public void inputElementsToTwoDimMatrix() { 29 | for(int i = 0; i < order; i++) { 30 | for(int j = 0; j < order; j++) { 31 | if(i+j < order) 32 | Mat[i][j]=single_dim_array[j]; 33 | else 34 | Mat[i][j]=single_dim_array[(i+j)-order]; 35 | } 36 | } 37 | 38 | } 39 | 40 | public void inputSingleDimArray() { 41 | Scanner in = new Scanner(System.in); 42 | for(int i=0;i single_dim_array[j]) { 57 | temp = single_dim_array[j]; 58 | single_dim_array[j]=single_dim_array[i]; 59 | single_dim_array[i]=temp; 60 | } 61 | } 62 | } 63 | } 64 | 65 | public void displayMatrix() { 66 | System.out.println("SINGLE DIMENSIONAL ARRAY AFTER SORTING:"); 67 | for(int k=0;k 2 | 3 | using namespace std; 4 | 5 | // Complete the organizingContainers function below. 6 | string organizingContainers(vector> container) { 7 | int i,j,a,b,x; 8 | long long int r[100],c[100]; 9 | 10 | 11 | for( i=0; i> q; 76 | cin.ignore(numeric_limits::max(), '\n'); 77 | 78 | for (int q_itr = 0; q_itr < q; q_itr++) { 79 | int n; 80 | cin >> n; 81 | cin.ignore(numeric_limits::max(), '\n'); 82 | 83 | vector> container(n); 84 | for (int i = 0; i < n; i++) { 85 | container[i].resize(n); 86 | 87 | for (int j = 0; j < n; j++) { 88 | cin >> container[i][j]; 89 | } 90 | 91 | cin.ignore(numeric_limits::max(), '\n'); 92 | } 93 | 94 | string result = organizingContainers(container); 95 | 96 | fout << result << "\n"; 97 | } 98 | 99 | fout.close(); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /Kartik/Hackerrank questions/sherlock and squares: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the squares function below. 8 | int squares(int a, int b) 9 | { 10 | long count = 0; 11 | long long int s = sqrt(a); 12 | cout<> q; 29 | cin.ignore(numeric_limits::max(), '\n'); 30 | 31 | for (int q_itr = 0; q_itr < q; q_itr++) { 32 | string ab_temp; 33 | getline(cin, ab_temp); 34 | 35 | vector ab = split_string(ab_temp); 36 | 37 | int a = stoi(ab[0]); 38 | 39 | int b = stoi(ab[1]); 40 | 41 | int result = squares(a, b); 42 | 43 | fout << result << "\n"; 44 | } 45 | 46 | fout.close(); 47 | 48 | return 0; 49 | } 50 | 51 | vector split_string(string input_string) { 52 | string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { 53 | return x == y and x == ' '; 54 | }); 55 | 56 | input_string.erase(new_end, input_string.end()); 57 | 58 | while (input_string[input_string.length() - 1] == ' ') { 59 | input_string.pop_back(); 60 | } 61 | 62 | vector splits; 63 | char delimiter = ' '; 64 | 65 | size_t i = 0; 66 | size_t pos = input_string.find(delimiter); 67 | 68 | while (pos != string::npos) { 69 | splits.push_back(input_string.substr(i, pos - i)); 70 | 71 | i = pos + 1; 72 | pos = input_string.find(delimiter, i); 73 | } 74 | 75 | splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); 76 | 77 | return splits; 78 | } 79 | -------------------------------------------------------------------------------- /Kartik/battleofvars/the maths test solution: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | //Enter your code here. Read input from STDIN. Print output to STDOUT 12 | 13 | int n,t,cnt=0; 14 | cin>>n; 15 | vector a; 16 | for(int i=0;i" 4 | # 1 "" 5 | # 1 "c:\\Users\\pnbal\\Desktop\\daa\\competitive-coding\\PIYUSH BALI\\.vscode\\tasks.json" 6 | { 7 | "version": "2.0.0", 8 | "tasks": [ 9 | { 10 | "type": "shell", 11 | "label": "C/C++: cpp.exe build active file", 12 | "command": "C:\\Mingw-w64\\mingw32\\bin\\cpp.exe", 13 | "args": [ 14 | "-g", 15 | "${file}", 16 | "-o", 17 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 18 | ], 19 | "options": { 20 | "cwd": "${workspaceFolder}" 21 | }, 22 | "problemMatcher": [ 23 | "$gcc" 24 | ], 25 | "group": { 26 | "kind": "build", 27 | "isDefault": true 28 | } 29 | } 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /PIYUSH BALI/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | //Author : 5hfT 2 | //data:11th Feb,2020 3 | //Task:shortcut to run a cpp file which will take input and show output of the programme automatically with programme runtime status such as memory comsume and execution time 4 | { 5 | "version": "2.0.0", 6 | "tasks": [ 7 | { 8 | "label": "Compile and run", 9 | "type": "shell", 10 | "command": "", 11 | "args": [ 12 | "g++", 13 | "${relativeFile}", 14 | "-o", 15 | "test", 16 | "&&", 17 | "test", 18 | "<", 19 | "input.txt", 20 | ">", 21 | "output.txt" 22 | ], 23 | "group": { 24 | "kind": "build", 25 | "isDefault": true 26 | }, 27 | "problemMatcher": { 28 | "owner": "cpp", 29 | "fileLocation": [ 30 | "relative", 31 | "${workspaceRoot}" 32 | ], 33 | "pattern": { 34 | "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", 35 | "file": 1, 36 | "line": 2, 37 | "column": 3, 38 | "severity": 4, 39 | "message": 5 40 | } 41 | } 42 | }, 43 | { 44 | "type": "cppbuild", 45 | "label": "C/C++: cpp.exe build active file", 46 | "command": "G:\\mingw\\mingw32\\bin\\cpp.exe", 47 | "args": [ 48 | "-g", 49 | "${file}", 50 | "-o", 51 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 52 | ], 53 | "options": { 54 | "cwd": "G:\\mingw\\mingw32\\bin" 55 | }, 56 | "problemMatcher": [ 57 | "$gcc" 58 | ], 59 | "group": "build", 60 | "detail": "compiler: G:\\mingw\\mingw32\\bin\\cpp.exe" 61 | } 62 | ] 63 | } -------------------------------------------------------------------------------- /PIYUSH BALI/A_plane_journey.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i A) 31 | { 32 | for(int i=A.size()-1;i>=0;i--) 33 | { 34 | if(taken[i]==false && A[i]<=cap) 35 | { 36 | taken[i]=true; 37 | return 1; 38 | } 39 | } 40 | return 0; 41 | } 42 | signed main() 43 | { 44 | int n,m; 45 | cin>>n>>m; 46 | vector A(n); 47 | vector B(m); 48 | forci(0,n) 49 | { 50 | cin>>A[i]; 51 | } 52 | forci(0,m) 53 | { 54 | cin>>B[i]; 55 | } 56 | sort(A.begin(),A.end()); 57 | sort(B.begin(),B.end()); 58 | if(A[n-1]>B[m-1]) 59 | { 60 | cout<<"-1"; 61 | } 62 | bool taken[n]={ false }; 63 | bool uddan[m]={ false }; 64 | vector trip(m,0); 65 | int ans= 0; 66 | //cout<()); 102 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>fact[],ll i) 33 | { 34 | int a=0,b=0,c=0; 35 | ll q=n; 36 | while(q%107==0) 37 | { 38 | a++; 39 | q=q/107; 40 | } 41 | q=n; 42 | while(q%1361==0) 43 | { 44 | b++; 45 | q=q/1361; 46 | } 47 | q=n; 48 | while(q%10000019==0) 49 | { 50 | c++; 51 | q=q/10000019; 52 | } 53 | //cout<>fact[],int n,int m) 59 | { 60 | 61 | if(x==n && y==m) 62 | { 63 | return 1;} 64 | if(x>n || y>m ) 65 | { 66 | return 0; 67 | } 68 | 69 | if(dp[x][y]!=-1) 70 | {return dp[x][y];} 71 | 72 | q=get<0>(fact[x][y]); 73 | z=get<1>(fact[x][y]); 74 | y=get<2>(fact[x][y]); 75 | 76 | 77 | if(q==2 && z==2 && y==1) 78 | { 79 | return 0; 80 | } 81 | int right=count(x,y+1,fact,n,m); 82 | int left=count(x+1,y,fact,n,m); 83 | 84 | return dp[x][y]=right+left; 85 | 86 | 87 | 88 | } 89 | 90 | signed main() 91 | { 92 | bolt 93 | ll n,m,a; 94 | cin>>n>>m; 95 | // vector A[n]; 96 | vector>fact[n]; 97 | 98 | memset(dp,-1,sizeof dp); 99 | forci(0,n) 100 | { 101 | 102 | forcj(0,m) 103 | { 104 | cin>>a; 105 | 106 | factorize(a,fact,i); 107 | 108 | } 109 | } 110 | 111 | cout<(fact[i][j]); 120 | // // cout<(fact[i][j]); 121 | // // cout<(fact[i][j])<<" "; 122 | 123 | // } 124 | // cout<<"/n"; 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | } 137 | 138 | //https://www.youtube.com/watch?v=yl2GaxVGUaI -------------------------------------------------------------------------------- /PIYUSH BALI/Changes_in_a_string.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>n; 42 | cin>>s; 43 | cntb=0;cnta=0;minseta=-1e9;cntpos=0; 44 | forci(0,n) 45 | { 46 | if(s[i]=='A') 47 | { 48 | cnta++; 49 | } 50 | } 51 | cntpos=cnta; 52 | forci(0,n) 53 | { 54 | if(s[i]=='A') 55 | { 56 | cnta=cnta-1; 57 | } 58 | else 59 | { 60 | cntb++; 61 | } 62 | cntpos=((cntpos)>=(cnta+cntb))?cnta+cntb:cntpos; 63 | } 64 | 65 | cout< 4 | #include 5 | using namespace std; 6 | #define INT_BITS 16 7 | class gfg 8 | { 9 | 10 | /*Function to left rotate n by d bits*/ 11 | public: 12 | uint16_t leftRotate(uint16_t n, uint16_t d) 13 | { 14 | 15 | /* In n<>(INT_BITS - d) */ 19 | return ((n) << d)|(n >> (INT_BITS - d)); 20 | } 21 | 22 | /*Function to right rotate n by d bits*/ 23 | uint16_t rightRotate(uint16_t n, uint16_t d) 24 | { 25 | /* In n>>d, first d bits are 0. 26 | To put last 3 bits of at 27 | first, do bitwise or of n>>d 28 | with n <<(INT_BITS - d) */ 29 | return ((n) >> d)|(n << (INT_BITS - d)); 30 | } 31 | }; 32 | 33 | /* Driver code*/ 34 | int main() 35 | { 36 | gfg g; 37 | int t;cin>>t;while(t--) 38 | { 39 | uint16_t n; 40 | uint16_t d; 41 | char c; 42 | cin>>n>>d>>c; 43 | if(c=='L') 44 | { 45 | cout << g.leftRotate(n, d)<<"\n";} 46 | else 47 | { 48 | cout << g.rightRotate(n, d)<<"\n"; 49 | } 50 | } 51 | } 52 | 53 | // This code is contributed by SoM15242 54 | -------------------------------------------------------------------------------- /PIYUSH BALI/Dislikes_and_Party.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | 6 | using namespace std; 7 | int findincol(int hand[10][10],int value) 8 | { 9 | for(int i=0 ;i<10;i++) 10 | { 11 | if(hand[i][0]==value) 12 | { 13 | return i; 14 | } 15 | } 16 | return -1; 17 | } 18 | void findinrow(int hand[10][10],int value,int q) 19 | { 20 | if(q==-1) 21 | { 22 | return; 23 | } 24 | for(int i=0;i<10;i++) 25 | { //cout<>n; 39 | int hand[10][10]={0}; 40 | for(int i=0;i<10;i++) 41 | { cin>>a; 42 | hand[i][0]=a; 43 | // cout<>b; 47 | hand[i][j]=b; 48 | 49 | } 50 | //cout<<"\n"; 51 | } 52 | for(int i=0;i<10;i++) 53 | { 54 | a=hand[i][0]; 55 | // cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>str; 51 | check(str)? cout << y: cout << n; 52 | cout<<"\n"; 53 | } 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | } 95 | 96 | //https://www.youtube.com/watch?v=yl2GaxVGUaI -------------------------------------------------------------------------------- /PIYUSH BALI/Divisiblity_by_160.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int cache[51]; 4 | int number=160; 5 | int numberofterm(int digit, int number) 6 | { 7 | int firstnum = (int)(pow(10, digit - 1)); 8 | int lastnum = (int)(pow(10, digit)); 9 | 10 | firstnum = (firstnum - firstnum % number) + number; 11 | 12 | 13 | lastnum = (lastnum - lastnum % number); 14 | 15 | 16 | return ((lastnum - firstnum) / number + 1); 17 | } 18 | 19 | int main() 20 | { 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /PIYUSH BALI/Happy_segments.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>n>>m; 34 | int arr[n]; 35 | int h[m]; 36 | forci(0,n) 37 | { 38 | cin>>arr[i]; 39 | } 40 | forci(0,m) 41 | { 42 | cin>>h[i]; 43 | } 44 | int dp[n][m+1]; 45 | memset(dp, 0, sizeof dp); 46 | dp[0][arr[0]]++; 47 | forci(1,n) 48 | { 49 | forcj(1,m) 50 | { 51 | 52 | } 53 | } 54 | 55 | forci(0,n) 56 | { 57 | forcj(0,m+1) 58 | { 59 | cout< 2 | using namespace std; 3 | 4 | int primeFactors(int n) 5 | { 6 | int ans=1; 7 | 8 | while (n % 2 == 0) 9 | { 10 | ans=ans*2; 11 | //cout << 2 << " "; 12 | n = n/2; 13 | } 14 | 15 | for (int i = 3; i <= sqrt(n); i = i + 2) 16 | { 17 | // While i divides n, print i and divide n 18 | 19 | while (n % i == 0) 20 | { 21 | //cout << i << " "; 22 | if(i==3) 23 | { 24 | ans=ans*3; 25 | } 26 | else 27 | { 28 | ans=ans*2; 29 | } 30 | n = n/i; 31 | } 32 | } 33 | 34 | if (n > 2) 35 | ans=ans*2; 36 | 37 | return ans; 38 | 39 | } 40 | 41 | 42 | int main() 43 | { 44 | int n; 45 | cin>>n; 46 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i arr(1000000); 30 | ll kill(ll n,ll k) 31 | { ll a=n; 32 | while(n%k==0) 33 | { 34 | if(arr[n]!=0) 35 | { 36 | return arr[n]; 37 | } 38 | n=n/k; 39 | 40 | } 41 | arr[a]=n; 42 | return n; 43 | } 44 | signed main() 45 | { 46 | test{ 47 | ll a,b; 48 | cin>>a>>b; 49 | ll ans=0; 50 | ans=(a*(a+1))/2; 51 | ll diff=0; 52 | fill(arr.begin(),arr.end(),0); 53 | 54 | 55 | for(ll i=b;i<=a;i=i+b) 56 | { 57 | diff+=i; 58 | ans=ans+kill(i,b); 59 | } 60 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>a[0]>>a[1]>>a[2]; 45 | ll p[3]; 46 | cin>>p[0]>>p[1]>>p[2]; 47 | 48 | 49 | 50 | } 51 | 52 | 53 | } 54 | 55 | //https://www.youtube.com/watch?v=yl2GaxVGUaI -------------------------------------------------------------------------------- /PIYUSH BALI/Operations_on_a_Tuple.cpp.prob: -------------------------------------------------------------------------------- 1 | {"group":"CodeChef - June Challenge 2020 Division 2","input":{"type":"stdin"},"interactive":false,"languages":{"java":{"mainClass":"Main","taskClass":"OperationsOnATuple"}},"memoryLimit":256,"name":"Operations on a Tuple","output":{"type":"stdout"},"srcPath":"c:\\Users\\pnbal\\Desktop\\daa\\competitive-coding\\PIYUSH BALI\\Operations_on_a_Tuple.cpp","testType":"single","tests":[{"id":1591627856211,"input":"2\n3 5 7\n6 5 10\n8 6 3\n9 7 8\n","output":"1\n2\n"}],"timeLimit":1000,"url":"https://www.codechef.com/JUNE20B/problems/TTUPLE"} -------------------------------------------------------------------------------- /PIYUSH BALI/Printing_patterns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | 5 | const int maxn = 3e2 + 14; 6 | int main(){ 7 | ios::sync_with_stdio(0), cin.tie(0); 8 | int r, c, x, y; 9 | cin >> r >> c >> x >> y; 10 | for(int i = 0; i < r; i++) 11 | for(int j = 0; j < c; j++) 12 | cout << max(abs(i - x), abs(j - y)) << " \n"[j==c-1]; 13 | } -------------------------------------------------------------------------------- /PIYUSH BALI/Removal_of_vertices.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;in=a; 37 | 38 | } 39 | 40 | }; 41 | signed main() 42 | { 43 | ll N,K; 44 | test 45 | { 46 | 47 | N=0,K=0; 48 | cin>>N>>K; 49 | ll v=(((2*K)%N)+1); 50 | forci(0,N) 51 | { 52 | 53 | 54 | } 55 | } 56 | 57 | 58 | 59 | 60 | } 61 | 62 | //https://www.youtube.com/watch?v=yl2GaxVGUaI -------------------------------------------------------------------------------- /PIYUSH BALI/Template.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>a; 34 | cout< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | typedef long long ll; 26 | typedef long double ld; 27 | typedef pair p32; 28 | typedef pair p64; 29 | typedef pair pdd; 30 | typedef vector v64; 31 | typedef vector v32; 32 | typedef vector > vv32; 33 | typedef vector > vv64; 34 | typedef vector > vvp64; 35 | typedef vector vp64; 36 | typedef vector vp32; 37 | ll MOD = 998244353; 38 | double eps = 1e-12; 39 | #define forn(i,e) for(ll i = 0; i < e; i++) 40 | #define forsn(i,s,e) for(ll i = s; i < e; i++) 41 | #define rforn(i,s) for(ll i = s; i >= 0; i--) 42 | #define rforsn(i,s,e) for(ll i = s; i >= e; i--) 43 | #define ln "\n" 44 | #define dbg(x) cout<<#x<<" = "<>n>>c0>>c1; 58 | v32 arr(n,0); 59 | forn(i,n) 60 | { 61 | cin>>arr[i]; 62 | } 63 | 64 | 65 | 66 | } 67 | int main() 68 | { 69 | fast_cin(); 70 | ll t; 71 | cin >> t; 72 | for(int it=1;it<=t;it++) { 73 | //cout << "Case #" << it+1 << ": "; 74 | solve(); 75 | } 76 | return 0; 77 | } -------------------------------------------------------------------------------- /PIYUSH BALI/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/PIYUSH BALI/book.pdf -------------------------------------------------------------------------------- /PIYUSH BALI/code.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x;while(y>0){if(y&1)res=(1ll*res*x); 26 | y=y>>1;x=(1ll*x*x);} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>m; 49 | ll k; 50 | cin>>k; 51 | string q; 52 | cin>>q; 53 | 54 | forci(0,m) 55 | { 56 | string s = ""; 57 | forcj(0,m) 58 | { 59 | if(i==j) 60 | { 61 | continue; 62 | } 63 | s = s+ q[i]; 64 | } 65 | ans.pb(stoi(s)%k); 66 | } 67 | sort(ans.begin(),ans.end()); 68 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>n; 36 | vi arr; 37 | forci(0,n) 38 | { 39 | cin>>x; 40 | arr.push_back(x); 41 | } 42 | sort(arr.begin(),arr.end()); 43 | int ans=0; 44 | forci(0,n-1) 45 | { 46 | if(arr[i+1]!=arr[i]) 47 | ans++; 48 | 49 | } 50 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 22 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 23 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 24 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 25 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;iprime; 28 | 29 | void primerange(int l,int h) 30 | { 31 | int f; 32 | while (l < h) 33 | { 34 | f = 0; 35 | 36 | for(int i=2; i <= l/2;++i) 37 | { 38 | if(l % i == 0) 39 | { 40 | f = 1; 41 | break; 42 | } 43 | } 44 | 45 | if (f == 0) 46 | prime.push_back(l); 47 | 48 | ++l; 49 | } 50 | } 51 | signed main() 52 | { 53 | primerange(2,500); 54 | 55 | ll ans=0; 56 | ll a,b; 57 | int x,flag; 58 | cin>>a>>b; 59 | forci(2,a/b) 60 | { 61 | if(!(binary_search(prime.begin(),prime.end(),i))) 62 | {continue;} 63 | flag=1; 64 | x=i; 65 | while(x<=a) 66 | { 67 | if(!(binary_search(prime.begin(),prime.end(),x))) 68 | flag=0; 69 | x=x+a/b; 70 | } 71 | if(flag) 72 | ans++; 73 | 74 | } 75 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 22 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 23 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 24 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 25 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i arr; 28 | signed main() 29 | { 30 | 31 | ll a,b; 32 | cin>>a>>b; 33 | ll ans=0; 34 | forci(0,a) 35 | { 36 | ll x; 37 | cin>>x; 38 | arr.push_back(x); 39 | } 40 | sort(arr.begin(), arr.end(),greater()); 41 | 42 | make_heap(arr.begin(),arr.end()); 43 | forci(0,b) 44 | { 45 | ll v=arr.front(); 46 | pop_heap(arr.begin(), arr.end()); 47 | arr.pop_back(); 48 | arr.push_back(v/2); 49 | push_heap(arr.begin(), arr.end()); 50 | } 51 | forci(0,arr.size()) 52 | { 53 | ans=ans+arr[i]; 54 | } 55 | cout< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | typedef long long ll; 26 | typedef long double ld; 27 | typedef pair p32; 28 | typedef pair p64; 29 | typedef pair pdd; 30 | typedef vector v64; 31 | typedef vector v32; 32 | typedef vector > vv32; 33 | typedef vector > vv64; 34 | typedef vector > vvp64; 35 | typedef vector vp64; 36 | typedef vector vp32; 37 | ll MOD = 998244353; 38 | double eps = 1e-12; 39 | #define forn(i,e) for(ll i = 0; i < e; i++) 40 | #define forsn(i,s,e) for(ll i = s; i < e; i++) 41 | #define rforn(i,s) for(ll i = s; i >= 0; i--) 42 | #define rforsn(i,s,e) for(ll i = s; i >= e; i--) 43 | #define ln "\n" 44 | #define dbg(x) cout<<#x<<" = "<>n; 77 | forn(i,n+2) 78 | { 79 | ans*=posstrain[i]; 80 | } 81 | cout<> t; 89 | calcu(); 90 | for(int it=1;it<=t;it++) { 91 | solve(); 92 | } 93 | return 0; 94 | } -------------------------------------------------------------------------------- /PIYUSH BALI/codevita3.cpp: -------------------------------------------------------------------------------- 1 | /* Name:- Piyush Nandkishor Bali 2 | Class:- TY D Computer 3 | */ 4 | #include 5 | 6 | using namespace std; 7 | #define ll long long 8 | #define ld long double 9 | #define pb push_back 10 | #define vi vector 11 | #define scf scanf 12 | #define pf printf 13 | #define MAX 1e9 14 | #define MIN -1e9 15 | #define mod 1000000007 16 | #define mod9 1000000009 17 | #define pi 3.1415926535 18 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 19 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 20 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 21 | #define test ll t;cin>>t;while(t--) 22 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 23 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 24 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 25 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 26 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i0;i--) 31 | { 32 | if(s1[i]==s2[s2.size()-1]) 33 | { 34 | 35 | 36 | } 37 | 38 | 39 | } 40 | signed main() 41 | { 42 | string p; 43 | string f; 44 | getline(cin, p); 45 | getline(cin,f); 46 | forci(0,p.length()) 47 | { 48 | if(p[i]==' ') 49 | continue; 50 | size_t found = f.find(p[i]); 51 | if( found!=string::npos) 52 | { 53 | 54 | }else 55 | { 56 | 57 | } 58 | 59 | } 60 | 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /PIYUSH BALI/december2.cpp: -------------------------------------------------------------------------------- 1 | #pragma GCC optimize("Ofast") 2 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") 3 | #pragma GCC optimize("unroll-loops") 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | using namespace std; 24 | 25 | typedef long long ll; 26 | typedef long double ld; 27 | typedef pair p32; 28 | typedef pair p64; 29 | typedef pair pdd; 30 | typedef vector v64; 31 | typedef vector v32; 32 | typedef vector > vv32; 33 | typedef vector > vv64; 34 | typedef vector > vvp64; 35 | typedef vector vp64; 36 | typedef vector vp32; 37 | ll MOD = 998244353; 38 | double eps = 1e-12; 39 | #define forn(i,e) for(ll i = 0; i < e; i++) 40 | #define forsn(i,s,e) for(ll i = s; i < e; i++) 41 | #define rforn(i,s) for(ll i = s; i >= 0; i--) 42 | #define rforsn(i,s,e) for(ll i = s; i >= e; i--) 43 | #define ln "\n" 44 | #define dbg(x) cout<<#x<<" = "<> t; 62 | for(int it=1;it<=t;it++) { 63 | cout << "Case #" << it+1 << ": "; 64 | solve(); 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /PIYUSH BALI/edges.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i edges[100001]; 31 | vectorvisited; 32 | int ans; 33 | set > s1; 34 | set > s2; 35 | void bfs(int u,int v) 36 | { 37 | visited[u]=true; 38 | if(u==v) 39 | { ans=s1.size(); 40 | return; 41 | } 42 | forci(0,edges[u].size()) 43 | { 44 | if(!(visited[edges[u][i]])) 45 | { 46 | bfs(edges[u][i],v,a); 47 | s1.erase(edges[u][i]) 48 | } 49 | } 50 | 51 | } 52 | signed main() 53 | { 54 | int n,m,a,b; 55 | cin>>n>>m>>a>>b; 56 | int u,v; 57 | visited.resize(n+1,false); 58 | forci(0,m) 59 | { 60 | cin>>u>>v; 61 | edges[u].pb(v); 62 | edges[v].pb(u); 63 | } 64 | bfs(a,b); 65 | 66 | cout< 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;ib) 32 | { 33 | return true; 34 | } 35 | return false; 36 | } 37 | int par[1000006]; 38 | int find(int i) 39 | { 40 | if (parent[i] == i) 41 | return i; 42 | return find(parent[i]); 43 | } 44 | void Union(int x, int y) 45 | { 46 | int xset = find(parent, x); 47 | int yset = find(parent, y); 48 | parent[xset] = yset; 49 | } 50 | signed main() 51 | { 52 | test 53 | { 54 | ll x; 55 | cin>>x; 56 | vi ans; 57 | ll q; 58 | forci(0,x){ 59 | cin>>q; 60 | ans.pb(q); 61 | } 62 | 63 | } 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | } 106 | 107 | //https://www.youtube.com/watch?v=yl2GaxVGUaI -------------------------------------------------------------------------------- /PIYUSH BALI/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 5 12 3 | 52436 -------------------------------------------------------------------------------- /PIYUSH BALI/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/PIYUSH BALI/output.txt -------------------------------------------------------------------------------- /PIYUSH BALI/q11272020.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/PIYUSH BALI/q11272020.cpp -------------------------------------------------------------------------------- /PIYUSH BALI/savegf.cpp: -------------------------------------------------------------------------------- 1 | // God is Great 2 | /* Piyush Bali:- WHY SOMETHING IS IMPOSSIBLE? 3 | */ 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | #define ld long double 8 | #define pb push_back 9 | #define vi vector 10 | #define scf scanf 11 | #define pf printf 12 | #define MAX 1e9 13 | #define MIN -1e9 14 | #define mod 1000000007 15 | #define mod9 1000000009 16 | #define pi 3.1415926535 17 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 18 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 19 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 20 | #define test ll t;cin>>t;while(t--) 21 | //https://youtu.be/miZlLXzVL8I 22 | 23 | ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);} 24 | ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);} 25 | ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod; 26 | y=y>>1;x=(1ll*x*x)%mod;} return res;} 27 | ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i>n>>m>>a>>b; 38 | ll dp[n+1][n+1]; 39 | memset(dp,0,sizeof(dp)); 40 | forci(0,m) 41 | { 42 | cin>>l1>>l2>>ene; 43 | dp[l1][l2]=ene; 44 | dp[l2][l1]=ene; 45 | } 46 | vector> sl; 47 | forci(0,n) 48 | { 49 | cin>>sta>>slp; 50 | sl.push_back(make_pair(sta,slp)); 51 | 52 | } 53 | 54 | ll flag; 55 | while(a!=b) 56 | { 57 | flag=1; 58 | for(ll i=b;i>a;i--) 59 | { 60 | if(stamina>=dp[a][i] && dp[a][i]!=0) 61 | { 62 | stamina=stamina-dp[a][i]; 63 | a=i; 64 | flag=0; 65 | break; 66 | } 67 | 68 | } 69 | if(flag) 70 | { 71 | sleep=sleep+sl[a-1].second; 72 | stamina=sl[a-1].first; 73 | } 74 | 75 | } 76 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define forci(p,n) for(ll i=p;i<(ll)n;i++) 5 | #define forcj(p,n) for(ll j=p;j<(ll)n;j++) 6 | #define v vector 7 | #define int long long 8 | #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); 9 | #define test ll t;cin>>t;while(t--) 10 | 11 | vector arr; 12 | void printPath(ll q,vector stack) 13 | { 14 | int i; 15 | int ans=0; 16 | for (i = 0; i < (ll)stack.size(); i++) 17 | { 18 | ans+=arr[stack[i]]; 19 | } 20 | cout< tree[], 52 | ll n, 53 | vector stack,ll q) 54 | { 55 | // visited array 56 | bool vis[n + 1]; 57 | 58 | memset(vis, false, sizeof(vis)); 59 | 60 | // DFS function call 61 | dfs(tree, vis, x, y, stack,q); 62 | } 63 | 64 | signed main() 65 | { 66 | ll n; 67 | cin>>n; 68 | n=n+1; 69 | arr.resize(n,0); 70 | 71 | forci(1,n) 72 | { 73 | cin>>arr[i]; 74 | } 75 | 76 | vector tree[n],stack; 77 | forci(1,n-1) 78 | { 79 | ll a,b; 80 | cin>>a>>b; 81 | tree[a].push_back(b); 82 | tree[b].push_back(a); 83 | } 84 | ll h; 85 | cin>>h; 86 | forci(0,h) 87 | { int q,x,y; 88 | cin>>q>>x>>y; 89 | DFSCall(x,y,tree,n,stack,q); 90 | stack.clear(); 91 | cout< 10 | using namespace std; 11 | 12 | int main() { 13 | int x[8],y[8],count=0; 14 | bool t = false,r = false; 15 | for (int i=0; i<8; i++){ 16 | cin >> x[i]; 17 | cin >> y[i]; 18 | } 19 | for (int i=0; i<8; i++){ 20 | for (int j=i+1; j<8; j++){ 21 | if(x[j]==x[i]||y[j]==y[i]){ 22 | cout << "YES"; 23 | t = true; 24 | break; 25 | } 26 | } 27 | if(t==true) 28 | break; 29 | } 30 | for (int z=0; z<8; z++){ 31 | for (int w=z+1; w<8; w++){ 32 | if(x[z]+1==x[w] && y[z]+1==y[w] || x[z]+2==x[w] && y[z]+2==y[w] || x[z]-1==x[w] && y[z]-1==y[w] || x[z]-2==x[w] && y[z]-2==y[w]){ 33 | cout << "YES"; 34 | r = true; 35 | break; 36 | } 37 | } 38 | if(r==true) 39 | break; 40 | } 41 | if(r != true && t != true){ 42 | cout << "NO"; 43 | } 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Pranav/cpp/OrganizingContainersofBalls.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | long long int i,j,sumc,n,q; 7 | vector > m; 8 | vector c; 9 | vector t; 10 | cin >> q; 11 | while(q--){ 12 | cin >> n; 13 | for(i=0;i> m[i][j]; 16 | } 17 | } 18 | for (i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | map contact; 9 | string name; 10 | int num, t; 11 | cin >> t; 12 | while (t--) 13 | { 14 | cin >> name; 15 | cin >> num; 16 | contact.insert(pair(name, num)); 17 | } 18 | 19 | while (cin >> name) 20 | { 21 | if (contact[name] == 0) 22 | cout << "Not found" << endl; 23 | else 24 | cout << name << "=" << contact[name] << endl; 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Pranav/cpp/PrintingPatterns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int mod(int x){ // returns the modulus of a number 6 | if(x<0){ 7 | int r = sqrt(x*x); 8 | return r; 9 | } 10 | else 11 | return x; 12 | } 13 | 14 | int max(int x,int y){ // returns the greatest number amoung two 15 | if(x>=y) 16 | return x; 17 | else 18 | return y; 19 | } 20 | 21 | int main() { 22 | int r,c,x,y; 23 | cin >> r >> c >> x >> y; 24 | for (int i=0; i 7 | using namespace std; 8 | 9 | int main() { 10 | string time; 11 | cin >> time; 12 | int h1 = (int)time[0] -'0'; 13 | int h2 = (int)time[1] -'0'; 14 | int h = (h1*10)+(h2%10); 15 | cout << h<< endl; 16 | if(time[8]=='A'){ 17 | if(h==12){ 18 | cout << "00"; 19 | for (int i=2; i<8; i++){ 20 | cout << time[i]; 21 | } 22 | } 23 | else{ 24 | for (int i=0; i<8; i++){ 25 | cout << time[i]; 26 | } 27 | } 28 | } 29 | if(time[8] == 'P'){ 30 | if(h == 12){ 31 | cout << "12"; 32 | for (int i=2; i<8; i++){ 33 | cout << time[i]; 34 | } 35 | } 36 | else{ 37 | cout << h + 12; 38 | for (int i=2; i<8; i++){ 39 | cout << time[i]; 40 | } 41 | } 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Pranav/cpp/Typesofbeakers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define test \ 4 | ll t; \ 5 | cin >> t; \ 6 | while (t--) 7 | using namespace std; 8 | 9 | int main(){ 10 | ll int n, k, days, b1=0, b2 ; 11 | test{ 12 | bool flag = true; 13 | days = 1; b2 = 0; 14 | cin >> n >> k; 15 | while ((b1 + b2) < k && flag){ 16 | for (int i=0; i=k){ 21 | flag = false; 22 | break; 23 | } 24 | if (b2*2 >= k){ 25 | b1 += n; 26 | n = n - b2; 27 | b2 =0; 28 | break; 29 | } 30 | } 31 | } 32 | cout << days << endl; 33 | } 34 | } -------------------------------------------------------------------------------- /Pranav/javascript/Kalidoscope/ReadMe.txt: -------------------------------------------------------------------------------- 1 | This might not work in Google Chrome. 2 | -------------------------------------------------------------------------------- /Pranav/javascript/Kalidoscope/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | p5.js example 8 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Pranav/javascript/Kalidoscope/sketch.js: -------------------------------------------------------------------------------- 1 | let symmetry = 10; 2 | 3 | let angle = 360 / symmetry; 4 | let saveButton, clearButton, mouseButton, keyboardButton; 5 | let slider; 6 | let r, g, b; 7 | 8 | function setup() { 9 | createCanvas(900, 890); 10 | angleMode(DEGREES); 11 | background(0); 12 | 13 | line(width / 2, width / 2); 14 | 15 | saveButton = createButton('save'); 16 | saveButton.mousePressed(saveFile); 17 | 18 | clearButton = createButton('clear'); 19 | clearButton.mousePressed(clearScreen); 20 | 21 | } 22 | 23 | function saveFile() { 24 | save('design.jpg'); 25 | } 26 | 27 | function clearScreen() { 28 | background(0); 29 | } 30 | 31 | 32 | function mousePressed(){ 33 | r = random(0,255); 34 | g = random(0,255); 35 | b = random(0,255); 36 | } 37 | function draw() { 38 | translate(width / 2, height / 2); 39 | //rotate () 40 | if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) { 41 | let mx = mouseX - width / 2; 42 | let my = mouseY - height / 2; 43 | let pmx = pmouseX - width / 2; 44 | let pmy = pmouseY - height / 2; 45 | 46 | 47 | 48 | if (mouseIsPressed) { 49 | for (let i = 0; i < symmetry; i++) { 50 | rotate(angle); 51 | let sw = 4; 52 | stroke(r,g,b); 53 | //stroke(random(0, 255), random(0, 255), random(0, 255)) 54 | strokeWeight(sw); 55 | line(mx, my, pmx, pmy); 56 | push(); 57 | scale(1, -1); 58 | line(mx, my, pmx, pmy); 59 | pop(); 60 | 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # competitive-coding 2 | 3 | This repo is competitive coding programmers of VIIT. 4 | -------------------------------------------------------------------------------- /Rohit/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Rohit/book.pdf -------------------------------------------------------------------------------- /Sahil/Codeforces Div. 2, A/10_Beautiful Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int arr[5][5],i,j,k,l; 11 | 12 | for(i=0;i<5;i++) 13 | { 14 | for(j=0;j<5;j++) 15 | { 16 | cin>>arr[i][j]; 17 | 18 | if(arr[i][j]==1) 19 | { 20 | k=i; 21 | l=j; 22 | } 23 | } 24 | } 25 | 26 | if((2-k)>=0) 27 | k=2-k; 28 | else 29 | k=k-2; 30 | 31 | if((2-l)>=0) 32 | l=2-l; 33 | else 34 | l=l-2; 35 | 36 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n,i; 11 | cin>>n; 12 | 13 | int arr[n]; 14 | 15 | for(i=0;i>arr[i]; 17 | 18 | int min=101,max=-1; 19 | int minIdx,maxIdx; 20 | 21 | for(i=0;i=0;i--) 34 | { 35 | if(arr[i]>=max) 36 | { 37 | max=arr[i]; 38 | maxIdx=i; 39 | } 40 | } 41 | 42 | 43 | if(maxIdx==minIdx) 44 | cout<<0; 45 | else if(maxIdx>minIdx) 46 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int n; 11 | cin>>n; 12 | 13 | if(n%2==1 || n==0 || n==2) 14 | cout<<"NO"; 15 | else 16 | cout<<"YES"; 17 | 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Sahil/Codeforces Div. 2, A/2_Way Too Long Words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void printAbr(string s) 8 | { 9 | if(s.length()<=10) 10 | cout<>n; 25 | 26 | while(n>0) 27 | { 28 | string s; 29 | 30 | cin>>s; 31 | 32 | printAbr(s); 33 | 34 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | string s; 11 | 12 | cin>>s; 13 | 14 | for(int i=0;i=65 && s[i]<=90) 19 | { 20 | s[i]+=32; 21 | cout<<"."< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | string s,a; 12 | cin>>s>>a; 13 | 14 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int i; 11 | string s; 12 | cin>>s; 13 | string a; 14 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n,i,j; 11 | cin>>n; 12 | 13 | int arr[n]; 14 | 15 | for(i=0;i>arr[i]; 17 | 18 | sort(arr,arr+n,greater()); 19 | 20 | 21 | int mySum=0,hisSum=0,count=0; 22 | for(i=0;ihisSum) 33 | break; 34 | } 35 | 36 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | bool check(string s) 9 | { 10 | int n=s.size(),m; 11 | 12 | for(int i=0;i>s; 54 | 55 | int ans=check(s); 56 | 57 | if(ans==1) 58 | cout<<"YES"; 59 | else 60 | cout<<"NO"; 61 | 62 | 63 | 64 | 65 | 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /Sahil/Codeforces Div. 2, A/8_Lucky Division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int arr[]={4,7,47,44,74,77,444,447,474,477,744,774,747,777}; 11 | int n=sizeof(arr)/sizeof(arr[0]); 12 | 13 | int num,i=0; 14 | cin>>num; 15 | 16 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n,i; 11 | cin>>n; 12 | 13 | int arr[n]; 14 | int temp[n]; 15 | 16 | for(i=0;i>arr[i]; 18 | 19 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | 11 | 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Sanket/TestFile.txt: -------------------------------------------------------------------------------- 1 | Test Document -------------------------------------------------------------------------------- /ShantanuK/BattleOfVars-1.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Solution { 5 | /*where is the attack?*/ 6 | 7 | public static void main(String[] args) { 8 | Scanner sc = new Scanner(System.in); 9 | int n = sc.nextInt(); 10 | String code; 11 | int d = 0; 12 | int o = 0; 13 | int p = 0; 14 | int e = 0; 15 | char c[] = new char[n]; 16 | code = sc.next(); 17 | for(int i = 0;imax){ 20 | max = s[i]; 21 | maxBreak++; 22 | } 23 | } 24 | System.out.println(maxBreak+" "+minBreak); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /ShantanuK/Company.java: -------------------------------------------------------------------------------- 1 | public class Company{ 2 | 3 | private String name; 4 | private int pvmt; 5 | 6 | public void setName(String nameIn){ 7 | name = nameIn; 8 | } 9 | 10 | public void getName(){ 11 | return name; 12 | } 13 | public void setPvmt(int pvmtIn){ 14 | pvmt = pvmtIn; 15 | } 16 | public void getPvmt(){ 17 | return pvmt; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /ShantanuK/Employee.java: -------------------------------------------------------------------------------- 1 | public class employee{ 2 | 3 | Company myCompany = new Company(); 4 | 5 | myCompany.setName("Shantanu"); 6 | myCompany.setPvmt(150); 7 | 8 | 9 | System.out.println(myCompany.getName); 10 | System.out.println(myCompany.getPvmt); 11 | 12 | -------------------------------------------------------------------------------- /ShantanuK/MarsExploration.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution{ 3 | public static void main(String[] args) { 4 | Scanner sc = new Scanner(System.in); 5 | String s = sc.next(); 6 | int x = 0; 7 | char c[] = new char [s.length()]; 8 | for(int i = 0;i>>>>>> 93f77f9812392c44048892c50466e90708aee9cd 52 | } -------------------------------------------------------------------------------- /ShantanuK/catmouse.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class catmouse { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | 9 | int t = sc.nextInt(); 10 | for(int i = 0;i disB){ 22 | System.out.println("Cat B"); 23 | } 24 | else if(disA == disB){ 25 | System.out.println("Mouse C"); 26 | } 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /ShantanuK/chocolateFeast.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution { 3 | public static void main(String[] args) { 4 | Scanner sc = new Scanner(System.in); 5 | int t= sc.nextInt(); 6 | for(int i = 0;i=m){ 13 | x-=m; 14 | j++; 15 | x++; 16 | } 17 | System.out.println(j); 18 | 19 | 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ShantanuK/countingValley.java: -------------------------------------------------------------------------------- 1 | package learn; 2 | import java.util.*; 3 | 4 | public class countingValley{ 5 | 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int n = sc.nextInt(); 9 | String s= sc.next(); 10 | int x = 0; 11 | char c[] = new char[n]; 12 | int v=0; 13 | for(int i = 0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int area,p=2000000000; 7 | cin>>area; 8 | for(int i=1;i<=sqrt(area);++i){ 9 | if(area%i==0){ 10 | if(2*(i+area/i) 3 | using namespace std; 4 | 5 | #define FILE freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdin); 6 | #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 7 | #define pb push_back 8 | #define ll long long 9 | #define vi vector 10 | #define vii vector 11 | #define pi pair 12 | #define MP make_pair 13 | #define B begin() 14 | #define E end() 15 | #define S size() 16 | #define L length() 17 | #define pf printf 18 | #define sf scanf 19 | 20 | int main(){ 21 | FAST 22 | int cases,a,b; 23 | cin>>cases; 24 | while(cases){ 25 | cin>>a>>b; 26 | if((a+b)/3 2 | using namespace std; 3 | 4 | int main(){ 5 | vector v={1,1,1,1,3,3,5}; //5 6 | int start=0,end=v.size()-1,mid; 7 | while(start<=end){ 8 | mid=(start+end)/2; 9 | if(v[mid]==3){ 10 | if(v[mid+1]!=3){ 11 | cout<<"\nfound at index "< 2 | using namespace std; 3 | 4 | int main(){ 5 | vector v={1,1,1,1,2,2,2,3,3,3,4}; //7 6 | int start=0,end=v.size()-1,mid; 7 | while(start<=end){ 8 | mid=(start+end)/2; 9 | if(v[mid]==3){ 10 | if(v[mid-1]!=3){ 11 | cout<<"\nfound at index "< 2 | int fib(int n) 3 | { 4 | int f[n+2]; 5 | int i; 6 | f[0] = 0; 7 | f[1] = 1; 8 | for (i = 2; i <= n; i++) 9 | { 10 | f[i] = f[i-1] + f[i-2]; 11 | } 12 | return f[n]; 13 | } 14 | int main () 15 | { 16 | int n = 9; 17 | printf("%d", fib(n)); 18 | getchar(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Siddhesh/Concepts/Kadane'sAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | // https://hackernoon.com/kadanes-algorithm-explained-50316f4fd8a6 2 | 3 | // DP Approach 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | vector v={1,2,-1,4,5,-1}, dp(6,-1); 9 | dp[0]=v[0]; 10 | for(int i=1;i 21 | using namespace std; 22 | 23 | int main(){ 24 | vector v={1,2,-1,4,5,-1}; 25 | int curr_sum=0,best_sum=0; 26 | for(int i=0;i 42 | using namespace std; 43 | 44 | int main(){ 45 | vector v={1,2,-1,4,5,-1}; 46 | int curr_sum=0,best_sum=0,s_i=0,e_i=0; 47 | for(int i=0;inext; 17 | } 18 | } 19 | 20 | 21 | int main() 22 | { 23 | Node* head = NULL; 24 | Node* second = NULL; 25 | Node* third = NULL; 26 | 27 | // allocate 3 nodes in the heap 28 | head = new Node(); 29 | second = new Node(); 30 | third = new Node(); 31 | 32 | head->data = 1; // assign data in first node 33 | head->next = second; // Link first node with second 34 | 35 | second->data = 2; // assign data to second node 36 | second->next = third; 37 | 38 | third->data = 3; // assign data to third node 39 | third->next = NULL; 40 | 41 | printList(head); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Siddhesh/Concepts/TraverseLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | // This function prints contents of linked list 11 | // starting from the given node 12 | void printList(Node* n) 13 | { 14 | while (n != NULL) { 15 | cout << n->data << " "; 16 | n = n->next; 17 | } 18 | } 19 | 20 | 21 | int main() 22 | { 23 | Node* head = NULL; 24 | Node* second = NULL; 25 | Node* third = NULL; 26 | 27 | // allocate 3 nodes in the heap 28 | head = new Node(); 29 | second = new Node(); 30 | third = new Node(); 31 | 32 | head->data = 1; // assign data in first node 33 | head->next = second; // Link first node with second 34 | 35 | second->data = 2; // assign data to second node 36 | second->next = third; 37 | 38 | third->data = 3; // assign data to third node 39 | third->next = NULL; 40 | 41 | printList(head); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Siddhesh/Hackerearth/Dislikes_And_Party(Handshakes).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | /* test cases failed while I tried to use int, so I have used long long */ 6 | long long i,j,x,cases,hs; 7 | 8 | /* used a set of pairs to avoid duplication of pairs */ 9 | set> s; 10 | long long v[10][10]; 11 | 12 | //input 13 | cin>>cases; 14 | for (i=0;i<10;++i){ 15 | for (j=0;j<10;++j){ 16 | cin>>x; 17 | v[i][j]=x; 18 | } 19 | } 20 | 21 | //solution 22 | /* making all the possible pairs (with 0th index) in each row of 10x10 matrix and inserting them in set */ 23 | for (i=0;i<10;++i){ 24 | for (j=1;j<10;++j){ 25 | if (v[i][0] 2 | using namespace std; 3 | 4 | int main(){ 5 | int i,j,r,c,cr,cc; 6 | cin>>r>>c>>cr>>cc; 7 | for (i=0;i 11 | using namespace std; 12 | 13 | int main() { 14 | int x,carry=0; 15 | cin>>x; 16 | string s=""; 17 | while(x!=0){ 18 | carry=x%2; 19 | s=to_string(carry)+s; 20 | x/=2; 21 | } 22 | if(s.length()%8!=0){ 23 | while(s.length()%8!=0){ 24 | s='0'+s; 25 | } 26 | } 27 | reverse(s.begin(),s.end()); 28 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int size,i,j; 8 | char x; 9 | cin>>size; 10 | char v[size][size]; 11 | 12 | //input 13 | for (i=0;i>x; 16 | v[i][j]=x; 17 | } 18 | } 19 | 20 | for (i=1;iv[i][j-1] && 23 | v[i][j]>v[i-1][j] && 24 | v[i][j]>v[i][j+1] && 25 | v[i][j]>v[i+1][j] 26 | ){ 27 | v[i][j]='X'; 28 | } 29 | } 30 | } 31 | 32 | for (i=0;i 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | // l,w,h for block 11 | // a,ht for prism where a is length and h is height 12 | int l,w,h,a,ht; 13 | cin>>l>>w>>h>>a>>ht; 14 | 15 | // no. of blocks in a row (row means just the base perimeter i.e. 4a) (just consider the base not height) 16 | // divided by the length gives no. of blocks 17 | int no_block = (4 * a) / l; 18 | 19 | // row_no is the no. of rows 20 | int row_no; 21 | 22 | // max(h,w) is considered as the height of block to minimize the no. of rows 23 | if (h < w){ 24 | row_no = ht/w; 25 | } 26 | else{ 27 | row_no = ht/h; 28 | } 29 | cout < 19 | using namespace std; 20 | 21 | int main() 22 | { 23 | int best=0,o=0,z=0,tot=0; 24 | string s; 25 | cin>>s; 26 | for(int i=0;i Solved by using a simple if condition inside the loop which checks if the index is valid. 7 | -> Initialized sum=carry, then added only if the indices are valid. 8 | -> Addition is done by simply subtracting '0' from the respective character which gives int sum. 9 | -> sum/2 gives carry and sum%2 gives current index addition. 10 | */ 11 | 12 | class Solution { 13 | public: 14 | string addBinary(string a, string b) { 15 | string ans=""; 16 | int carry=0,sum,size_a=a.length()-1,size_b=b.length()-1; 17 | while(size_a>=0 || size_b>=0){ 18 | sum=carry; 19 | if(size_a>=0){ 20 | sum+=a[size_a]-'0'; 21 | --size_a; 22 | } 23 | if(size_b>=0){ 24 | sum+=b[size_b]-'0'; 25 | --size_b; 26 | } 27 | ans=to_string(sum%2)+ans; 28 | carry=sum/2; 29 | } 30 | //if carry still exists after all addition is done 31 | if(carry==1){ 32 | ans='1'+ans; 33 | } 34 | return ans; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/AddTwoNumbersLinkedList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 14 | if(l1==nullptr) return l2; 15 | if(l2==nullptr) return l1; 16 | ListNode *ans=nullptr; 17 | ListNode *head=nullptr; 18 | int sum; 19 | bool carry=0; 20 | while (l1!=nullptr || l2!=nullptr){ 21 | sum=0; 22 | if(l1==nullptr){ 23 | sum+=l2->val+carry; 24 | } 25 | else if (l2==nullptr){ 26 | sum+=l1->val+carry; 27 | } 28 | else{ 29 | sum+=l1->val+l2->val+carry; 30 | } 31 | carry=sum/10; 32 | sum=sum%10; 33 | if(ans==nullptr){ 34 | ans = new ListNode(sum); 35 | head= ans; 36 | } 37 | else{ 38 | head->next= new ListNode(sum); 39 | head=head->next; 40 | } 41 | if(l1!=nullptr){ 42 | l1=l1->next; 43 | } 44 | if(l2!=nullptr){ 45 | l2=l2->next; 46 | } 47 | } 48 | if(carry){ 49 | head->next = new ListNode(carry); 50 | head=head->next; 51 | } 52 | return ans; 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/BackspaceStringCompare_0ms.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool backspaceCompare(string S, string T) { 4 | string ans1="",ans2=""; 5 | stringstream obj1(S); 6 | stringstream obj2(T); 7 | char letter; 8 | while (obj1>>letter){ 9 | if (letter=='#'){ 10 | if (ans1==""){ 11 | continue; 12 | } 13 | else{ 14 | ans1.pop_back(); 15 | } 16 | } 17 | else{ 18 | ans1.push_back(letter); 19 | } 20 | } 21 | while (obj2>>letter){ 22 | if (letter=='#'){ 23 | if (ans2==""){ 24 | continue; 25 | } 26 | else { 27 | ans2.pop_back(); 28 | } 29 | } 30 | else{ 31 | ans2.push_back(letter); 32 | } 33 | } 34 | if (ans1==ans2){ 35 | return true; 36 | } 37 | return false; 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/BinaryTreePreorderTraversal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | 13 | // Iterative solution using stack 14 | class Solution { 15 | public: 16 | vector preorderTraversal(TreeNode* root) { 17 | if(!root) return {}; 18 | vector v; 19 | v.push_back(root->val); 20 | stack s; 21 | s.push(root); 22 | while (!s.empty()){ 23 | TreeNode *main=s.top(); 24 | if(main->left){ 25 | s.push(main->left); 26 | v.push_back(main->left->val); 27 | main->left=nullptr; 28 | } 29 | else if(main->right){ 30 | s.push(main->right); 31 | v.push_back(main->right->val); 32 | main->right=nullptr; 33 | } 34 | else{ 35 | s.pop(); 36 | } 37 | } 38 | return v; 39 | } 40 | }; 41 | 42 | 43 | //RECURSION 44 | class Solution { 45 | public: 46 | void recur(vector& v,TreeNode *root){ 47 | if(root->left){ 48 | v.push_back(root->left->val); 49 | recur(v,root->left); 50 | } 51 | if(root->right){ 52 | v.push_back(root->right->val); 53 | recur(v,root->right); 54 | } 55 | return; 56 | } 57 | 58 | vector preorderTraversal(TreeNode* root) { 59 | if(!root) return {}; 60 | vector v; 61 | v.push_back(root->val); 62 | recur(v,root); 63 | return v; 64 | } 65 | }; 66 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ClimbStairs(Using DP).cpp: -------------------------------------------------------------------------------- 1 | // Nothing but just Fibonacci sequence. 2 | // Using DP is the key here.. else time limit exceeds. 3 | 4 | //ITERATION 5 | class Solution { 6 | public: 7 | int climbStairs(int n) { 8 | if(n==0 || n==1) return 1; 9 | vector v(n+1); 10 | v[0]=1; 11 | v[1]=1; 12 | for(int i=2;i<=n;++i){ 13 | v[i]=v[i-1]+v[i-2]; 14 | } 15 | return v[n]; 16 | } 17 | }; 18 | 19 | //RECURSION 20 | class Solution { 21 | public: 22 | static int ans(vector& v, int n){ 23 | if(n==0) return 1; 24 | if(n==1) return 1; 25 | if(v[n]!=-1){ 26 | return v[n]; 27 | } 28 | v[n]=ans(v,n-1)+ans(v,n-2); 29 | return v[n]; 30 | } 31 | int climbStairs(int n) { 32 | vector v(n+1,-1); 33 | if(n>1){ 34 | v[0]=1; 35 | v[1]=1; 36 | } 37 | return ans(v,n); 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/CountAndSay.cpp: -------------------------------------------------------------------------------- 1 | // n=1, "1" default case 2 | // n=2, "11" above string is read as one 1s 3 | // n=3, "21" -||- two 1s 4 | // ... Used recursion as below 5 | 6 | 7 | class Solution { 8 | public: 9 | string countAndSay(int n) { 10 | if(n==1){ 11 | return "1"; 12 | } 13 | int count=0; 14 | string s=countAndSay(n-1),ans=""; 15 | for(int i=0;i v; 4 | int h,t,im,size; 5 | /** Initialize your data structure here. Set the size of the queue to be k. */ 6 | MyCircularQueue(int k) { 7 | v.resize(k,INT_MIN); 8 | h=0,t=0,im=k,size=k; 9 | } 10 | 11 | /** Insert an element into the circular queue. Return true if the operation is successful. */ 12 | bool enQueue(int value) { 13 | if(isFull()) return false; 14 | if(t==0 && v[t]==INT_MIN){ 15 | v[t]=value; 16 | --im; 17 | } 18 | else{ 19 | if(t==size-1){ 20 | t=0; 21 | v[t]=value; 22 | --im; 23 | } 24 | else{ 25 | v[++t]=value; 26 | --im; 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | /** Delete an element from the circular queue. Return true if the operation is successful. */ 33 | bool deQueue() { 34 | if(isEmpty()) return false; 35 | if(h==size-1){ 36 | v[h]=INT_MIN; 37 | ++im; 38 | h=0; 39 | } 40 | else{ 41 | v[h]=INT_MIN; 42 | ++h; 43 | ++im; 44 | if(v[h]==INT_MIN) { 45 | h=0; 46 | t=0; 47 | } 48 | } 49 | return true; 50 | } 51 | 52 | /** Get the front item from the queue. */ 53 | int Front() { 54 | if(isEmpty()) return -1; 55 | return v[h]; 56 | } 57 | 58 | /** Get the last item from the queue. */ 59 | int Rear() { 60 | if(isEmpty()) return -1; 61 | return v[t]; 62 | } 63 | 64 | /** Checks whether the circular queue is empty or not. */ 65 | bool isEmpty() { 66 | if(im==size) return true; 67 | return false; 68 | } 69 | 70 | /** Checks whether the circular queue is full or not. */ 71 | bool isFull() { 72 | if(im==0) return true; 73 | return false; 74 | } 75 | }; 76 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/FindAllNumbersDisappearedInAnArray.cpp: -------------------------------------------------------------------------------- 1 | //Link : https://github.com/siddheshkothadi/competitive-coding/blob/master/Siddhesh/Notes/FindAllNumbersDisappearedInAnArray.jpg 2 | 3 | class Solution { 4 | public: 5 | vector findDisappearedNumbers(vector& nums) { 6 | vector v; 7 | size_t vi; 8 | for(int i=0;i0){ 11 | nums[vi]=-nums[vi];} 12 | } 13 | for(int i=0;i0){ 15 | v.push_back(i+1); 16 | } 17 | } 18 | return v; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/FirstAndLastPositionOfElementInASortedArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | if (binary_search(nums.begin(),nums.end(),target)){ 5 | auto low=lower_bound(nums.begin(),nums.end(),target); 6 | auto upp=upper_bound(nums.begin(),nums.end(),target); 7 | int l=low-nums.begin(),u=upp-nums.begin()-1; 8 | return {l,u}; 9 | } 10 | return {-1,-1}; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/HeightChecker.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int heightChecker(vector& h) { 4 | int count=0; 5 | vector v=h; 6 | sort(h.begin(),h.end()); 7 | if(h==v) return 0; 8 | for(int i=0;isize_h){ 23 | return -1; 24 | } 25 | for(int i=0;i<=size_h-size_n;++i){ 26 | if(haystack.substr(i,size_n)==needle){ 27 | return i; 28 | } 29 | } 30 | return -1; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/IntersectionOfTwoLinkedLists.cpp: -------------------------------------------------------------------------------- 1 | //AWESOME PROBLEM... VERY SIMPLE SOLUTION 2 | /* 3 | 1-2-3-4-5-6-7-8-9-NULL 4 | | 5 | 1-3 6 | EXPECTED ANSWER IS NODE "5"; 7 | 8 | APPROACH:: 9 | 1. HAVE TWO POINTERS POINTING AT THE RESPECTIVE HEADS OF THE TWO LINKED LISTS 10 | 2. MOVE BOTH OF THEM ONE STEP AT A TIME 11 | 3. IF ANY OF THE POINTER REACHES THE END OF THE LIST I.E. NULL... MAKE IT POINT TO THE HEAD OF THE OTHER LIST! 12 | */ 13 | 14 | class Solution { 15 | public: 16 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 17 | ListNode *a=headA,*b=headB; 18 | while(a!=b){ 19 | if(a!=nullptr){ 20 | a=a->next; 21 | } 22 | else{ 23 | a=headB; 24 | } 25 | if(b!=nullptr){ 26 | b=b->next; 27 | } 28 | else{ 29 | b=headA; 30 | } 31 | } 32 | return a; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/LengthOfLastWord.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | bool first_space=true; 5 | int count=0; 6 | for(int i=s.length()-1;i>=0;--i){ 7 | if(s[i]==' '){ 8 | if(first_space){ 9 | continue; 10 | } 11 | else{ 12 | break; 13 | } 14 | } 15 | else{ 16 | first_space=false; 17 | ++count; 18 | } 19 | } 20 | return count; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/LinkedListCycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | 10 | //Two pointers f & s one is slow other is fast 11 | class Solution { 12 | public: 13 | bool hasCycle(ListNode *head) { 14 | if(head==nullptr || head->next==nullptr) return 0; 15 | if(head->next==head) return 1; 16 | ListNode *f=head,*s=head->next; 17 | while (f!=s){ 18 | if(f->next==nullptr || s->next==nullptr || s->next->next==nullptr) return 0; 19 | f=f->next; 20 | s=s->next->next; 21 | } 22 | return 1; 23 | } 24 | }; 25 | 26 | // With head as f and s 27 | class Solution { 28 | public: 29 | bool hasCycle(ListNode *head) { 30 | if(head==nullptr) return 0; 31 | ListNode *f=head,*s=head; 32 | bool first=1; 33 | while(true){ 34 | if(s->next==nullptr || s->next->next==nullptr) return 0; 35 | if(f==s && !first){ 36 | return 1; 37 | } 38 | first=0; 39 | f=f->next; 40 | s=s->next->next; 41 | } 42 | return 0; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/LinkedListCycleII.cpp: -------------------------------------------------------------------------------- 1 | // FLOYD'S ALGORITHM 2 | // LINK:: https://youtu.be/9YTjXqqJEFE 3 | /* 4 | APPROACH:: 5 | FIRST FIND OUT IF THERE IS A CYCLE ... REFER TO LINKED LIST CYCLE SOLUTION 6 | IF THERE IS A CYCLE :: 7 | MOVE FIRST PTR TO START AND KEEP SCND PTR WHERE IT IS 8 | NOW KEEP MOVING FORWARD EACH PTR... 9 | THE POINT WHERE THEY MEET WILL BE THE POINT WHERE THE CYCLE STARTS.. WHICH IS ASKED IN THE SOLUTION 10 | */ 11 | 12 | class Solution { 13 | public: 14 | ListNode *detectCycle(ListNode *head) { 15 | if(head==nullptr) return head; 16 | ListNode *f=head,*s=head; 17 | bool found=0,first=1; 18 | while(true){ 19 | if(s->next==nullptr || s->next->next==nullptr){ 20 | break; 21 | } 22 | if(s==f && !first){ 23 | found=1; 24 | break; 25 | } 26 | first=0; 27 | f=f->next; 28 | s=s->next->next; 29 | } 30 | if(found){ 31 | f=head; 32 | while(f!=s){ 33 | f=f->next; 34 | s=s->next; 35 | } 36 | return f; 37 | } 38 | return nullptr; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/LongestCommonPrefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | string s=""; 5 | if (strs.size()==0) return s; 6 | char c; 7 | int i,j; 8 | for (i=0;i q; 8 | q.push(root); 9 | while(!q.empty()){ 10 | int size=q.size(); 11 | for(int i=0;ileft){ 15 | q.push(root->left); 16 | } 17 | if(root->right){ 18 | q.push(root->right); 19 | } 20 | } 21 | ++count; 22 | } 23 | return count; 24 | } 25 | }; 26 | 27 | //RECURSION 28 | class Solution { 29 | public: 30 | int recur(TreeNode* root,int count){ 31 | if(!root) return count; 32 | int left=recur(root->left,count+1); 33 | int right=recur(root->right,count+1); 34 | return max(left,right); 35 | } 36 | 37 | int maxDepth(TreeNode* root) { 38 | if(!root) return 0; 39 | //passesd 0 instead of 1 (i.e. initial depth) because the recur returns when the root is null 40 | //which is why count is always 1 greater than the actual depth 41 | return recur(root,0); 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/MaximumSubarray(Kadane's Algorithm).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | THIS CODE HANDLES NEGATIVE VALUES BY: 3 | 1. SETTING INITIAL sum AND best VALUES AS FIRST ELEMENT OF NUMS. 4 | 2. LOOPING FROM 2ND ELEMENT INSTEAD OF FIRST. 5 | */ 6 | class Solution { 7 | public: 8 | int maxSubArray(vector& nums) { 9 | int sum=nums[0],best=nums[0]; 10 | for(int i=1;i& nums) { 22 | bool all_neg=true; 23 | int sum=0,best=0,neg_max=INT_MIN; 24 | for(int i=0;ival>l2->val){ 19 | temp = l2; 20 | l2=l2->next; 21 | } 22 | else{ 23 | temp = l1; 24 | l1=l1->next; 25 | } 26 | if(ans==nullptr){ 27 | ans = temp; 28 | ptr = ans; 29 | } 30 | else{ 31 | ans->next=temp; 32 | ans=ans->next; 33 | } 34 | } 35 | if(l1!=nullptr){ 36 | while(l1!=nullptr){ 37 | ans->next= l1; 38 | ans=ans->next; 39 | l1=l1->next; 40 | } 41 | } 42 | else if(l2!=nullptr){ 43 | while(l2!=nullptr){ 44 | ans->next=l2; 45 | ans=ans->next; 46 | l2=l2->next; 47 | } 48 | } 49 | return ptr; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/MergeTwoSortedLists.cpp: -------------------------------------------------------------------------------- 1 | // TRICK : TRAVERSE TWO LISTS BACKWARDS 2 | // Below is the Ideal Approach 3 | /* 4 | m+n-1 5 | | 6 | v1={1,2,3,0,0,0}, v2={2,5,6} 7 | | | 8 | m-1 n-1 9 | 10 | WHICHEVER IS BIGGER AMONG v1[m-1] and v2[n-1] WILL BE ADDED TO THE m+n-1 th INDEX; 11 | */ 12 | // O(n) time, O(1) space 13 | class Solution { 14 | public: 15 | void merge(vector& v1, int m, vector& v2, int n) { 16 | // To avoid use of any negative indices 17 | while(n>0 && m>0){ 18 | if(v1[m-1]>v2[n-1]) { 19 | v1[m+n-1]=v1[m-1]; 20 | --m; 21 | } 22 | else{ 23 | v1[m+n-1]=v2[n-1]; 24 | --n; 25 | } 26 | } 27 | // For remaining +ve indices 28 | if(n!=0){ 29 | while(n!=0){ 30 | v1[m+n-1]=v2[n-1]; 31 | --n; 32 | } 33 | } 34 | else if(m!=0){ 35 | while(m!=0){ 36 | v1[m+n-1]=v1[m-1]; 37 | --m; 38 | } 39 | } 40 | } 41 | }; 42 | 43 | 44 | // OTHER APPROACH - CREATING A COPY OF FIRST VECTOR - O(n) space 45 | class Solution { 46 | public: 47 | void merge(vector& v3, int m, vector& v2, int n) { 48 | vector v1=v3; 49 | int i=0,j=0; 50 | while (j=m){ 52 | v3[i+j]=v2[j]; 53 | ++j; 54 | continue; 55 | } 56 | if(j>=n){ 57 | v3[i+j]=v1[i]; 58 | ++i; 59 | continue; 60 | } 61 | if(v1[i]& nums) { 4 | int index=0,size=nums.size(); 5 | for (int a:nums){ 6 | if (a!=0){ 7 | nums[index]=a; 8 | ++index; 9 | } 10 | } 11 | while(index>& grid, int i, int j){ 14 | 15 | // Edge cases and when element != '1' 16 | if(i<0 || i>=grid.size() || j<0 || j>=grid[0].size() || grid[i][j]!='1') return; 17 | 18 | //for '1' check all the 4 adjacent elements 19 | grid[i][j]='0'; 20 | fun(grid,i+1,j); 21 | fun(grid,i,j+1); 22 | fun(grid,i-1,j); 23 | fun(grid,i,j-1); 24 | } 25 | 26 | int numIslands(vector>& grid) { 27 | int no_islands=0; 28 | for(int i=0;i>& grid) { 49 | if ( grid.size() == 0 || grid[0].size() == 0 ) { 50 | return 0; 51 | } 52 | 53 | int m = grid.size(); 54 | int n = grid[0].size(); 55 | 56 | pair root, current; 57 | queue> the_queue; 58 | 59 | // Find first 1 60 | bool flag = false; 61 | for ( int i = 0; i < m; i++ ) { 62 | for ( int j = 0; j < n; j++ ) { 63 | if ( grid[i][j] == '1' ) { 64 | root = make_pair(i,j); 65 | flag = true; 66 | break; 67 | } 68 | } 69 | if ( flag ) { 70 | break; 71 | } 72 | } 73 | 74 | // If no 1, return count 0 75 | if ( !flag ) { 76 | return 0; 77 | } 78 | 79 | the_queue.push(root); 80 | 81 | while ( !the_queue.empty() ) { 82 | current = the_queue.front(); 83 | int i = current.first, 84 | j = current.second; 85 | 86 | // Push neighbouring 1s to queue and set 87 | // them to 0 to avoid re-addition to queue 88 | if ( i - 1 >= 0 && grid[i - 1][j] == '1' ) { 89 | the_queue.push(make_pair(i - 1, j)); 90 | grid[i - 1][j] = '0'; 91 | } 92 | if ( j + 1 < n && grid[i][j + 1] == '1' ) { 93 | the_queue.push(make_pair(i, j + 1)); 94 | grid[i][j + 1] = '0'; 95 | } 96 | if ( i + 1 < m && grid[i + 1][j] == '1' ) { 97 | the_queue.push(make_pair(i + 1, j)); 98 | grid[i + 1][j] = '0'; 99 | } 100 | if ( j - 1 >= 0 && grid[i][j - 1] == '1' ) { 101 | the_queue.push(make_pair(i, j - 1)); 102 | grid[i][j - 1] = '0'; 103 | } 104 | 105 | // convert handled 1s to 0s 106 | grid[i][j] = '0'; 107 | the_queue.pop(); 108 | } 109 | 110 | return 1 + numIslands(grid); 111 | } 112 | }; 113 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/OddEvenLinkedList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | I/P 1-2-3-4-5-6-7-NULL 3 | O/P 1-3-5-7-2-4-6-NULL 4 | 5 | APPROACH :: Two Pointers ... One pointing at even and other pointing at odd position. 6 | */ 7 | 8 | class Solution { 9 | public: 10 | ListNode* oddEvenList(ListNode* head) { 11 | if(!head || !head->next) return head; 12 | ListNode *odd=head,*even=head->next,*even_head=head->next,*odd_tail=head; 13 | while(odd->next || even->next){ 14 | if(!odd->next || !odd->next->next){ 15 | odd->next=nullptr; 16 | odd_tail=odd; 17 | } 18 | else{ 19 | odd->next=odd->next->next; 20 | odd=odd->next; 21 | odd_tail=odd; 22 | } 23 | if(!even->next || !even->next->next){ 24 | even->next=nullptr; 25 | } 26 | else{ 27 | even->next=even->next->next; 28 | even=even->next; 29 | } 30 | } 31 | odd_tail->next=even_head; 32 | return head; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/OpenTheLock.cpp: -------------------------------------------------------------------------------- 1 | //BFS USING QUEUE 2 | 3 | class Solution { 4 | public: 5 | int openLock(vector& dr, string t) { 6 | ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 7 | if(t=="0000"){ 8 | return 0; 9 | } 10 | unordered_set d; 11 | for(string a: dr){ 12 | if(a==t || a=="0000") return -1; 13 | d.insert(a); 14 | } 15 | queue q; 16 | unordered_map visited; 17 | int count=0,size; 18 | string rw,root; 19 | string start="0000"; 20 | visited[start]=1; 21 | q.push(start); 22 | while(!q.empty()){ 23 | ++count; 24 | size=q.size(); 25 | for(int i=0;i 1-2-3-Null => H1 at 1 H2 at 3 8 | 2. -> 1-2-3-4-5-6-Null => H1 at 3 and H2 at 4 9 | 3. REVERSE THE FIRST HALF AND COMPARE IT WITH THE SECOND HALF. 10 | */ 11 | 12 | class Solution { 13 | public: 14 | void reverse(ListNode* head, ListNode* last){ 15 | if(!head || head->next==last) return; 16 | ListNode *curr=head,*prev=nullptr; 17 | while (curr->next!=last){ 18 | ListNode *nxt=curr->next; 19 | curr->next=prev; 20 | prev=curr; 21 | curr=nxt; 22 | } 23 | curr->next=prev; 24 | } 25 | 26 | bool isPalindrome(ListNode* head) { 27 | if(!head || !head->next) return 1; 28 | ListNode *f=head,*s=head; 29 | ListNode *h1,*h2; 30 | while (true){ 31 | //Important logic 32 | if(s->next->next==nullptr){ 33 | h1=f; 34 | h2=h1->next; 35 | break; 36 | } 37 | s=s->next->next; 38 | if(s->next==nullptr) { 39 | h1=f; 40 | h2=h1->next->next; 41 | break; 42 | } 43 | f=f->next; 44 | } 45 | //reverse head->...->h1 to h1->...->head 46 | reverse(head,h1->next); 47 | while (h1 && h2){ 48 | if(h1->val!=h2->val) return 0; 49 | h1=h1->next; 50 | h2=h2->next; 51 | } 52 | return 1; 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/Palindrome_0ms.cpp: -------------------------------------------------------------------------------- 1 | /* converting int to string using to_string and looping through it takes 8 ms to run but if I reverse int using a while loop, it runs in 0ms! */ 2 | 3 | class Solution { 4 | public: 5 | bool isPalindrome(int x) { 6 | if (x<0) return false; 7 | if(x==0) return true; 8 | long long ans=0; 9 | int copy=x; 10 | while(x){ 11 | ans=ans*10+(x%10); 12 | x/=10; 13 | } 14 | if((ans-copy)==0) return true; 15 | else return false; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/PathSum.cpp: -------------------------------------------------------------------------------- 1 | //BINARY TREE:: RECURSION 2 | class Solution { 3 | public: 4 | void recur(TreeNode* root,int sum,int curr,bool& found){ 5 | if(!root) return; 6 | if(found) return; 7 | curr+=root->val; 8 | if(!root->left && !root->right && curr==sum){ 9 | found=1; 10 | return; 11 | } 12 | recur(root->left,sum,curr,found); 13 | recur(root->right,sum,curr,found); 14 | return; 15 | } 16 | 17 | bool hasPathSum(TreeNode* root, int sum) { 18 | bool found=0; 19 | recur(root,sum,0,found); 20 | if(found) return 1; 21 | return 0; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/PlusOne.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | PROBLEM: I/P={1,2,3} -> ADD ONE TO IT -> O/P={1,2,4} 3 | - DONE USING CARRY 4 | ONLY CASE WHEN WE HAVE TO CHANGE THE SIZE IS WHEN ALL THE ELEMENTS ARE 9! 5 | - IT BECOMES 1...xxxxxxx...0 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector plusOne(vector& digits) { 11 | int carry=1; 12 | for(int i=digits.size()-1;i>=0;--i){ 13 | if(digits[i]+carry==10){ 14 | digits[i]=0; 15 | } 16 | else{ 17 | digits[i]+=carry; 18 | carry=0; 19 | break; 20 | } 21 | } 22 | if(carry){ 23 | digits[0]=1; 24 | digits.push_back(0); 25 | } 26 | return digits; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RemoveDuplicatesFromSortedArray.cpp: -------------------------------------------------------------------------------- 1 | //*******Using UNIQUE********* 2 | //Using erase- 20ms 3 | class Solution { 4 | public: 5 | int removeDuplicates(vector& nums) { 6 | nums.erase(unique(nums.begin(), nums.end()), nums.end()); 7 | return nums.size(); 8 | } 9 | }; 10 | //Using resize-12ms 11 | class Solution { 12 | public: 13 | int removeDuplicates(vector& nums) { 14 | nums.resize(distance(nums.begin(),unique(nums.begin(), nums.end()))); 15 | return nums.size(); 16 | } 17 | }; 18 | 19 | //Faster than 99.7% 20 | class Solution { 21 | public: 22 | int removeDuplicates(vector& nums) { 23 | if (nums.size()==0){ 24 | return 0; 25 | } 26 | if (nums.size()==1){ 27 | return 1; 28 | } 29 | int index=0,i,count=1; 30 | for (i=1;i& nums) { 44 | int count=1; 45 | if (nums.size()==0){ 46 | return 0; 47 | } 48 | auto first=nums.begin(); 49 | for (auto ptr=nums.begin()+1;ptr!=nums.end();++ptr){ 50 | if (*ptr==*first){ 51 | nums.erase(ptr); 52 | --ptr; 53 | } 54 | else{ 55 | first=ptr; 56 | ++count; 57 | } 58 | } 59 | return count; 60 | } 61 | }; 62 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RemoveDuplicatesIIFromSortedLinkedList.cpp: -------------------------------------------------------------------------------- 1 | // We have to delete all the occurrences of the element if it occurrs more than once in the list. 2 | // LIKE 1-2-3-3-3-4 BECOMES 1-2-4 3 | // Used 3 pointers i.e. t,f,s for simplicity; t stays back while f and s traverse ahead 4 | // Can be done only using two pointers where s=f->next! 5 | 6 | class Solution { 7 | public: 8 | ListNode* deleteDuplicates(ListNode* head) { 9 | if(head==nullptr || head->next==nullptr) return head; 10 | ListNode *ans= new ListNode(69, nullptr); 11 | ListNode *t=ans,*f=head,*s=f->next; 12 | 13 | while(true){ 14 | 15 | if(f->val==s->val){ 16 | while (s->val==f->val){ 17 | s=s->next; 18 | if(s==nullptr){ 19 | break; 20 | } 21 | } 22 | if(s==nullptr || s->next==nullptr){ 23 | t->next=s; 24 | break; 25 | } 26 | f=s; 27 | s=s->next; 28 | } 29 | else{ 30 | t->next=f; 31 | t=t->next; 32 | f=t->next; 33 | s=f->next; 34 | if(s==nullptr){ 35 | t->next=f; 36 | break; 37 | } 38 | } 39 | } 40 | return ans->next; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RemoveDuplicatesILinkedList.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteDuplicates(ListNode* head) { 4 | ListNode *tmp; 5 | tmp=head; 6 | while (tmp!=nullptr && tmp->next!=nullptr){ 7 | if(tmp->val==tmp->next->val){ 8 | tmp->next=tmp->next->next; 9 | } 10 | else{ 11 | tmp=tmp->next; 12 | } 13 | } 14 | return head; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RemoveElement.cpp: -------------------------------------------------------------------------------- 1 | //1. Maintaining order 2 | class Solution { 3 | public: 4 | int removeElement(vector& nums, int val) { 5 | int index=0; 6 | // Using the following loop reduced time of execution 7 | // Instead of for loop 8 | for (int a:nums){ 9 | if (a!=val){ 10 | nums[index]=a; 11 | ++index; 12 | } 13 | } 14 | return index; 15 | } 16 | }; 17 | 18 | //2. Without maintaining order 19 | 20 | class Solution { 21 | public: 22 | int removeElement(vector& nums, int val) { 23 | int index=0,size=nums.size(),i=0; 24 | while(inext=head; 17 | ListNode *f=dummy,*s=head,*tmp=nullptr; 18 | while(s){ 19 | if(s->next==nullptr){ //FOR LAST ELEMENT 20 | s->val==val ? f->next=nullptr : f->next=s; 21 | f->next=nullptr; 22 | break; 23 | } 24 | if(s->val==val){ 25 | s=s->next; 26 | } 27 | else{ 28 | f->next=s; 29 | f=f->next; 30 | s=s->next; 31 | } 32 | } 33 | return dummy->next; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RemoveNthNodeFromEndOfLinkedList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Approach : traverse list using two pointers. 3 | 1. initialize two ptrs to a dummy node which points at the head of list. 4 | 2. separate two pointers by a distance of n (n is the index(1-based) of the element which is to be deleted (counting backwards)) 5 | 3. traverse the list by maintaining the distance and incrementing both ptrs till the leading ptr reaches nullptr (i.e. end) 6 | 4. set trailing ptr's next to its next's next. 7 | */ 8 | 9 | class Solution { 10 | public: 11 | ListNode* removeNthFromEnd(ListNode* head, int n) { 12 | ListNode *first,*second; 13 | ListNode *dummy= new ListNode(69,head); 14 | first=dummy; 15 | second=dummy; 16 | 17 | for(int i=0;i<=n;++i){ 18 | second=second->next; 19 | } 20 | 21 | while(second!=nullptr){ 22 | first=first->next; 23 | second=second->next; 24 | } 25 | first->next=first->next->next; 26 | return dummy->next; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ReplaceElementsWithGreatestElementOnRightSide.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector replaceElements(vector& arr) { 4 | if(arr.size()==0) return arr; 5 | int max=arr[arr.size()-1],tmp; 6 | for(int i=arr.size()-1;i>=0;--i){ 7 | if(i==arr.size()-1){ 8 | arr[i]=-1; 9 | } 10 | else{ 11 | tmp=arr[i]; 12 | arr[i]=max; 13 | if(max INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0; 20 | //similarly for negative integers... 21 | if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0; 22 | rev = rev * 10 + pop; 23 | } 24 | return rev; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ReverseInteger_0ms.textClipping: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Siddhesh/Leetcode/ReverseInteger_0ms.textClipping -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ReverseLinkedList.cpp: -------------------------------------------------------------------------------- 1 | // Ideal approach:: 2 | class Solution { 3 | public: 4 | ListNode* reverseList(ListNode* head) { 5 | if(!head || !head->next) return head; 6 | ListNode *curr=head,*prev=nullptr; 7 | while(curr->next){ 8 | ListNode *nxt=curr->next; 9 | curr->next=prev; 10 | prev=curr; 11 | curr=nxt; 12 | } 13 | curr->next=prev; 14 | return curr; 15 | } 16 | }; 17 | 18 | // USING RECURSION! 19 | class Solution { 20 | public: 21 | ListNode* reverseList(ListNode* head) { 22 | if(!head || !head->next) return head; 23 | ListNode *p=reverseList(head->next); // p will actually be the last node i.e. the first node after reversing 24 | head->next->next=head; 25 | head->next=nullptr; 26 | return p; 27 | } 28 | }; 29 | 30 | 31 | // Non ideal approach .. moving to end of list 32 | class Solution { 33 | public: 34 | ListNode* reverseList(ListNode* head) { 35 | if(head==nullptr || head->next==nullptr) return head; 36 | ListNode *first=head,*second=head->next,*tmp=nullptr; 37 | while(head->next!=nullptr){ 38 | head=head->next; 39 | } 40 | //head as last i.e. 5 41 | //consider last as head 42 | 43 | tmp=head->next; 44 | while(first!=nullptr){ 45 | first->next=tmp; 46 | tmp=first; 47 | first=second; 48 | if(second!=nullptr){ 49 | second=second->next; 50 | } 51 | } 52 | return head; 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/RomanToInteger.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ******* 3 | PREVIOUS APPROACH USING MAP... IT TOOK AROUND 24-36 ms TO EXECUTE 4 | ******* 5 | int romanToInt(string s) { 6 | int i,size=s.size(),sum=0; 7 | map m = { 8 | {'I',1}, 9 | {'V',5}, 10 | {'X',10}, 11 | {'L',50}, 12 | {'C',100}, 13 | {'D',500}, 14 | {'M',1000}, 15 | {' ',0}}; 16 | for (i=0;iLENGTH.. AS THE PATTERN IS REPEATED ONCE K EXCEEDS LENGTH. 5 | 3. IF REMAINDER == 0 RETURN ORIGINAL LIST 6 | 4. MOVE TO THE LEN-REMAINDER-1 TH ELEMENT WITH THE HELP OF A POINTER I.E. TILL THE ELEMENT UP TO WHICH THERE WILL BE NO CHANGE IN THE LIST 7 | 5. MAKE ITS NEXT A HEAD (TO BE RETURNED) 8 | 6. MAKE ITS NEXT NULL 9 | 7. MAKE LAST'S NEXT POINT TO INITIAL HEAD 10 | */ 11 | 12 | /* 13 | FOR K=2:: 14 | 15 | head last 16 | | | 17 | 1 - 2 - 3 - 4 - 5 - NULL 18 | | 19 | ptr 20 | */ 21 | class Solution { 22 | public: 23 | ListNode* rotateRight(ListNode* head, int k) { 24 | if(head==nullptr || head->next==nullptr || k==0) return head; 25 | int len=1; 26 | ListNode *last= head,*ptr=head,*ans=nullptr; 27 | while(last->next!=nullptr){ 28 | last=last->next; 29 | ++len; 30 | } 31 | int no = k%len; 32 | if(no==0) return head; 33 | for(int i=0;inext; 35 | } 36 | ans=ptr->next; 37 | ptr->next=nullptr; 38 | last->next=head; 39 | return ans; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/SearchInsertPosition.cpp: -------------------------------------------------------------------------------- 1 | // Used lower_bound 2 | class Solution { 3 | public: 4 | int searchInsert(vector& nums, int target) { 5 | return (int)(lower_bound(nums.begin(),nums.end(),target)-nums.begin()); 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/SingleNumber.cpp: -------------------------------------------------------------------------------- 1 | /* MY INITIAL APPROACH:: Time O(n), Space O(n) 2 | Used a haspmap... 3 | class Solution { 4 | public: 5 | int singleNumber(vector& nums) { 6 | map m; 7 | for(int a:nums){ 8 | m[a]++; 9 | } 10 | for (auto b:m){ 11 | if(b.second==1){ 12 | return b.first; 13 | break; 14 | } 15 | } 16 | return 0; 17 | } 18 | }; 19 | */ 20 | 21 | /*Following is a better approach which uses xor operator: Time O(n), Space O(1) ... runs in 12ms 22 | NOTE: 1. 0 ^ (xor) a number = that number itself 23 | 2. number ^ same number = 0 24 | 3. thus if a number comes two times then value of xor always becomes 0. 25 | 4. if a number occurs only once then the final value after all xor operations is equal to that number! 26 | */ 27 | 28 | class Solution { 29 | public: 30 | int singleNumber(vector& nums) { 31 | int x=0; 32 | for (int a: nums){ 33 | x=x^a; 34 | } 35 | return x; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/SortArrayByParity.cpp: -------------------------------------------------------------------------------- 1 | // Changing array such that even nos. come first and then there are odd nos. 2 | 3 | class Solution { 4 | public: 5 | vector sortArrayByParity(vector& A) { 6 | int tmp; 7 | int i=0,j=A.size()-1; 8 | bool i_even,j_even; 9 | while (ix && mid*midx){ 12 | end=mid-1; 13 | } 14 | else{ 15 | start=mid+1; 16 | } 17 | } 18 | } 19 | return mid; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/SwapNodesInPairsLinkedList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | i/p 1-2-3-4-5-nullptr => o/p 2-1-4-3-5 3 | YOU CAN'T JUST SWAP THE VALUES... YOU HAVE TO SWAP ADJACENT NODES 4 | 5 | IDEA:: 6 | 1. Iterate using two ptrs. 7 | *. Temporary node->next=second (not in first iteration... Check 4th step) 8 | 2. First->next = second->next 9 | 3. Second->next = first 10 | 4. Save the first node in a temporary pointer 11 | */ 12 | 13 | class Solution { 14 | public: 15 | ListNode* swapPairs(ListNode* head) { 16 | if(head==nullptr) return head; 17 | if(head->next==nullptr) return head; 18 | ListNode *first=head,*second=head->next,*tmp=nullptr; 19 | 20 | while (first!=nullptr && second!=nullptr){ 21 | if(tmp==nullptr){ 22 | first->next=second->next; 23 | second->next=first; 24 | head=second; 25 | tmp=first; 26 | } 27 | else{ 28 | tmp->next=second; 29 | first->next=second->next; 30 | second->next=first; 31 | tmp=first; 32 | } 33 | first=first->next; 34 | if(first!=nullptr){ 35 | second=first->next; 36 | } 37 | } 38 | return head; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/SymmetricTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | RECURSION:: 3 | TO THINK PROPERLY, 4 | THINK ONLY ABOUT SUBPART OF A TREE i.e a parent and its children 5 | 1. L->R should be equal to R->L and vice versa. 6 | 2. AND THEN DECIDE THE EDGECASES 7 | - LIKE BOTH SHOULD END AT THE SAME TIME etc. 8 | */ 9 | 10 | class Solution { 11 | public: 12 | bool recur(TreeNode *l,TreeNode *r){ 13 | if(!l && !r) return 1; // ending case where both nodes are not valid 14 | if(!l && r || l && !r) return 0; // if only one node is valid return false 15 | return (l->val==r->val) && (recur(l->right,r->left)) && recur(l->left,r->right); 16 | } 17 | 18 | bool isSymmetric(TreeNode* root) { 19 | if(!root) return 1; 20 | if(!root->left && !root->right) return 1; 21 | return recur(root->left,root->right); 22 | } 23 | }; 24 | 25 | 26 | //USING QUEUE:: 27 | class Solution { 28 | public: 29 | bool isSymmetric(TreeNode* root) { 30 | if(!root) return 1; 31 | if(!root->left && !root->right) return 1; 32 | if(!root->left && root->right || root->left && !root->right) return 0; 33 | queue ql,qr; 34 | if(root->left->val==root->right->val){ 35 | ql.push(root->left); 36 | qr.push(root->right); 37 | } 38 | else{ 39 | return 0; 40 | } 41 | while(!ql.empty() || !qr.empty()){ 42 | TreeNode *l=ql.front(),*r=qr.front(); 43 | ql.pop(); 44 | qr.pop(); 45 | if(l->left && r->right){ 46 | if(l->left->val==r->right->val){ 47 | ql.push(l->left); 48 | qr.push(r->right); 49 | } 50 | else{ 51 | return 0; 52 | } 53 | } 54 | else if(!r->right && l->left || r->right && !l->left){ 55 | return 0; 56 | } 57 | if(l->right && r->left){ 58 | if(l->right->val==r->left->val){ 59 | ql.push(l->right); 60 | qr.push(r->left); 61 | } 62 | else{ 63 | return 0; 64 | } 65 | } 66 | else if(!r->left && l->right || r->left && !l->right){ 67 | return 0; 68 | } 69 | } 70 | return 1; 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ThirdMaximumNumber.cpp: -------------------------------------------------------------------------------- 1 | // T-O(N) && S-O(1) 2 | class Solution { 3 | public: 4 | int thirdMax(vector& nums) { 5 | long long int max1=LONG_MIN; 6 | long long int max2=LONG_MIN; 7 | long long int max3=LONG_MIN; 8 | int sz=nums.size(); 9 | for(int i=0;imax1) 12 | { 13 | max3=max2; 14 | max2=max1; 15 | max1=nums[i]; 16 | } 17 | else if(nums[i]>max2 && nums[i]!=max1) 18 | { 19 | max3=max2; 20 | max2=nums[i]; 21 | } 22 | else if(nums[i]>max3 && nums[i]!=max1 && nums[i]!=max2) 23 | { 24 | max3=nums[i]; 25 | } 26 | } 27 | 28 | return max3==LONG_MIN ? max1 : max3; 29 | } 30 | }; 31 | 32 | // T-O(N) && S-O(N) 33 | class Solution { 34 | public: 35 | int thirdMax(vector& nums) { 36 | set s; 37 | for(int a:nums){ 38 | s.insert(a); 39 | } 40 | auto ptr = s.rbegin(); 41 | if(s.size()>=3){ 42 | ++ptr; 43 | ++ptr; 44 | return *ptr; 45 | } 46 | return *ptr; 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/TwoSum.cpp: -------------------------------------------------------------------------------- 1 | // First approach was brute force... two nested for loops -> O(n^2) 2 | 3 | // Below is a better approach which uses map 4 | // KEY -> NUMBER | because find() works only for key NOT value! 5 | // VALUE -> INDEX | 6 | // Used **unordered_map** because look-up i.e. find() takes constant time O(1) most of the times... 7 | // Whereas find() in map requires O(logn) time. 8 | 9 | 10 | 11 | 12 | 13 | class Solution { 14 | public: 15 | vector twoSum(vector& nums, int target) { 16 | unordered_map m; 17 | unordered_map::iterator ptr; 18 | int i,size=nums.size(),diff; 19 | for (i=0;isecond,i}; 24 | } 25 | m.insert({nums[i],i}); 26 | } 27 | return {}; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ValidMoumtainArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector& A) { 4 | if(A.size()==0 || A.size()==1) return 0; 5 | int i=0; 6 | 7 | //increasing 8 | while (i<=A.size()-2 && A[i]A[i+1]){ 19 | ++i; 20 | } 21 | 22 | // If i is at the end... return true 23 | if(i==A.size()-1) return 1; 24 | return 0; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/ValidParentheses.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Used Stack like approach: 3 | For e.g. for "([])" : 4 | for first two input chars... stack will be : 5 | [ 6 | ( 7 | then ] comes next... [ will be popped from top.. stack now just has ( 8 | then for )... ( will be popped. 9 | Thus for every valid case... final size of the stack will be zero! 10 | */ 11 | 12 | class Solution { 13 | public: 14 | bool isValid(string s) { 15 | int i,size=s.length(); 16 | vector stac; 17 | if(size==0) return true; 18 | if(size==1) return false; 19 | for (i=0;i inorderTraversal(TreeNode* root) { 7 | if(!root) return {}; 8 | TreeNode *curr=root; 9 | vector v; 10 | stack s; 11 | while(curr || !s.empty()){ 12 | while(curr){ 13 | s.push(curr); 14 | curr=curr->left; 15 | } 16 | curr=s.top(); 17 | s.pop(); 18 | v.push_back(curr->val); 19 | curr=curr->right; 20 | } 21 | return v; 22 | } 23 | }; 24 | //RECURSION 25 | class Solution { 26 | public: 27 | void recur(vector& v,TreeNode *root){ 28 | if(root->left){ 29 | recur(v,root->left); 30 | } 31 | v.push_back(root->val); 32 | if(root->right){ 33 | recur(v,root->right); 34 | } 35 | return; 36 | } 37 | 38 | vector inorderTraversal(TreeNode* root) { 39 | if(!root) return {}; 40 | vector v; 41 | recur(v,root); 42 | return v; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/_BinaryTreeLevelOrderTraversal.cpp: -------------------------------------------------------------------------------- 1 | //RECURSIVE APPROACH REMAINING 2 | 3 | //BFS Using Queue 4 | class Solution { 5 | public: 6 | vector> levelOrder(TreeNode* root) { 7 | if(!root) return {}; 8 | vector> v; 9 | queue q; 10 | q.push(root); 11 | v.push_back({root->val}); 12 | while(!q.empty()){ 13 | vector vi; 14 | int size=q.size(); 15 | for(int i=0;ileft){ 19 | q.push(node->left); 20 | vi.push_back(node->left->val); 21 | } 22 | if(node->right){ 23 | q.push(node->right); 24 | vi.push_back(node->right->val); 25 | } 26 | } 27 | if(vi.size()){ 28 | v.push_back(vi); 29 | } 30 | } 31 | return v; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/_BinaryTreePostOrderTraversal.cpp: -------------------------------------------------------------------------------- 1 | //Iterative approach remaining 2 | 3 | class Solution { 4 | public: 5 | void recur(vector& v,TreeNode* root){ 6 | if(root->left){ 7 | recur(v,root->left); 8 | } 9 | if(root->right){ 10 | recur(v,root->right); 11 | } 12 | v.push_back(root->val); 13 | return; 14 | } 15 | 16 | vector postorderTraversal(TreeNode* root) { 17 | if(!root) return {}; 18 | vector v; 19 | recur(v,root); 20 | return v; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Siddhesh/Leetcode/_PopulatingNextRightPointersInEachNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | Node* left; 7 | Node* right; 8 | Node* next; 9 | 10 | Node() : val(0), left(NULL), right(NULL), next(NULL) {} 11 | 12 | Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 13 | 14 | Node(int _val, Node* _left, Node* _right, Node* _next) 15 | : val(_val), left(_left), right(_right), next(_next) {} 16 | }; 17 | */ 18 | //RECURSIVE APPROACH REMAINING 19 | class Solution { 20 | public: 21 | Node* connect(Node* root) { 22 | if(!root) return root; 23 | Node *top,*nxt; 24 | queue q; 25 | q.push(root); 26 | while(!q.empty()){ 27 | int size=q.size(); 28 | for(int i=0;inext=nxt; 33 | if(top->left){ 34 | q.push(top->left); 35 | } 36 | if(top->right){ 37 | q.push(top->right); 38 | } 39 | } 40 | top->next=NULL; 41 | } 42 | return root; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /Siddhesh/Notes/FindAllNumbersDisappearedInAnArray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Siddhesh/Notes/FindAllNumbersDisappearedInAnArray.jpg -------------------------------------------------------------------------------- /Siddhesh/Notes/Screenshot_2020-06-02-21-18-49-48.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Siddhesh/Notes/Screenshot_2020-06-02-21-18-49-48.jpg -------------------------------------------------------------------------------- /Siddhesh/Notes/a.out: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Siddhesh/Playground/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Siddhesh/Playground/a.out -------------------------------------------------------------------------------- /Siddhesh/Playground/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /Siddhesh/Playground/output.txt: -------------------------------------------------------------------------------- 1 | 123 -------------------------------------------------------------------------------- /Siddhesh/Playground/playground.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Read i/p Write o/p 5 | #define FILE freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); 6 | 7 | #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 8 | #define pb push_back 9 | #define ll long long 10 | #define vi vector 11 | #define vii vector 12 | #define pi pair 13 | #define MP make_pair 14 | #define B begin() 15 | #define E end() 16 | #define S size() 17 | #define L length() 18 | #define pf printf 19 | #define sf scanf 20 | 21 | int main(){ 22 | FILE 23 | FAST 24 | int n; 25 | cin>>n; 26 | vi v = {1,2,3}; 27 | for(int i=0;i 3 | using namespace std; 4 | 5 | #define FILE freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdin); 6 | #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 7 | #define pb push_back 8 | #define ll long long 9 | #define vi vector 10 | #define vii vector 11 | #define pi pair 12 | #define MP make_pair 13 | #define B begin() 14 | #define E end() 15 | #define S size() 16 | #define L length() 17 | #define pf printf 18 | #define sf scanf 19 | 20 | int main(){ 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Siddhesh/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Siddhesh/a.out -------------------------------------------------------------------------------- /Siddhesh/foobar_withgoogle/Level 1/braille.py: -------------------------------------------------------------------------------- 1 | #Convert sentences containing letters into Braille where: 2 | # 1-> bump , 0-> flat 3 | 4 | Dict = { 5 | "a" : "100000", 6 | "b" : "110000", 7 | "c" : "100100", 8 | "d" : "100110", 9 | "e" : "100010", 10 | "f" : "110100", 11 | "g" : "110110", 12 | "h" : "110010", 13 | "i" : "010100", 14 | "j" : "010110", 15 | "k" : "101000", 16 | "l" : "111000", 17 | "m" : "101100", 18 | "n" : "101110", 19 | "o" : "101010", 20 | "p" : "111100", 21 | "q" : "111110", 22 | "r" : "111010", 23 | "s" : "011100", 24 | "t" : "011110", 25 | "u" : "101001", 26 | "v" : "111001", 27 | "w" : "010111", 28 | "x" : "101101", 29 | "y" : "101111", 30 | "z" : "101011", 31 | } 32 | ans="" 33 | s = input() 34 | for i in s: 35 | if (i==" "): 36 | ans+="000000" 37 | elif (i.isupper()): 38 | ans+="000001"+Dict[i.lower()] 39 | else: 40 | ans+=Dict[i] 41 | 42 | print(ans) 43 | -------------------------------------------------------------------------------- /Siddhesh/foobar_withgoogle/Level 2/maximum_product_of_a_subset_of_an_array.py: -------------------------------------------------------------------------------- 1 | # [-2,-3,4,-5] -> '60' 2 | # LOGIC:: 3 | # If no. of -ve elements are even-> Simply multiply every element! 4 | # If -ve elements are odd, divide the above product by the negative element which has least absolute value(mod) 5 | 6 | def solution(xs): 7 | 8 | if(len(xs)==1): 9 | return str(xs[0]) 10 | 11 | zeroes=0 12 | negatives=0 13 | 14 | #variable below is used to find negative element with min absolute value(mod) 15 | least=-999999999 16 | 17 | ans=1 18 | 19 | for i in xs: 20 | if(i==0): 21 | zeroes+=1 22 | #continue is used to avoid multiplication by 0 23 | continue 24 | if(i<0): 25 | negatives+=1 26 | if(i>least): 27 | least=i 28 | ans*=i 29 | if(zeroes==len(xs)): 30 | return '0' 31 | if(negatives%2!=0): 32 | if(negatives==1 and zeroes>0 and negatives+zeroes==len(xs)): 33 | # for e.g. -> [0,0,-2,0] 34 | return '0' 35 | ans/=least 36 | return str(int(ans)) 37 | -------------------------------------------------------------------------------- /Sourabh/book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushnbali/competitive-coding/f16bb1c62ff7f7328b05c664dffdbc051c7f3c8f/Sourabh/book.pdf -------------------------------------------------------------------------------- /Tanishq: -------------------------------------------------------------------------------- 1 | include 2 | void main() { 3 | printf("test file."); 4 | } 5 | -------------------------------------------------------------------------------- /Vatsal Arya/CodeChef/BytelandianGoldCoins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long change(long long n){ 6 | long long n1=n/2; 7 | long long n2=n/3; 8 | long long n3=n/4; 9 | if(n1>11) 10 | n1=change(n1); 11 | if(n2>11) 12 | n2=change(n2); 13 | if(n3>11) 14 | n3=change(n3); 15 | { 16 | if(n1+n2+n3>n) 17 | return n1+n2+n3; 18 | else 19 | return n; 20 | } 21 | } 22 | int main() { 23 | long long n{}; 24 | while(cin>>n){ 25 | cout< 2 | using namespace std; 3 | 4 | float area(int x1, int y1, int x2, int y2, int x3, int y3) { 5 | return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); 6 | } 7 | 8 | bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) { 9 | float A = area (x1, y1, x2, y2, x3, y3); 10 | float A1 = area (x, y, x2, y2, x3, y3); 11 | float A2 = area (x1, y1, x, y, x3, y3); 12 | float A3 = area (x1, y1, x2, y2, x, y); 13 | return (A == A1 + A2 + A3); 14 | } 15 | 16 | int main() { 17 | int t; 18 | cin>>t; 19 | while(t--){ 20 | int x1,x2,x3,y1,y2,y3,x,y; 21 | cin>>x1>>y1>>x2>>y2>>x3>>y3>>x>>y; 22 | if(isInside(x1,y1,x2,y2,x3,y3,x,y)) 23 | cout<<"INSIDE"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin >> t; 7 | while(t--){ 8 | int table[256] = {0}; 9 | int n{},i{},flag{1}; 10 | cin >> n; 11 | string x; 12 | while(n--){ 13 | cin >> x; 14 | if(table[x[0]] == 1) 15 | flag = 0; 16 | else table[x[0]] = 1; 17 | } 18 | if(flag) cout << "YES\n"; 19 | else cout << "NO\n"; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Vatsal Arya/HackerEarth/EncodedStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | string S; 5 | cin>>S; 6 | int n=S.length(); 7 | assert(n >= 1 && n <= 100000); 8 | long long sum=0; 9 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int t{},n{},count{}; 6 | cin>>t; 7 | while(t--){ 8 | count=0; 9 | cin>>n; 10 | int arr[n],len{},lis[n]; 11 | for(int i=0; i>arr[i]; 13 | len=0; 14 | for (int i = 0; i < n; i++) 15 | lis[i] = 1; 16 | for (int i = 1; i < n; i++) { 17 | for (int j = 0; j < i; j++) { 18 | if (arr[i] > arr[j] && (i-j)<=(arr[i]-arr[j])){ 19 | lis[i] = max(lis[i], lis[j] + 1); 20 | } 21 | } 22 | len = max(len, lis[i]); 23 | } 24 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int t{},j{},a{},b{},count{},n{}; 6 | cin>>t; 7 | vector vec; 8 | while(t--){ 9 | count=0; 10 | cin>>a>>b; 11 | for(int i=a; i<=b; i++){ 12 | vec={}; 13 | n=i; 14 | while(n>0){ 15 | vec.push_back(n%10); 16 | n /= 10; 17 | } 18 | for(j=0; j 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int t{},n{},min{},count{}; 7 | cin>>t; 8 | while(t--){ 9 | min=10000000; 10 | count=0; 11 | cin>>n; 12 | int arr[n]; 13 | for(int i=0; i>arr[i]; 15 | if(arr[i]=2) 18 | count += arr[i]-1; 19 | else 20 | count++; 21 | } 22 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | cout<>a>>b>>n; 9 | cout<<((n-b)/(a-b)); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Vatsal Arya/HackerEarth/readme.txt: -------------------------------------------------------------------------------- 1 | Repo for competitive codeing questions that I've solved! -------------------------------------------------------------------------------- /Vatsal Arya/readme.txt: -------------------------------------------------------------------------------- 1 | Repo for competitive codeing questions that I've solved! -------------------------------------------------------------------------------- /Vishal Patil/10_beautiful_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // your code goes here 6 | int i,j,a[5][5]; 7 | for(i=0;i<5;i++) 8 | { 9 | for(j=0;j<5;j++) 10 | { 11 | scanf("%d",&a[i][j]); 12 | if(a[i][j]==1) 13 | { 14 | printf("%d\n",abs(2-i)+abs(2-j)); 15 | } 16 | } 17 | } 18 | 19 | return 0; 20 | } 21 |   22 | -------------------------------------------------------------------------------- /Vishal Patil/12_game_with_sticks.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // your code goes here 6 | int a,b,c; 7 | scanf("%d%d",&a,&b); 8 | c=a*b; 9 | if(a>=b) 10 | { 11 | if((c/a)%2==0) 12 | { 13 | printf("Malvika\n"); 14 | } 15 | else 16 | { 17 | printf("Akshat\n"); 18 | } 19 | 20 | } 21 | else 22 | { 23 | if((c/b)%2==0) 24 | { 25 | printf("Malvika\n"); 26 | } 27 | else 28 | { 29 | printf("Akshat\n"); 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Vishal Patil/13_games.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // your code goes here 6 | int t,i,a[30][2],j,v; 7 | scanf("%d",&t); 8 | for(i=0;i 2 | 3 | int main(void) { 4 | // your code goes here 5 | int t,i,a[100][3],sumx,sumy,sumz; 6 | scanf("%d",&t); 7 | for(i=0;i 2 | using namespace std; 3 | 4 | int main(void) 5 | { 6 | // your code goes here 7 | int n,m,a[50],i,m1[50],min; 8 | cin>>n>>m; 9 | for(i=0;i>a[i]; 12 | } 13 | sort(a,a+m); 14 | for(i=0;im1[i]) 22 | { 23 | min=m1[i]; 24 | } 25 | } 26 | cout< 2 | 3 | int main(void) 4 | { 5 | // your code goes here 6 | int a,b,c,i,max,a1[6]; 7 | scanf("%d%d%d",&a,&b,&c); 8 | a1[0]=a+b*c; 9 | a1[1]=a*b+c; 10 | a1[2]=a*(b+c); 11 | a1[3]=(a+b)*c; 12 | a1[4]=a*b*c; 13 | a1[5]=a+b+c; 14 | max=a1[0]; 15 | for(i=0;i<6;i++) 16 | { 17 | if(max 2 | 3 | int main(void) 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | if(t%2==0&&t>2) 8 | printf("YES"); 9 | else 10 | printf("NO"); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Vishal Patil/20_even_or_odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // your code goes here 6 | long long int a,b; 7 | scanf("%lld%lld",&a,&b); 8 | if(a%2==0) 9 | { 10 | if(b<=a/2) 11 | { 12 | printf("%lld\n",2*b-1); 13 | } 14 | else 15 | { 16 | printf("%lld\n",2*(b-a/2)); 17 | } 18 | } 19 | else 20 | { 21 | if(b<=1+a/2) 22 | { 23 | printf("%lld\n",2*b-1); 24 | } 25 | else 26 | { 27 | printf("%lld\n",2*(-1+b-a/2)); 28 | } 29 | 30 | } 31 | 32 | return 0; 33 | } 34 |   35 | -------------------------------------------------------------------------------- /Vishal Patil/2_way_too_long_words.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int t,i; 7 | char a[100]; 8 | scanf("%d",&t); 9 | for(i=0;i10) 13 | { 14 | printf("%c%d%c\n",a[0],strlen(a)-2,a[strlen(a)-1]); 15 | } 16 | else 17 | { 18 | printf("%s\n",a); 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Vishal Patil/5_helpful_math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,i,j,v; 7 | char a[100],cm,b[100]; 8 | scanf("%s",a); 9 | j=0; 10 | if(a[1]!='\0') 11 | { 12 | for(i=0;a[i]!='\0';i++) 13 | { 14 | cm=a[i]; 15 | if(a[i]!=a[1]) 16 | { 17 | b[j]=a[i]; 18 | //i++; 19 | j++; 20 | 21 | } 22 | } 23 | } 24 | //printf("%d\n",j); 25 | 26 | if(j==0) 27 | { 28 | printf("%s",a); 29 | } 30 | else 31 | { 32 | sort(b,b+j); 33 | for(v=0;v 2 | using namespace std; 3 | int main(void) 4 | { 5 | int t,i,sum,m,v,a[100]; 6 | cin>>t; 7 | sum=0; 8 | for(i=0;i=0;i--) 21 | { 22 | v+=a[i]; 23 | if(v 2 | int fun(int n) 3 | { 4 | int i,k,m; 5 | m=0; 6 | for(i=0;n>0;i++) 7 | { 8 | k=n%10; 9 | n=n/10; 10 | if(k==4||k==7) 11 | { 12 | m++; 13 | 14 | } 15 | } 16 | if(m==i) 17 | { 18 | return 1; 19 | } 20 | else 21 | { 22 | return 0; 23 | } 24 | } 25 | int main(void) 26 | { 27 | int t,i,j; 28 | scanf("%d",&t); 29 | for(i=1;i<=t;i++) 30 | { 31 | if(t%i==0) 32 | { 33 | j=fun(i); 34 | } 35 | if(j==1) 36 | { 37 | break; 38 | } 39 | } 40 | if(j==0) 41 | { 42 | printf("NO\n"); 43 | } 44 | else 45 | { 46 | printf("YES\n"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Vishal Patil/readme.md: -------------------------------------------------------------------------------- 1 | This repo contains the Codeforces Div.2 A problems 2 | -------------------------------------------------------------------------------- /file.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int a,b; 7 | cin >> a; 8 | cin >> b; 9 | for(int i = a ; i<=b ; i++){ 10 | if (i>=1 && i<=9) 11 | switch(i) 12 | { 13 | case 1: 14 | cout << "one\n"; 15 | break; 16 | case 2: 17 | cout << "two\n"; 18 | break; 19 | case 3: 20 | cout << "three\n"; 21 | break; 22 | case 4: 23 | cout << "four\n"; 24 | break; 25 | case 5: 26 | cout << "five\n"; 27 | break; 28 | case 6: 29 | cout << "six\n"; 30 | break; 31 | case 7: 32 | cout << "seven\n"; 33 | break; 34 | case 8: 35 | cout << "eight\n"; 36 | break; 37 | case 9: 38 | cout << "nine\n"; 39 | } 40 | else if(i>9 && i%2==0) 41 | { 42 | cout << "even\n"; 43 | 44 | } 45 | else 46 | { 47 | cout << "odd\n"; 48 | 49 | } 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /shubham/det.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i,j,n,trac=0; 5 | int d,m; 6 | double c; 7 | printf("enter the order of matrix"); 8 | scanf("%d",&n); 9 | int a[n][n]; 10 | printf("enter the elements of n x n matrix"); 11 | for(i=0;i