├── GeeksForGeeks ├── BinaryTree │ ├── README.md │ └── leadNodeLinkList.cpp ├── BST │ ├── addAllGreaterValues.cpp │ ├── searchKey.cpp │ ├── checkIfBstIsLinear.cpp │ └── insertkey.cpp ├── Array │ ├── reverseArray.cpp │ ├── elementWithOddFreq.cpp │ ├── missingNumber.cpp │ ├── binarySearch.cpp │ ├── segregate0and1.cpp │ ├── rotateArrayReversalAlgo.cpp │ ├── leaderInArray.cpp │ ├── ceilingInASortedArray.cpp │ ├── floorInASortedArray.cpp │ ├── equlibriumIndex.cpp │ ├── distinctElementsInWindowK.cpp │ ├── largestSumContiguousSubarray.cpp │ ├── sumOfTwoNumbersInArray.cpp │ ├── freqOfElementsSpecial.cpp │ ├── rotateArray.cpp │ ├── checkConsecutiveElements.cpp │ └── twoElementsWhoseSumIsClosestToZero.cpp └── Stack │ └── reverseString.cpp ├── README.md ├── InterviewBit ├── DynamicProgramming │ ├── UNIQUETREE.cpp │ ├── TRIANGLE.cpp │ ├── STOCKS1.cpp │ ├── JUMP1.cpp │ ├── STAIRS.cpp │ ├── STOCKS2.cpp │ ├── COINCHANGE2.cpp │ ├── ADJACENT.cpp │ ├── MINPATH.cpp │ ├── PalindromePartitioningII.cpp │ ├── JUMP2.cpp │ ├── EDITDISTANCE.cpp │ ├── WordBreak.cpp │ ├── PATHS2.cpp │ ├── DistinctSubequences.cpp │ ├── MaxRectangleInBinaryMatrix.cpp │ ├── LIS.cpp │ ├── ANYTWO.cpp │ ├── MAXPROD.cpp │ └── DECODEWAYS.cpp ├── Greedy │ ├── MICE.cpp │ ├── BULBS.cpp │ ├── MAJORITY.cpp │ ├── GAS.cpp │ └── SEATS.cpp ├── Math │ ├── Gcd.cpp │ ├── Factorial.cpp │ ├── EXCEL1.cpp │ ├── ISPOWER.cpp │ ├── Arrange.cpp │ ├── PRIMESUM.cpp │ ├── EXCEL2.cpp │ ├── PATHS.cpp │ ├── REVINT.cpp │ └── PALINDROME.cpp ├── Bit-Manipulation │ ├── SINGLE.cpp │ ├── Num1Bits.cpp │ ├── REVBITS.cpp │ ├── DifferentBitsSumPairwise.cpp │ └── SINGLE2.cpp ├── Two-Pointers │ ├── INTERSECTARR.cpp │ ├── DIFFK.cpp │ ├── SORTCOLOR.cpp │ ├── MERGE.cpp │ ├── REMELM.cpp │ ├── REMDUP.cpp │ ├── REMDUP2.cpp │ ├── CONTAINER.cpp │ ├── 3SUM.cpp │ └── 3SUMZERO.cpp ├── BinarySearch │ ├── SQRT.cpp │ ├── POW.cpp │ ├── INSERTPOS.cpp │ └── SEARCH2D.cpp ├── Array │ ├── DIAGONAL.cpp │ ├── WAVE.cpp │ ├── NARRAY.cpp │ ├── REACH.cpp │ ├── PASCAL2.cpp │ ├── Positive.cpp │ ├── MAXSUM.cpp │ └── PASCAL1.cpp ├── Trees │ ├── MAXDEPTH.cpp │ ├── INORDER.cpp │ ├── MINDEPTH.cpp │ ├── PREORDER.cpp │ ├── INVERT.cpp │ ├── POSTORDER.cpp │ ├── SAMETREE.cpp │ ├── SYMMETRY.cpp │ ├── KTHSMALLEST2.cpp │ ├── FLATTEN.cpp │ ├── ARRAYBST.cpp │ ├── BALANCED.cpp │ ├── PATH.cpp │ ├── CARTESIAN.cpp │ ├── LCA.cpp │ ├── BINTREE2.cpp │ ├── BINTREE1.cpp │ └── PATH2.cpp ├── Stacks-And-Queues │ ├── NearestSmallerElement.cpp │ ├── BRACES.cpp │ ├── EVALEXP.cpp │ └── RAIN.cpp ├── Backtracking │ ├── PERMUTATIONS.cpp │ ├── PARENTHESIS.cpp │ ├── COMBINATIONS.cpp │ ├── GRAYCODE.cpp │ ├── SUBSET2.cpp │ ├── PERMSEQ.cpp │ ├── PALINPART.cpp │ ├── COMB.cpp │ └── SUBSET.cpp ├── String │ ├── STRSTR.cpp │ ├── ZIGZAG.cpp │ ├── LENGTHLAST.cpp │ ├── REVWORD.cpp │ ├── PALINDROME.cpp │ ├── ROMAN2INT.cpp │ ├── SortedPermutationRank.cpp │ ├── ADDBINARY.cpp │ └── LONGESTPALIN.cpp ├── Hashing │ ├── DIFFK2.cpp │ ├── ANAGRAMS.cpp │ └── REPEATSTR.cpp ├── Graphs │ ├── SumOfFibonacciNumbers.cpp │ └── LevelOrder.cpp └── Link-List │ ├── SWAPL.cpp │ ├── ROTATELIST.cpp │ ├── REMDUPLNK.cpp │ ├── NTHEND.cpp │ ├── REVERSELIST.cpp │ ├── LISTCYCLE.cpp │ ├── LPALIN.cpp │ ├── MERGE.cpp │ └── REORDERLIST.cpp ├── Spoj ├── TEST.java ├── FUNPROB.cpp ├── FAST2.py ├── FACTMULO.py ├── BC.cpp ├── FACTMUL.py ├── FCTRL.cpp ├── EIGHTS.cpp ├── IOIPALIN.cpp ├── AIBOHP.cpp ├── CANDY.cpp ├── MAJOR.cpp ├── MCOINS.cpp ├── FANCY.cpp ├── STAMPS.cpp ├── ABA12C.cpp ├── EC_CONB.cpp ├── DCEPC11B.cpp ├── PRIME1.cpp ├── KURUK14.cpp ├── PCPC12J.cpp ├── ADDREV.cpp ├── NSTEPS.cpp ├── ACPC10D.cpp ├── MAIN72.cpp ├── VFRIENDS.cpp ├── PIR.java ├── TOANDFRO.java ├── MYQ1.cpp ├── PTIME.cpp ├── COMDIV.cpp ├── UPDATEIT.cpp ├── MKMONEY.cpp ├── MAXNUM.cpp ├── CZ_PROB1.cpp └── LASTDIG.cpp ├── HackerEarth ├── CityAndFlood.cpp ├── TerribleChandu.c ├── ChanduAndConsecutiveLetters.c ├── MonkAtTheGraphTheory.cpp ├── ZrZr.cpp ├── UpUp.cpp ├── Monk'sBirthdayParty.cpp ├── MonkAndTheCollosion.c ├── DifDif.cpp ├── MonkInTheLandOfPokemons.c ├── MonkAndChampionsLeague.cpp ├── TheMagicalShop.c ├── Monk'sLoveForFood.cpp ├── DieDie.cpp ├── ArjitAndPrintingPress.cpp ├── MonkAndHisFriends.cpp ├── TheMonkAndTheClassMarks.cpp ├── MonkAndTheMagicalCandyBags.cpp ├── ChanduAndHisInterns.cpp ├── PrateekAndHisFriends.c ├── 4141.cpp ├── Monk'sEncounterWithPolynomial.c ├── LittleJhoolAndHisAddiction.cpp ├── PrefPref.cpp ├── MonkAndMultiplication.cpp ├── ChanduAndHisGirlfriendReturns.c ├── DiscoverTheMonk.c ├── CheckIt.cpp ├── TheConfusedMonk.c ├── MonkAndThetasks.cpp ├── TheOldMonk.c ├── Barcode.cpp ├── TheEnlightedOnes.c ├── GuessTheTriangle.cpp ├── MonkAndTheMatchMaking.cpp ├── MonkInTheSecretServices.cpp ├── MonkAndThePowerOfTime.cpp ├── Leaderboard.cpp ├── OnlyIntegersAllowed.cpp ├── CoprCopr.cpp ├── MonkAndSomeQueries.cpp ├── ChanduAndHisGirlfriend.c ├── CityAndSoilders.cpp ├── MonkAndETF.cpp ├── SamuAndShootingGame.cpp ├── SamuAndSpecialCoprimeNumbers.cpp ├── HasanAndTrip.cpp └── ThreeSum.cpp ├── Algorithms ├── modularExponention.cpp ├── sieveOfEratosthenes.cpp ├── euclidsAlgorithm.cpp ├── towerOfHanoi.cpp ├── permutations.cpp ├── extendedEuclid.cpp ├── kadane'sAlgorithm.cpp ├── quickSort.cpp └── mergeSort.cpp └── Codechef ├── EQUALITY.cpp ├── CHEFST.cpp ├── XORNUBER.cpp ├── CHEFRP.cpp ├── WDTBAM.cpp ├── BHALLALADEVA.cpp ├── TWOSTR.cpp ├── BROKPHON.cpp ├── SUBINC.cpp ├── ASP.cpp ├── MSTEP.cpp ├── DONUTS.cpp ├── CIELNUM2.cpp ├── ADTRI.cpp ├── MANYCHEF.cpp ├── STEM.cpp ├── HELLO.cpp ├── ORACLCS.cpp ├── CODECRCK.cpp ├── PPNUM.cpp ├── BANROB.cpp └── KALAKEYA.cpp /GeeksForGeeks/BinaryTree/README.md: -------------------------------------------------------------------------------- 1 | contains questions and their code implementation on binary trees 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository doesnot contains any open source project files. 2 | It is simply a codebank of various sport programming questions which I solved to get a better hand and understanding of computer science and algorithms. 3 | 4 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/UNIQUETREE.cpp: -------------------------------------------------------------------------------- 1 | int Solution::numTrees(int A) { 2 | int B[A+1],i,j; 3 | for(i=0;i0;j--) 8 | B[i]+=B[j-1]*B[i-j]; 9 | return B[A]; 10 | } 11 | -------------------------------------------------------------------------------- /InterviewBit/Greedy/MICE.cpp: -------------------------------------------------------------------------------- 1 | int Solution::mice(vector &A, vector &B) { 2 | sort(A.begin(),A.end()); 3 | sort(B.begin(),B.end()); 4 | int i,ans=INT_MIN; 5 | for(i=0;i0){ 10 | y=A%B; 11 | A=B; 12 | B=y; 13 | } 14 | return A; 15 | } 16 | -------------------------------------------------------------------------------- /InterviewBit/Math/Factorial.cpp: -------------------------------------------------------------------------------- 1 | /* Given an integer n, return the number of trailing zeroes in n!. */ 2 | int Solution::trailingZeroes(int A) { 3 | int n=5; 4 | int count=0; 5 | while((int)A/n>0){ 6 | count+=(int)A/n; 7 | n=n*5; 8 | } 9 | return count; 10 | } 11 | -------------------------------------------------------------------------------- /Spoj/TEST.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Test 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner c=new Scanner(System.in); 7 | int x=0; 8 | while(x!=42) 9 | { 10 | int i=c.nextInt(); 11 | if(i!=42) 12 | System.out.println(i); 13 | x=i; 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /HackerEarth/CityAndFlood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int N,K,a,b,i; 6 | map mymap; 7 | scanf("%d%d",&N,&K); 8 | while(N--){ 9 | scanf("%d%d",&a,&b); 10 | mymap.erase(b); 11 | } 12 | printf("%d\n",mymap.size()); 13 | } 14 | -------------------------------------------------------------------------------- /HackerEarth/TerribleChandu.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int T; 6 | int i; 7 | char string[31]; 8 | scanf("%d",&T); 9 | while(T--){ 10 | scanf("%s",string); 11 | for(i=strlen(string)-1;i>=0;i--) 12 | printf("%c",string[i]); 13 | printf("\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/TRIANGLE.cpp: -------------------------------------------------------------------------------- 1 | int Solution::minimumTotal(vector > &A) { 2 | int i,j,n=A.size(); 3 | for(i=n-2;i>=0;i--){ 4 | for(j=0;j &A) { 2 | int min_so_far=INT_MAX; 3 | int diff=0; 4 | for(int i=0;iA[i]) 6 | min_so_far=A[i]; 7 | diff=max(diff,A[i]-min_so_far); 8 | } 9 | return diff; 10 | } 11 | -------------------------------------------------------------------------------- /Algorithms/modularExponention.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * complexity O(logb) 3 | */ 4 | long long modularExponention(long long a,long long b,long long m){ 5 | long long x=1,y=a%m; 6 | while(b>0){ 7 | if(b&1){ 8 | x=(x*y); 9 | x%=m; 10 | } 11 | b=b>>1; 12 | y=y*y; 13 | y%=m; 14 | } 15 | return x; 16 | } 17 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/JUMP1.cpp: -------------------------------------------------------------------------------- 1 | int Solution::canJump(vector &A) { 2 | int i,n=A.size(),d=n-1,jump=1; 3 | i=n-2; 4 | while(i>=0){ 5 | jump=d-i; 6 | if(A[i]>=jump){ 7 | d=i; 8 | } 9 | i--; 10 | } 11 | if(d==0) 12 | return 1; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /InterviewBit/Bit-Manipulation/SINGLE.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an array of integers, every element appears twice except for one. Find that single one. 3 | */ 4 | 5 | int Solution::singleNumber(const vector &A) { 6 | int i; 7 | int n=A.size(); 8 | int ans=0; 9 | for(i=0;i 2 | #define MAX 100001 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main(){ 7 | int N,M; 8 | scanf("%d%d",&N,&M); 9 | while(N>0 || M>0){ 10 | double b=(double)(M-N+1)/(M+1); 11 | printf("%0.6lf\n",max(0.0,b)); 12 | scanf("%d%d",&N,&M); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Spoj/FAST2.py: -------------------------------------------------------------------------------- 1 | table =[] 2 | for i in range (0,502): 3 | table.append(-1) 4 | table[0]=1 5 | def po2(n): 6 | if(table[n]!=-1): 7 | return table[n] 8 | table[n]=po2(n-1)<<1 9 | return table[n] 10 | t=int(input()) 11 | for i in range (0, t): 12 | n=int(input()) 13 | print((po2(n+1)-1)%1298074214633706835075030044377087) 14 | -------------------------------------------------------------------------------- /Spoj/FACTMULO.py: -------------------------------------------------------------------------------- 1 | t=input() 2 | while t>0: 3 | count=0 4 | l=[f(i) for f,i in zip((int, int), raw_input().split())] 5 | p=l[0] 6 | n=l[1] 7 | j=p 8 | k=1 9 | while k>0: 10 | k=int(n/j) 11 | count+=j*k*(k-1)/2 12 | s=n-k*j+1 13 | count+=s*k 14 | j=p*j 15 | print count,"\n"; 16 | t-=1 17 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/STAIRS.cpp: -------------------------------------------------------------------------------- 1 | int Solution::climbStairs(int A) { 2 | if(A==1) 3 | return 1; 4 | if(A==2) 5 | return 2; 6 | int p=2; 7 | int q=1; 8 | int i=2; 9 | int ans=0; 10 | while(i 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t,m; 7 | long long a,b,c; 8 | long long n; 9 | scanf("%d",&t); 10 | for(int i=1;i<=t;i++) 11 | { 12 | cin>>a>>b>>c; 13 | n=a*b*c-1; 14 | m=ceil(log2(a))+ceil(log2(b))+ceil(log2(c)); 15 | cout<<"Case #"<=373166: 5 | fact1=0 6 | i=n+1 7 | p=109546051211 8 | 9 | 10 | fact=1 11 | while i<=n: 12 | j=1 13 | fact=fact*i 14 | if fact>p: 15 | fact=fact%p 16 | fact1=fact1*fact 17 | if fact1>p: 18 | fact1=fact1%p 19 | if fact1==0: 20 | break 21 | i+=1 22 | print fact1,"\n" 23 | 24 | -------------------------------------------------------------------------------- /InterviewBit/Bit-Manipulation/Num1Bits.cpp: -------------------------------------------------------------------------------- 1 | /* Write a function that takes an unsigned integer and returns the number of 1 bits it has */ 2 | 3 | int Solution::numSetBits(unsigned int A) { 4 | int count=0; 5 | unsigned int bit=1; 6 | int i; 7 | for(i=0;i<33;i++){ 8 | if(bit & A) 9 | count++; 10 | bit=bit<<1; 11 | } 12 | return count; 13 | } 14 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/STOCKS2.cpp: -------------------------------------------------------------------------------- 1 | int Solution::maxProfit(const vector &A) { 2 | int n=A.size(); 3 | if(n==0) 4 | return 0; 5 | int i; 6 | int investment=A[0]; 7 | int profit=0; 8 | for(i=1;i 2 | 3 | int main() 4 | { 5 | int T; 6 | int i,n; 7 | char string[31]; 8 | scanf("%d",&T); 9 | while(T--){ 10 | scanf("%s",string); 11 | n=strlen(string); 12 | printf("%c",string[0]); 13 | for(i=1;irightChild,sum); 9 | sum+=current->data; 10 | current->data=sum; 11 | addSum(current->leftChild,sum); 12 | } 13 | /* 14 | *int sum=0; 15 | *call addsum(tree.root,sum); 16 | */ 17 | -------------------------------------------------------------------------------- /HackerEarth/MonkAtTheGraphTheory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int N,i; 6 | int degree=0,degree_count=0; 7 | bool flag=1; 8 | scanf("%d",&N); 9 | for(i=0;i>1)==N-1 && flag) 17 | printf("Yes\n"); 18 | else printf("No\n"); 19 | 20 | } -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/COINCHANGE2.cpp: -------------------------------------------------------------------------------- 1 | #define mod 1000007 2 | int Solution::coinchange2(vector &A, int B) { 3 | int table[B+1],n=A.size(); 4 | memset(table,0,sizeof(table)); 5 | table[0]=1; 6 | int i,j; 7 | for(i=0;idata==key) 9 | return current; 10 | else if(current->data > key) 11 | return search(current->leftChild,key); 12 | else return search(current->rightChild,key); 13 | } 14 | -------------------------------------------------------------------------------- /GeeksForGeeks/BinaryTree/leadNodeLinkList.cpp: -------------------------------------------------------------------------------- 1 | void takeinput(Tree &tree){ 2 | int n,i; 3 | IN(n); 4 | tree.pre=new int[n]; 5 | tree.in=new int[n]; 6 | FOR(i,n) 7 | IN(tree.pre[i]); //input preorder traversal 8 | FOR(i,n) 9 | IN(tree.in[i]); //input inorder traversal 10 | i=0; 11 | tree.root=tree.makeTree(n,i,0,n-1); //create tree according to traversals 12 | } 13 | 14 | /* call createLinkList(tree.root,&head); */ 15 | -------------------------------------------------------------------------------- /HackerEarth/ZrZr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | 5 | ll foo(ll N){ 6 | ll ans=0; 7 | ll five=5; 8 | while(N/five > 0){ 9 | ans+=N/five; 10 | five*=5; 11 | } 12 | return ans; 13 | } 14 | int main() 15 | { 16 | int T; 17 | ll N; 18 | scanf("%d",&T); 19 | while(T--){ 20 | scanf("%lld",&N); 21 | printf("%lld\n",foo(N)); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /HackerEarth/UpUp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char c; 7 | char prev=' '; 8 | c=getchar(); 9 | while(c!=EOF){ 10 | 11 | if(prev==' '){ 12 | if(c-'a'>=0 and c-'a'<=25){ 13 | printf("%c",c-'a'+'A'); 14 | } 15 | else printf("%c",c ); 16 | }else printf("%c",c); 17 | prev=c; 18 | c=getchar(); 19 | } 20 | printf("\n"); 21 | } 22 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/ADJACENT.cpp: -------------------------------------------------------------------------------- 1 | int Solution::adjacent(vector > &A) { 2 | int n=A[0].size(); 3 | int B[n+1]; 4 | int i,ans,maxi=0; 5 | for(i=0;i<=n;i++) 6 | B[i]=0; 7 | B[n-1]=max(A[0][n-1],A[1][n-1]); 8 | ans=B[n-1]; 9 | for(i=n-2;i>=0;i--){ 10 | maxi=max(B[i+2],maxi); 11 | B[i]=max(A[0][i],A[1][i])+maxi; 12 | ans=max(ans,B[i]); 13 | } 14 | return ans; 15 | } 16 | -------------------------------------------------------------------------------- /InterviewBit/Bit-Manipulation/REVBITS.cpp: -------------------------------------------------------------------------------- 1 | /* Reverse bits of an 32 bit unsigned integer */ 2 | 3 | unsigned int Solution::reverse(unsigned int A) { 4 | unsigned int msb=1; 5 | unsigned int lsb=1; 6 | unsigned int ans=0; 7 | int i; 8 | for(i=0;i<31;i++) 9 | msb=msb<<1; 10 | for(i=0;i<32;i++){ 11 | if(msb&A) 12 | ans=ans|(lsb); 13 | lsb=lsb<<1; 14 | msb=msb>>1; 15 | } 16 | return ans; 17 | } 18 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/MINPATH.cpp: -------------------------------------------------------------------------------- 1 | int Solution::minPathSum(vector > &A) { 2 | int m=A.size(); 3 | int n=A[0].size(); 4 | int i,j; 5 | vector v(n,0); 6 | v[n-1]=A[m-1][n-1]; 7 | for(i=n-2;i>=0;i--) 8 | v[i]=v[i+1]+A[m-1][i]; 9 | for(i=m-2;i>=0;i--){ 10 | v[n-1]+=A[i][n-1]; 11 | for(j=n-2;j>=0;j--) 12 | v[j]=A[i][j]+min(v[j],v[j+1]); 13 | } 14 | return v[0]; 15 | } 16 | -------------------------------------------------------------------------------- /HackerEarth/Monk'sBirthdayParty.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int T,i,n; 6 | scanf("%d",&T); 7 | set myset; 8 | string s; 9 | while(T--){ 10 | scanf("%d",&n); 11 | for(i=0;i>s; 13 | myset.insert(s); 14 | } 15 | for(auto it=myset.begin();it!=myset.end();it++) 16 | printf("%s\n",(*it).c_str()); 17 | myset.clear(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Spoj/FCTRL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int noOfZeros(int m); 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int i=0; 10 | while(i>m; 14 | cout<0) 24 | { 25 | p=m/pow(5,i); 26 | sum+=p; 27 | i++; 28 | } 29 | return sum; 30 | } 31 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndTheCollosion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int T,N,X[10],i,temp,sum; 6 | scanf("%d",&T); 7 | while(T--){ 8 | sum=0; 9 | scanf("%d",&N); 10 | memset(X,0,sizeof(X)); 11 | for(i=0;i0) 17 | sum+=(X[i]-1); 18 | } 19 | printf("%d\n",sum); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /InterviewBit/Greedy/BULBS.cpp: -------------------------------------------------------------------------------- 1 | int Solution::bulbs(vector &A) { 2 | int n=A.size(); 3 | int i=0,j; 4 | int toggles=0; 5 | bool toggle=0; 6 | while(i 2 | #define IN(L) scanf("%d",&L) 3 | #define mod 1000003 4 | #define MAX 100001 5 | #define ll long long 6 | using namespace std; 7 | 8 | 9 | int main(){ 10 | 11 | string s; 12 | cin>>s; 13 | set se; 14 | for(int i=1;i<=s.length();i++){ 15 | int p=0; 16 | int q=i-1; 17 | while(q 7 | #define IN(a) scanf("%d",&a) 8 | using namespace std; 9 | int gcd(int a,int b){ 10 | int y=1; 11 | while(b>0){ 12 | y=a%b; 13 | a=b; 14 | b=y; 15 | } 16 | return a; 17 | } 18 | int main(){ 19 | int a,b; 20 | IN(a); 21 | IN(b); 22 | printf("%d\n",gcd(a,b)); 23 | } 24 | -------------------------------------------------------------------------------- /HackerEarth/MonkInTheLandOfPokemons.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int T,N,i,A,B,count; 6 | int *H=malloc(sizeof(int)*1000001); 7 | scanf("%d",&T); 8 | while(T--){ 9 | count=0; 10 | scanf("%d",&N); 11 | memset(H,0,1000001*sizeof(int)); 12 | for(i=0;i &A) { 2 | //moores voting algorithm 3 | int i,n=A.size(); 4 | int candidate; 5 | int count=0; 6 | for(i=0;i Solution::intersect(const vector &A, const vector &B) { 3 | int p=0,q=0; 4 | int n=A.size(),m=B.size(); 5 | vector ans; 6 | while(pB[q]) q++; 13 | else p++; 14 | } 15 | return ans; 16 | } 17 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndChampionsLeague.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | priority_queue pq; 7 | int N,M,i,X; 8 | long long ans=0; 9 | 10 | scanf("%d%d",&M,&N); 11 | for(i=0;i &gas, const vector &cost) { 2 | int n=gas.size(),i,j,temp; 3 | int sum=0; 4 | int begin=0; 5 | int tank=0; 6 | for(i=0;i=0) 15 | return begin; 16 | return -1; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /InterviewBit/Math/ISPOWER.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given a positive integer which fits in a 32 bit signed integer, 3 | *find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers. 4 | */ 5 | 6 | bool Solution::isPower(int A) { 7 | int i,j; 8 | if(A==1) 9 | return 1; 10 | for(i=2;i<100000;i++){ 11 | j=i; 12 | while(j<=INT_MAX/i){ 13 | j=j*i; 14 | if(j==A) 15 | return 1; 16 | } 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Codechef/EQUALITY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 50000 3 | #define FOR(a,b) for(a=0;a>1; 12 | sq=mid*mid; 13 | if(sq==B) 14 | return (int)mid; 15 | else if(sq 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string a[4]={"192","442","692","942"}; 7 | int t,z=0; 8 | cin>>t; 9 | while(z>n; 13 | long long i=n/4; 14 | if(n%4==0) 15 | i--; 16 | long long j=n%4; 17 | if(j==0) 18 | j=4; 19 | if(i>0) 20 | cout< 2 | using namespace std; 3 | int main(){ 4 | int T; 5 | scanf("%d",&T); 6 | long long n1,n2,m; 7 | long long mini; 8 | long long ans; 9 | long long sum; 10 | while(T--){ 11 | ans=0; 12 | scanf("%lld%lld%lld",&n1,&n2,&m); 13 | ans=max(n1,n2)-min(n1,n2); 14 | mini=min(n1,n2); 15 | sum=m*(m+1); 16 | sum=sum>>1; 17 | ans+=2*max(mini-sum,(long long)0); 18 | printf("%lld\n",ans); 19 | } 20 | } -------------------------------------------------------------------------------- /InterviewBit/Array/DIAGONAL.cpp: -------------------------------------------------------------------------------- 1 | vector > Solution::diagonal(vector > &A) { 2 | int i=0,j=0,x,y; 3 | int n=A.size(); 4 | vector> v; 5 | vector row; 6 | i=0;j=0; 7 | while(i=0 ){ 10 | row.push_back(A[x][y]); 11 | x++;y--; 12 | } 13 | v.push_back(row); 14 | row.clear(); 15 | if(jleft),findMaxDepth(A->right)); 16 | } 17 | int Solution::maxDepth(TreeNode* A) { 18 | return findMaxDepth(A); 19 | } 20 | -------------------------------------------------------------------------------- /InterviewBit/Array/WAVE.cpp: -------------------------------------------------------------------------------- 1 | /*Given an array of integers, sort the array into a wave like array and return it, 2 | *In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... 3 | */ 4 | void swap(int& a,int &b){ 5 | int c=a; 6 | a=b; 7 | b=c; 8 | } 9 | vector Solution::wave(vector &A) { 10 | int n=A.size(); 11 | sort(A.begin(),A.begin()+n); //sort elements 12 | for(int i=0;i 2 | #include 3 | int main() 4 | { 5 | int n,i; 6 | long long a,b,ans,temp; 7 | char buffer[100001]; 8 | scanf("%lld%lld",&a,&b); 9 | scanf("%s",buffer); 10 | n=strlen(buffer); 11 | if(buffer[0]=='1') 12 | ans=a%b; 13 | else ans=0; 14 | temp=a%b; 15 | for(i=1;i &v,TreeNode *A){ 11 | if(!A) 12 | return; 13 | inorder(v,A->left); 14 | v.push_back(A->val); 15 | inorder(v,A->right); 16 | } 17 | vector Solution::inorderTraversal(TreeNode* A) { 18 | vector v; 19 | inorder(v,A); 20 | return v; 21 | } 22 | -------------------------------------------------------------------------------- /InterviewBit/Stacks-And-Queues/NearestSmallerElement.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::prevSmaller(vector &A) { 2 | stack mystack; 3 | int n=A.size(); 4 | vector v; 5 | mystack.push(-1); 6 | for(int i=0;imystack.top()){ 8 | v.push_back(mystack.top()); 9 | mystack.push(A[i]); 10 | }else{ 11 | while(A[i]<=mystack.top()) mystack.pop(); 12 | v.push_back(mystack.top()); 13 | mystack.push(A[i]); 14 | } 15 | } 16 | return v; 17 | } 18 | -------------------------------------------------------------------------------- /InterviewBit/Trees/MINDEPTH.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | /* find minimum depth of binary Tree */ 11 | int depth(TreeNode *A){ 12 | if(!A) 13 | return INT_MAX; 14 | if(!A->left && !A->right) 15 | return 1; 16 | return 1+min(depth(A->left),depth(A->right)); 17 | } 18 | int Solution::minDepth(TreeNode* A) { 19 | return depth(A); 20 | } 21 | -------------------------------------------------------------------------------- /InterviewBit/Trees/PREORDER.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | void preorder(vector &v,TreeNode *A){ 11 | if(!A) 12 | return; 13 | v.push_back(A->val); 14 | preorder(v,A->left); 15 | preorder(v,A->right); 16 | } 17 | vector Solution::preorderTraversal(TreeNode* A) { 18 | vector v; 19 | preorder(v,A); 20 | return v; 21 | } 22 | -------------------------------------------------------------------------------- /InterviewBit/Two-Pointers/DIFFK.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an array ‘A’ of sorted integers and another non negative integer k, 3 | *find if there exists 2 indices i and j such that A[i] - A[j] = k, i != j. 4 | */ 5 | int Solution::diffPossible(vector &A, int B) { 6 | int n=A.size(); 7 | int p=0,q=0; 8 | while(qB) 14 | p++; 15 | else if(A[q]-A[p]left); 14 | mirror(A->right); 15 | TreeNode *temp; 16 | temp=A->left; 17 | A->left=A->right; 18 | A->right=temp; 19 | } 20 | TreeNode* Solution::invertTree(TreeNode* root) { 21 | mirror(root); 22 | return root; 23 | } 24 | -------------------------------------------------------------------------------- /Codechef/XORNUBER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define FOR(a,b) for(int a=0;a>1; 17 | return A; 18 | }else return -1; 19 | 20 | } 21 | 22 | int main() 23 | { 24 | int T,A; 25 | scanf("%d",&T); 26 | while(T--){ 27 | scanf("%d",&A); 28 | printf("%d\n",xorVal(A)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /HackerEarth/Monk'sLoveForFood.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int Q,type,cost; 8 | stack mystack; 9 | scanf("%d",&Q); 10 | while(Q--){ 11 | scanf("%d",&type); 12 | if(type==1){ 13 | if(mystack.empty()) 14 | printf("No Food\n"); 15 | else { 16 | printf("%d\n",mystack.top()); 17 | mystack.pop(); 18 | } 19 | }else{ 20 | scanf("%d",&cost); 21 | mystack.push(cost); 22 | } 23 | } 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /InterviewBit/Trees/POSTORDER.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | void postorder(vector &v,TreeNode *A){ 11 | if(!A) 12 | return; 13 | postorder(v,A->left); 14 | postorder(v,A->right); 15 | v.push_back(A->val); 16 | } 17 | vector Solution::postorderTraversal(TreeNode* A) { 18 | vector v; 19 | postorder(v,A); 20 | return v; 21 | 22 | } 23 | -------------------------------------------------------------------------------- /HackerEarth/DieDie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define mod 1000000007 3 | #define ll long long 4 | 5 | using namespace std; 6 | 7 | ll power(ll a, ll b){ 8 | ll x=1; 9 | ll y=a%mod; 10 | while(b>0){ 11 | if(b&1){ 12 | x=x*y; 13 | x%=mod; 14 | } 15 | b=b>>1; 16 | y=y*y; 17 | y%=mod; 18 | } 19 | return x; 20 | } 21 | 22 | int main() 23 | { 24 | int t,i; 25 | scanf("%d",&t); 26 | while(t--){ 27 | ll n; 28 | scanf("%lld",&n); 29 | ll deno=power(2,n-1); 30 | ll ans=power(deno,mod-2); 31 | printf("%lld\n",ans ); 32 | } 33 | 34 | 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /GeeksForGeeks/BST/checkIfBstIsLinear.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Time =O(n) & extra Space =O(1) 3 | */ 4 | 5 | /* it is assumed that given array represents bst in pre order so there are no duplicates */ 6 | bool bstCheck(int a[],int n){ 7 | int i; 8 | int current_max=INT_MAX; 9 | int current_min=INT_MIN; 10 | for(i=1;i current_max) //if not in range return false 12 | return false; 13 | if(a[i] v(n+1,INT_MAX); 12 | v[n]=-1; 13 | for(i=n-1;i>=0;i--){ 14 | for(j=i;j 2 | #include 3 | #define FOR(a,b) for(a=0;a 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T,n,m,i,j; 7 | char x[10001]; 8 | char y[10001]; 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%s%s",x,y); 12 | n=strlen(x); 13 | m=strlen(y); 14 | sort(y,y+m); 15 | i=0;j=0; 16 | while(iy[j]){ 18 | printf("%c",y[j]); 19 | j++; 20 | }else printf("%c",x[i]); 21 | i++; 22 | } 23 | printf("\n"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndHisFriends.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,m,T,i; 7 | long long candies; 8 | unordered_map mymap; 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%d%d",&n,&m); 12 | for(i=0;i &Vec) { 12 | int N = Vec.size(); 13 | for(int i = 0; i < N; ++i) { 14 | Vec[i] = Vec[i] + (Vec[Vec[i]]%N) * N; 15 | } 16 | 17 | for(int i = 0; i < N; ++i) { 18 | Vec[i] = Vec[i] / N; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HackerEarth/TheMonkAndTheClassMarks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int i,j,n; 7 | string s; 8 | map mymap[101]; 9 | scanf("%d",&n); 10 | while(n--){ 11 | cin>>s>>i; 12 | if(mymap[i].find(s)!=mymap[i].end()) 13 | mymap[i][s]+=1; 14 | else mymap[i][s]=1; 15 | } 16 | for(i=100;i>0;i--){ 17 | if(!mymap[i].empty()){ 18 | for(auto it=mymap[i].begin();it!=mymap[i].end();it++){ 19 | for(j=0;jfirst)];j++) 20 | printf("%s %d\n",(it->first).c_str(),i); 21 | } 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/JUMP2.cpp: -------------------------------------------------------------------------------- 1 | 2 | int Solution::jump(vector &A) { 3 | int i,n=A.size(); 4 | if(n==1) 5 | return 0; 6 | int steps=1; 7 | int currentMaxReachable=A[0]; 8 | int maxReachable=A[0]; 9 | for(i=0;i<=maxReachable;i++){ 10 | if(i==n-1) 11 | return steps; 12 | currentMaxReachable=max(currentMaxReachable,i+A[i]); 13 | if(i==maxReachable){ 14 | if(currentMaxReachable<=i) 15 | return -1; 16 | maxReachable=currentMaxReachable; 17 | steps++; 18 | } 19 | } 20 | return -1; 21 | } 22 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndTheMagicalCandyBags.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n,T,k; 7 | long long candies; 8 | long long ans; 9 | scanf("%d",&T); 10 | while(T--){ 11 | ans=0; 12 | priority_queue pq; 13 | scanf("%d%d",&n,&k); 14 | while(n--){ 15 | scanf("%lld",&candies); 16 | pq.push(candies); 17 | } 18 | while(k--){ 19 | candies=pq.top(); 20 | ans+=candies; 21 | pq.pop(); 22 | candies=candies>>1; 23 | pq.push(candies); 24 | } 25 | printf("%lld\n",ans); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/EDITDISTANCE.cpp: -------------------------------------------------------------------------------- 1 | int Solution::minDistance(string A, string B) { 2 | int i,j,k; 3 | int n=A.length(); 4 | int m=B.length(); 5 | int M[n+1]; 6 | int del,rep,ins; 7 | for(i=0;i<=n;i++) 8 | M[n-i]=i; 9 | for(i=m-1;i>=0;i--){ 10 | rep=M[n]; 11 | M[n]+=1; 12 | for(j=n-1;j>=0;j--){ 13 | del=M[j+1]; 14 | ins=M[j]; 15 | if(B[i]==A[j]) 16 | M[j]=min(1+del,min(rep,1+ins)); 17 | else M[j]=1+min(del,min(rep,ins)); 18 | rep=ins; 19 | } 20 | } 21 | return M[0]; 22 | } 23 | -------------------------------------------------------------------------------- /HackerEarth/ChanduAndHisInterns.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void sieve(bool *A){ 4 | for(int i=0;i<10000001;i++) 5 | A[i]=true; 6 | A[10000001]=1; 7 | for(int i=2;i<3163;i++) 8 | for(int j=i*i;j<=10000000 && A[i];j+=i) 9 | A[j]=false; 10 | } 11 | int main() 12 | { 13 | int n,x; 14 | bool *A=new bool[10000001]; 15 | sieve(A); 16 | scanf("%d",&n); 17 | while(n--){ 18 | scanf("%d",&x); 19 | if(A[x]) 20 | printf("NO\n"); 21 | else{ 22 | if(((int)sqrt(x))*((int)sqrt(x))==x && A[(int)sqrt(x)]) 23 | printf("NO\n"); 24 | else printf("YES\n"); 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Codechef/WDTBAM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int N,T; 7 | char Q[1000]; 8 | char c; 9 | int W[1001]; 10 | int i,count=0; 11 | scanf("%d",&T); 12 | while(T--){ 13 | count=0; 14 | scanf("%d",&N); 15 | for(i=0;i>Q[i]; 17 | for(i=0;i>c; 19 | if(c==Q[i]) 20 | count++; 21 | } 22 | scanf("%d",&W[0]); 23 | for(i=1;i &A,vector< vector > &v,int l){ 8 | if(l==A.size()-1){ 9 | v.push_back(A); 10 | return; 11 | } 12 | for(int i=l;i > Solution::permute(vector &A) { 19 | vector > v; 20 | permutations(A,v,0); 21 | sort(v.begin(),v.end()); 22 | return v; 23 | } 24 | -------------------------------------------------------------------------------- /Codechef/BHALLALADEVA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define MAX 100000 7 | using namespace std; 8 | int main(){ 9 | int N,Q; 10 | int i,k; 11 | long long arr[MAX]; 12 | long long sum[MAX]; 13 | scanf("%d",&N); 14 | int ans=0; 15 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define FOR(a,b) for(int a=0;a>s1>>s2; 27 | if(check(s1,s2)) 28 | printf("Yes\n"); 29 | else printf("No\n"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/WordBreak.cpp: -------------------------------------------------------------------------------- 1 | int Solution::wordBreak(string A, vector &B) { 2 | int i,j; 3 | int n=A.length(); 4 | 5 | vector v(n+1,0); 6 | v[n]=1; 7 | unordered_map dict; 8 | for(i=0;i=0;i--){ 13 | for(j=i;j 2 | #define FOR(a,b) for(a=0;a &s,string temp,int a,int n){ 3 | if(a>n) 4 | return; 5 | if(a==0){ 6 | while(n--) 7 | temp+=")"; 8 | s.push_back(temp); 9 | return; 10 | } 11 | temp+='('; 12 | paranthesis(s,temp,a-1,n); 13 | temp[temp.length()-1]=')'; 14 | paranthesis(s,temp,a,n-1); 15 | } 16 | vector Solution::generateParenthesis(int A) { 17 | vector s; 18 | string temp=""; 19 | paranthesis(s,temp,A,A); 20 | return s; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Codechef/SUBINC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | long long subarray(int A[],int n){ 3 | int i; 4 | int p=0,q=1; 5 | long long sum=0,count=0; 6 | while(qA[q]){ 8 | sum=q-p; 9 | sum=sum*(sum+1); 10 | sum=sum>>1; 11 | count+=sum; 12 | p=q; 13 | } 14 | q++; 15 | } 16 | sum=q-p; 17 | sum=sum*(sum+1); 18 | sum=sum>>1; 19 | count+=sum; 20 | return count; 21 | } 22 | int main(){ 23 | int N,T; 24 | int i; 25 | scanf("%d",&T); 26 | int A[100000]; 27 | while(T--){ 28 | scanf("%d",&N); 29 | for(i=0;i0){ 17 | if(n&1){ 18 | a=(a*y)%d; 19 | } 20 | y=y*y; 21 | y=y%d; 22 | if(y<0) 23 | y+=d; 24 | n=n>>1; 25 | } 26 | return (int)a; 27 | } 28 | -------------------------------------------------------------------------------- /InterviewBit/Math/PRIMESUM.cpp: -------------------------------------------------------------------------------- 1 | /* Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.*/ 2 | 3 | vector Solution::primesum(int A) { 4 | vector B; 5 | int i,j; 6 | int root=sqrt(A); 7 | bool *array=new bool[A+1]; 8 | for(i=0;i<=A;i++) 9 | array[i]=0; 10 | for(i=2;i<=root;i++){ 11 | if(array[i]==0) 12 | for(j=i;j<=A/i;j++) 13 | array[i*j]=1; 14 | } 15 | for(i=2;i 2 | int canInvite(long long *A,long long x,int n){ 3 | int p=0,q=0; 4 | while(qx) p++; 8 | else q++; 9 | } 10 | return 0; 11 | } 12 | int main() 13 | { 14 | int i,n,T; 15 | long long x; 16 | long long *A=malloc(sizeof(long long)*1000001); 17 | scanf("%d",&T); 18 | while(T--){ 19 | scanf("%d",&n); 20 | scanf("%lld",&x); 21 | A[0]=0; 22 | for(i=1;i<=n;i++){ 23 | scanf("%lld",&A[i]); 24 | A[i]+=A[i-1]; 25 | } 26 | if(canInvite(A,x,n+1)) 27 | printf("YES\n"); 28 | else printf("NO\n"); 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /InterviewBit/Bit-Manipulation/DifferentBitsSumPairwise.cpp: -------------------------------------------------------------------------------- 1 | #define mod 1000000007 2 | int Solution::cntBits(vector &A) { 3 | long long even,odd; 4 | long long ans=0; 5 | int n=A.size(); 6 | int i,j,allzero=0; 7 | while(allzero>1; 17 | } 18 | ans+=(odd*even); 19 | ans%=mod; 20 | } 21 | ans%=mod; 22 | ans=(ans<<1); 23 | ans%=mod; 24 | return (int)ans; 25 | } 26 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/PATHS2.cpp: -------------------------------------------------------------------------------- 1 | int Solution::uniquePathsWithObstacles(vector > &A) { 2 | int m=A.size(); 3 | int n=A[0].size(); 4 | int i,j; 5 | vector v(n,0); 6 | if(A[m-1][n-1]==1) 7 | return 0; 8 | v[n-1]=1; 9 | for(i=n-2;i>=0;i--){ 10 | if(A[m-1][i]==1) 11 | v[i]=0; 12 | else v[i]=v[i+1]; 13 | } 14 | for(i=m-2;i>=0;i--){ 15 | if(A[i][n-1]==1) 16 | v[n-1]=0; 17 | for(j=n-2;j>=0;j--){ 18 | if(A[i][j]==1) 19 | v[j]=0; 20 | else v[j]+=v[j+1]; 21 | 22 | } 23 | } 24 | return v[0]; 25 | } 26 | -------------------------------------------------------------------------------- /HackerEarth/4141.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define mod 1000003 3 | #define mod2 1000000007 4 | #define ll long long 5 | #define max 1000001 6 | #define base 101 7 | using namespace std; 8 | 9 | 10 | int main() 11 | { 12 | int T,i; 13 | scanf("%d",&T); 14 | while(T--){ 15 | int a,b,c,n; 16 | scanf("%d%d%d%d",&a,&b,&c,&n); 17 | int prev=(a*10+b)%41; 18 | if(n==1) 19 | prev=a; 20 | int next; 21 | for(i=2;i 2 | int expression(long long A,long long B,long long C,long long K,long long x0){ 3 | long long val=A*x0*x0; 4 | val+=(B*x0); 5 | val+=C; 6 | val-=K; 7 | if(val >= 0) 8 | return 1; 9 | return 0; 10 | } 11 | int main() 12 | { 13 | int T; 14 | long long A,B,C,K,x0; 15 | long long l,r,mid; 16 | scanf("%d",&T); 17 | while(T--){ 18 | scanf("%lld%lld%lld%lld",&A,&B,&C,&K); 19 | l=0; r=100000; 20 | while(l<=r){ 21 | mid=(l+r)>>1; 22 | if(expression(A,B,C,K,mid)){ 23 | x0=mid; 24 | r=mid-1; 25 | }else l=mid+1; 26 | } 27 | printf("%lld\n",x0); 28 | 29 | } 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /InterviewBit/Two-Pointers/SORTCOLOR.cpp: -------------------------------------------------------------------------------- 1 | /* Given an array of 0 1 and 2 sort it */ 2 | void swap(int &a,int &b){ 3 | int c=a; 4 | a=b; 5 | b=c; 6 | } 7 | int twopointers(vector &A,int b,int n){ 8 | int p=0,q=n-1; 9 | while(p &A) { 24 | int n=A.size(); 25 | int one=twopointers(A,2,n); 26 | twopointers(A,1,one); 27 | } 28 | -------------------------------------------------------------------------------- /InterviewBit/String/STRSTR.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *strstr - locate a substring ( needle ) in a string ( haystack ) 3 | *Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 4 | */ 5 | 6 | int Solution::strStr(const string &haystack, const string &needle) { 7 | 8 | int n=haystack.length(); 9 | int z=needle.length(); 10 | if(n==0 || z==0) 11 | return -1; 12 | int i,j; 13 | for(i=0;ival==B->val) 19 | return (this->isSameTree(A->left,B->left))& 20 | (this->isSameTree(A->right,B->right)); 21 | else return 0; 22 | } 23 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/reverseArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write a program to reverse an array 3 | *Time=O(n) space O(1) 4 | */ 5 | #include 6 | #define IN(a) scanf("%d",&a) 7 | #define FOR(a,b) for(a=0;a 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int T; 7 | int *a=new int[1000000]; 8 | int N,i,K; 9 | scanf("%d",&T); 10 | while(T--){ 11 | scanf("%d%d",&N,&K); 12 | for(i=0;i>1; 19 | sort(a,a+N); 20 | printf("%d\n",a[N-1]-a[0]); 21 | if(a[N-1]-a[0]K) 24 | printf("No more girlfriends!\n"); 25 | else printf("Lucky chap!\n"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Codechef/ASP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,T,i,count,l; 5 | bool flag; 6 | int *A=new int[1000000]; 7 | scanf("%d",&T); 8 | while(T--){ 9 | count=0; 10 | flag=1; 11 | scanf("%d",&n); 12 | for(i=0;i 2 | using namespace std; 3 | int min(int a,int b){ 4 | if(a &A, int B) { 6 | unordered_map mymap; 7 | int k=B; 8 | for(int i=0;ival != B->val) 19 | return 0; 20 | return (symetric(A->left,B->right)&symetric(A->right,B->left)); 21 | 22 | } 23 | int Solution::isSymmetric(TreeNode* A) { 24 | return symetric(A,A); 25 | } 26 | -------------------------------------------------------------------------------- /InterviewBit/Graphs/SumOfFibonacciNumbers.cpp: -------------------------------------------------------------------------------- 1 | #define ll long long 2 | int Solution::fibsum(int A) { 3 | vector fibo; 4 | fibo.push_back(1); 5 | fibo.push_back(1); 6 | fibo[1]=1; 7 | fibo[2]=1; 8 | ll prev=1,last_prev=1; 9 | ll new_ppl=2; 10 | while(new_ppl0){ 21 | if(fibo[i]>B) 22 | i--; 23 | else{ 24 | B=B-fibo[i]; 25 | steps++; 26 | } 27 | } 28 | return steps; 29 | } 30 | -------------------------------------------------------------------------------- /Codechef/MSTEP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define FOR(i,n) for(i=0;i 2 | using namespace std; 3 | 4 | bool isSubsequence(string s,string t){ 5 | int k=0; 6 | for(int i=0;i>s; 19 | cin>>t; 20 | int n=t.length(); 21 | int l=1,r=s.length(); 22 | int ans=0; 23 | while(l<=r){ 24 | int mid=(l+r)>>1; 25 | //cout< 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b) for(a=0;a mystack; 13 | int i=0; //loop variable 14 | while(buffer[i]!='\0'){ 15 | mystack.push(buffer[i]); 16 | i++; 17 | } 18 | i=0; 19 | while(!mystack.empty()){ 20 | buffer[i]=mystack.top(); 21 | mystack.pop(); 22 | i++; 23 | } 24 | buffer[i]='\0'; 25 | } 26 | int main(){ 27 | char buffer[101]; 28 | scanf("%s",buffer); 29 | reverse(buffer); 30 | printf("%s\n",buffer); 31 | } 32 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndMultiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | priority_queue pq; 7 | int N,i,t1,t2,t3; 8 | 9 | scanf("%d",&N); 10 | int a[N]; 11 | for(i=0;i &A, int B) { 13 | int n=A.size(); 14 | int l=0,r=n-1; 15 | int mid; 16 | while(l<=r){ 17 | mid=(l+r)>>1; 18 | if(A[mid]==B) 19 | return mid; 20 | else if(A[mid] 2 | 3 | int main() 4 | { 5 | int T,m,n,i,p,q; 6 | int A[500000]; 7 | int B[500000]; 8 | scanf("%d",&T); 9 | while(T--){ 10 | scanf("%d%d",&m,&n); 11 | for(i=0;iB[q]){ 18 | printf("%d ",A[p]); 19 | p++; 20 | }else{ 21 | printf("%d ",B[q]); 22 | q++; 23 | } 24 | } 25 | while(pleft,k,smallest,i); 14 | if(l) 15 | return 1; 16 | i=i+1; 17 | smallest=t->val; 18 | if(i==k) 19 | return 1; 20 | return kth(t->right,k,smallest,i); 21 | 22 | 23 | } 24 | int Solution::kthsmallest(TreeNode* root, int k) { 25 | int i=0; 26 | int smallest=0; 27 | kth(root,k,smallest,i); 28 | return smallest; 29 | } 30 | -------------------------------------------------------------------------------- /Algorithms/towerOfHanoi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Implement tower of hanoi 3 | *Time=O(2^n) & space =O(1) 4 | */ 5 | #include 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b) for(a=0;a A 7 | *2 -> B 8 | *3 -> C 9 | ... 10 | *26 -> Z 11 | *27 -> AA 12 | *28 -> AB 13 | */ 14 | string Solution::convertToTitle(int A) { 15 | A; 16 | int maxPower=1; 17 | string s; 18 | int character; 19 | while(A >0){ 20 | character=(A-1)%26; 21 | A=A-character-1; 22 | A/=26; 23 | character+=65; 24 | s=s+(char)character; 25 | } 26 | char c; 27 | int n=s.length(); 28 | for(int i=0;i 2 | int present(int A[],int n,int K){ 3 | int l=0,r=n-1,mid; 4 | while(l<=r){ 5 | mid=(l+r)>>1; 6 | if(A[mid]==K) 7 | return 1; 8 | else if(A[mid]>K) 9 | r=mid-1; 10 | else l=mid+1; 11 | } 12 | return 0; 13 | } 14 | int comp(const void *A, const void *B){ 15 | return (*(int*)A- *(int*)B); 16 | } 17 | int main() 18 | { 19 | int N,Q,K,i; 20 | int A[100000]; 21 | scanf("%d%d",&N,&Q); 22 | for(i=0;i > Solution::anagrams(const vector &A) { 5 | int n=A.size(); 6 | int i; 7 | vector> v; 8 | vector B; 9 | unordered_map> mymap; 10 | string temp; 11 | for(i=0;isecond); 22 | it++; 23 | } 24 | return v; 25 | } 26 | -------------------------------------------------------------------------------- /InterviewBit/Trees/FLATTEN.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | void flat(TreeNode* A,TreeNode **previous){ 11 | if(!A) 12 | return; 13 | TreeNode *R=A->right; 14 | if(*previous){ 15 | (*previous)->right=A; 16 | (*previous)->left=NULL; 17 | } 18 | *previous=A; 19 | flat(A->left,previous); 20 | flat(R,previous); 21 | 22 | 23 | } 24 | TreeNode* Solution::flatten(TreeNode* A) { 25 | if(!A) 26 | return A; 27 | TreeNode *current=NULL; 28 | flat(A,¤t); 29 | return A; 30 | } 31 | -------------------------------------------------------------------------------- /Codechef/DONUTS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 20001 5 | #define FOR(i,a,n) for(i=a;i0){ 12 | a[p]-=1; 13 | a[q-1]+=a[q]; 14 | a[q-1]+=1; 15 | q--; 16 | b++; 17 | }else p++; 18 | } 19 | return b; 20 | } 21 | int main(){ 22 | int i; 23 | int T,N,M,b; 24 | scanf("%d",&T); 25 | while(T--){ 26 | scanf("%d%d",&N,&M); 27 | FOR(i,0,M) 28 | scanf("%d",&a[i]); 29 | if(N==M) 30 | printf("%d\n",M/2); 31 | else{ 32 | sort(a,a+M); 33 | printf("%d\n",foo(M)); 34 | } 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Spoj/AIBOHP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int *a=new int[6101]; 5 | int *b=new int[6101]; 6 | int min(int a,int b){ 7 | if(a>s; 39 | printf("%d\n",palindrome(s,s.length())); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /InterviewBit/Array/NARRAY.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::repeatedNumber(const vector &A) { 2 | // Do not write main() function. 3 | // Do not read input, instead use the arguments to the function. 4 | // Do not print the output, instead return values as specified 5 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 6 | vector v(2,0); 7 | int n=A.size(); 8 | long long sum=0,sqsum=0; 9 | int i; 10 | for(i=0;i>1; 18 | v[1]=(int)(sqsum-v[0]); 19 | return v; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /InterviewBit/String/LENGTHLAST.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given a string s consists of upper/lower-case alphabets and empty space characters ' ', 3 | *return the length of last word in the string. 4 | *If the last word does not exist, return 0. 5 | */ 6 | int Solution::lengthOfLastWord(const string &A) { 7 | int n=A.length(); 8 | int i; 9 | int current_length=0; 10 | int last_length=0; 11 | for(i=0;i0) 16 | last_length=current_length; 17 | current_length=0; 18 | } 19 | } 20 | if(current_length >0) 21 | last_length=current_length; 22 | return last_length; 23 | } 24 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/DistinctSubequences.cpp: -------------------------------------------------------------------------------- 1 | int Solution::numDistinct(string S, string T) { 2 | int i,j,k; 3 | int n=T.length(); 4 | i=S.length()-1; 5 | while(S[i]!=T[n-1] && i>=0) i--; 6 | if(i<0) 7 | return 0; 8 | S=S.substr(0,i+1); 9 | int m=S.length(); 10 | int A[m][n]; 11 | memset(A,0,sizeof(A)); 12 | A[m-1][n-1]=1; 13 | for(i=m-2;i>=0;i--){ 14 | k=m-i; 15 | for(j=n-1;j>=0 && k;j--){ 16 | if(S[i]==T[j]){ 17 | if(j==n-1) 18 | A[i][j]=1+A[i+1][j]; 19 | else A[i][j]=A[i+1][j+1]+A[i+1][j]; 20 | } 21 | else A[i][j]=A[i+1][j]; 22 | k--; 23 | } 24 | } 25 | return A[0][0]; 26 | } 27 | -------------------------------------------------------------------------------- /Spoj/CANDY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int p=1; 7 | while(p==1){ 8 | int n; 9 | cin>>n; 10 | if(n==-1) 11 | break; 12 | int *a=new int[n]; 13 | for(int i=0;i>a[i]; 15 | int avg=0; 16 | for(int i=0;i0) 28 | sum+=avg-a[i]; 29 | else sum+=a[i]-avg; 30 | } 31 | cout<next; 13 | while(pivot2){ 14 | pivot1->next=pivot2->next; 15 | pivot2->next=pivot1; 16 | if(previous) 17 | previous->next=pivot2; 18 | else A=pivot2; 19 | previous=pivot1; 20 | pivot1=pivot1->next; 21 | if(pivot1) 22 | pivot2=pivot1->next; 23 | else pivot2=NULL; 24 | 25 | } 26 | return A; 27 | } 28 | -------------------------------------------------------------------------------- /InterviewBit/String/REVWORD.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an input string, reverse the string word by word. 3 | *Given s = "the sky is blue", 4 | *return "blue is sky the". 5 | */ 6 | void Solution::reverseWords(string &A) { 7 | stack mystack; 8 | string temp=""; 9 | int n=A.length(); 10 | int i; 11 | for(i=0;i 2 | #define N 2001 3 | #define maxN 1000000 4 | using namespace std; 5 | int max(int a,int b){ 6 | if(a>b) 7 | return a; 8 | return b; 9 | } 10 | int freq[N]; 11 | int main(){ 12 | int t,n,j,i,temp,maxfreq,maxNo=-10000,maxfreqno=-10000; 13 | for(i=0;i<2001;i++) 14 | freq[i]=0; 15 | scanf("%d",&t); 16 | while(t--){ 17 | scanf("%d",&n); 18 | maxfreq=0; 19 | maxfreqno=0; 20 | for(i=0;i<=2000;i++) 21 | freq[i]=0; 22 | for(i=0;imaxfreq){ 27 | maxfreq=freq[temp]; 28 | maxfreqno=temp-1000; 29 | } 30 | } 31 | if(maxfreq>(int)n/2) 32 | printf("YES %d\n",maxfreqno); 33 | else printf("NO\n"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Spoj/MCOINS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 3000 3 | #define mod 100000007 4 | #define ll long long 5 | #define ini(x) scanf("%d",&x); 6 | #define inl(x) scanf("%lld",&x); 7 | #define ind(x) scanf("%lf",&x); 8 | #define type double 9 | using namespace std; 10 | 11 | int main(){ 12 | int i,l,k; 13 | int n; 14 | ini(k);ini(l);ini(n); 15 | int a[50]; 16 | for(i=0;i=0 && arr[i-k]==0) or ((i-l)>=0 && arr[i-l]==0) ) 23 | arr[i]=1; 24 | else arr[i]=0; 25 | } 26 | for(i=0;i &A,int i,int j){ 12 | if(i>j) 13 | return NULL; 14 | int mid=(i+j)>>1; 15 | TreeNode *treenode=new TreeNode(A[mid]); 16 | if(i==j) 17 | return treenode; 18 | treenode->left=createTree(A,i,mid-1); 19 | treenode->right=createTree(A,mid+1,j); 20 | return treenode; 21 | } 22 | TreeNode* Solution::sortedArrayToBST(const vector &A) { 23 | return createTree(A,0,A.size()-1); 24 | } 25 | -------------------------------------------------------------------------------- /HackerEarth/CheckIt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N,M,T,i,x; 7 | bool flag; 8 | int a[40000]; 9 | int b[40000]; 10 | bool c[5000]; 11 | scanf("%d",&T); 12 | while(T--){ 13 | 14 | scanf("%d%d",&N,&M); 15 | flag=true; 16 | for(i=0;i &X, vector &Y) { 7 | int current_x,current_y; 8 | int n=X.size(); 9 | int i; 10 | int steps=0; 11 | if(n==0) 12 | return steps; 13 | current_x=X[0]; 14 | current_y=Y[0]; 15 | for(i=1;i 2 | long long gcd(long long A,long long B){ 3 | if(B==0) 4 | return A; 5 | return gcd(B,A%B); 6 | } 7 | long long modExp(long long a,long long b,long long c){ 8 | long long ans=1; 9 | while(b>0){ 10 | if(b&1){ 11 | ans*=a; 12 | ans%=c; 13 | } 14 | a*=a; 15 | a%=c; 16 | b=b>>1; 17 | } 18 | return ans; 19 | } 20 | int main() 21 | { 22 | int n,i; 23 | long long A[50]; 24 | long long fx=1,gx=1,c=1000000007; 25 | scanf("%d",&n); 26 | for(i=0;i 2 | #include 3 | #define FOR(a,b) for(a=0;a0) 8 | return 0; 9 | if(a[8]>=a[5] && a[5]>=a[3]) 10 | return 1; 11 | return 0; 12 | } 13 | int main(){ 14 | int i; 15 | int d; 16 | char c; 17 | int a[10]={0}; 18 | int N,count=0; 19 | scanf("%d",&N); 20 | getchar(); 21 | while(N--){ 22 | i=0; 23 | c=getchar(); 24 | while((c=getchar())!='\n'){ 25 | d=c-'0'; 26 | if(d<10 && d>=0) 27 | a[d]+=1; 28 | else if(c==' ') 29 | memset(a,0,sizeof(a)); 30 | } 31 | if(isCeil(a)) 32 | count++; 33 | memset(a,0,sizeof(a)); 34 | } 35 | printf("%d\n",count ); 36 | } 37 | -------------------------------------------------------------------------------- /InterviewBit/Link-List/ROTATELIST.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 | /*Given a list, rotate the list to the right by k places, where k is non-negative.*/ 11 | ListNode* Solution::rotateRight(ListNode* A, int B) { 12 | int n=1; 13 | ListNode *tail=A; 14 | ListNode *current=A; 15 | while(tail->next){ 16 | n++; 17 | tail=tail->next; 18 | } 19 | if(B>=n) 20 | B%=n; 21 | if(B==0) 22 | return A; 23 | n=n-B-1; 24 | while(n--) 25 | current=current->next; 26 | tail->next=A; 27 | A=current->next; 28 | current->next=NULL; 29 | return A; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Spoj/FANCY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | string s; 12 | cin>>s; 13 | int *a1=new int[s.length()]; 14 | int j=0; 15 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t,n,no,count; 7 | int a[1000]; 8 | scanf("%d",&t); 9 | for(int m=1;m<=t;m++) 10 | { 11 | count=0; 12 | scanf("%d",&no); 13 | scanf("%d",&n); 14 | for(int i=0;i=0;i--) 20 | { 21 | count+=a[i]; 22 | if(count>=no) 23 | { 24 | flag=true; 25 | j=n-i; 26 | break; 27 | } 28 | } 29 | if(flag) 30 | { 31 | cout<<"Scenario #"< &A, vector &B) { 6 | vector C; 7 | int n=A.size(); 8 | int m=B.size(); 9 | int p=0,q=0; 10 | for(p=0;pleft) && !(A->right)) 17 | return 1; 18 | int lh=1+height(A->left); 19 | int rh=1+height(A->right); 20 | if(lh<0 || rh<0) 21 | return INT_MIN; 22 | if(abs(lh-rh)>1) 23 | return INT_MIN; 24 | return max(lh,rh); 25 | 26 | } 27 | int Solution::isBalanced(TreeNode* A) { 28 | if(height(A)<0) 29 | return 0; 30 | return 1; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Algorithms/permutations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Generate All possible ppermutations of a string 3 | */ 4 | 5 | /* utility function that swaps two characters */ 6 | void swap(char &a,char &b){ 7 | char c=a; 8 | a=b; 9 | b=c; 10 | } 11 | /* generates all possible permutations of the string */ 12 | void generatePermutations(string &s,vector &v,int l){ 13 | if(l==s.length()-1){ 14 | v.push_back(s); 15 | return; 16 | } 17 | for(int i=l;i permute(string s){ 25 | vector v; 26 | generatePermutations(s,v,0); 27 | for(int i=0;i 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;adata=key; 4 | return node; 5 | } 6 | /* 7 | *first it searches for the place node should be if its not there than inserts it there 8 | *New node inserted is always a leaf 9 | *uses binary tree property to search key if not found returns null 10 | *Time =O(n) in worst case & no extra space 11 | */ 12 | bool insert(Node *current,int key){ 13 | if(!current) 14 | return 0; 15 | if(current->data==key) 16 | return true; 17 | else if(current->data > key){ 18 | if(!insert(current->leftChild,key)) 19 | current->leftChild=createNode(key); 20 | } 21 | else if(current->data rightChild,key)) 23 | current->rightChild=createNode(key); 24 | } 25 | return true; 26 | } 27 | -------------------------------------------------------------------------------- /InterviewBit/Link-List/REMDUPLNK.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 | /* 11 | *Given a sorted linked list, delete all duplicates such that each element appear only once. 12 | */ 13 | ListNode* Solution::deleteDuplicates(ListNode* A) { 14 | ListNode *current=A; 15 | ListNode *fwd=A->next; 16 | while(fwd){ 17 | if(current->val==fwd->val){ 18 | current->next=fwd->next; 19 | fwd->next=NULL; 20 | free(fwd); 21 | fwd=current->next; 22 | } 23 | else{ 24 | current=current->next; 25 | fwd=fwd->next; 26 | } 27 | } 28 | return A; 29 | } 30 | -------------------------------------------------------------------------------- /InterviewBit/Two-Pointers/REMELM.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an array and a value, remove all the instances of that value in the array. 3 | *Also return the number of elements left in the array after the operation. 4 | *It does not matter what is left beyond the expected length. 5 | * If array A is [4, 1, 1, 2, 1, 3] 6 | *and value elem is 1, 7 | *then new length is 3, and A is now [4, 2, 3] 8 | */ 9 | int Solution::removeElement(vector &A, int B) { 10 | int n=A.size(); 11 | int i; 12 | i=0; 13 | while(i 2 | #define MAX 100009 3 | #define mod 1000000007 4 | #define ll long long 5 | #define ini(x) scanf("%d",&x); 6 | #define inl(x) scanf("%lld",&x); 7 | #define type double 8 | using namespace std; 9 | 10 | ll K[101]; 11 | ll dp[101]; 12 | ll const inf=(ll)1<<60; 13 | int main(){ 14 | int T,i,j; 15 | int n,k; 16 | ini(T); 17 | while(T--){ 18 | ini(n);ini(k); 19 | for(i=1;i<=k;i++){ 20 | inl(K[i]); 21 | if(K[i]==-1) 22 | K[i]=inf; 23 | } 24 | K[0]=inf; 25 | 26 | dp[0]=0; 27 | for(i=1;i<=k;i++){ 28 | dp[i]=inf; 29 | for(j=1;j<=i;j++){ 30 | dp[i]=min(dp[i],K[j]+dp[i-j]); 31 | } 32 | } 33 | ll ans=dp[k]; 34 | if(ans==inf) 35 | ans=-1; 36 | printf("%lld\n",ans); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /InterviewBit/Math/PATHS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). 3 | *How many possible unique paths are there? 4 | *Note: A and B will be such that the resulting answer fits in a 32 bit signed integer. 5 | */ 6 | 7 | /*** Ans= (A+B-2)!/[(A-1)! *(B-1)!]**/ 8 | int Solution::uniquePaths(int A, int B) { 9 | int bigger=A-1; 10 | int smaller=B-1; 11 | if(B>A){ 12 | bigger=B-1; 13 | smaller=A-1; 14 | } 15 | if(smaller==0) 16 | return 1; 17 | long long ans=1; 18 | int i; 19 | for(i=0;ival; 18 | if(!A->left && !A->right){ 19 | if(sum==B) 20 | return 1; 21 | return 0; 22 | } 23 | return (isSum(A->left,sum,B)|isSum(A->right,sum,B)); 24 | 25 | } 26 | int Solution::hasPathSum(TreeNode* A, int B) { 27 | return isSum(A,0,B); 28 | } 29 | -------------------------------------------------------------------------------- /Codechef/ADTRI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define MAX 5000000 7 | using namespace std; 8 | bool *pythagoren=new bool[MAX+1]; 9 | void preprocess(){ 10 | int i,j,k; 11 | for(i=0;i<=MAX;i++) 12 | pythagoren[i]=0; 13 | for(i=1;i<2240;i++){ 14 | for(j=i+1;j<2240;j++){ 15 | k=i*i+j*j; 16 | if(k <= MAX) 17 | pythagoren[k]=1; 18 | } 19 | } 20 | for(i=1;i<=MAX;i++){ 21 | if(pythagoren[i]){ 22 | for(j=2;i*j<=MAX;j++) 23 | pythagoren[j*i]=1; 24 | } 25 | } 26 | } 27 | int main(){ 28 | int T,N; 29 | preprocess(); 30 | scanf("%d",&T); 31 | while(T--){ 32 | scanf("%d",&N); 33 | if(pythagoren[N]) 34 | printf("YES\n"); 35 | else printf("NO\n"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndThetasks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ones(long long A){ 5 | int count=0; 6 | while(A>0){ 7 | if(A&1) 8 | count++; 9 | A=A>>1; 10 | } 11 | return count; 12 | } 13 | 14 | int main() 15 | { 16 | int T,N,i,j; 17 | long long A; 18 | scanf("%d",&T); 19 | vector< vector >v; 20 | vector row; 21 | for(i=0;i<64;i++) 22 | v.push_back(row); 23 | 24 | while(T--){ 25 | scanf("%d",&N); 26 | for(i=0;i 2 | #define MAX 100000 3 | #define FOR(i,n) for(i=0;ib) 8 | return a; 9 | return b; 10 | } 11 | int monkiness(ll B[],ll k,int n){ 12 | int m=-1; 13 | int l=0,r=n-1,mid; 14 | while(l<=r){ 15 | mid=(l+r)>>1; 16 | if(k<=B[mid]){ 17 | m=mid; 18 | l=mid+1; 19 | }else r=mid-1; 20 | } 21 | return m; 22 | } 23 | int main() 24 | { 25 | int i,T,n,m=0; 26 | ll A[MAX]; 27 | ll B[MAX]; 28 | scanf("%d",&T); 29 | while(T--){ 30 | m=0; 31 | IN(n); 32 | FOR(i,n) 33 | IN(A[i]); 34 | FOR(i,n) 35 | IN(B[i]); 36 | FOR(i,n) 37 | m=max(m,monkiness(B,A[i],n)-i); 38 | printf("%d\n",m); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/missingNumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *You are given a list of n-1 integers and these integers are in the range of 1 to n. 3 | *There are no duplicates in list. 4 | *One of the integers is missing in the list. Write an efficient code to find the missing integer 5 | *time=O(n) space=O(1) no extra space 6 | *uses bitwise xor 7 | */ 8 | #include 9 | #define IN(a) scanf("%d",&a) 10 | #define FOR(a,b) for(a=0;a> &v,vector &row,int i,int n,int k){ 8 | if(k==0){ 9 | v.push_back(row); 10 | return; 11 | } 12 | if(i==n) 13 | return; 14 | for(int j=i;j > Solution::combine(int n, int k) { 21 | vector> v; 22 | vector row; 23 | backTrack(v,row,0,n,k); 24 | return v; 25 | } 26 | -------------------------------------------------------------------------------- /InterviewBit/Math/REVINT.cpp: -------------------------------------------------------------------------------- 1 | /* Reverse a Given int if the result overflows return 0 */ 2 | 3 | int Solution::reverse(int A) { 4 | bool negative=0; 5 | if(A<0){ 6 | negative=1; 7 | A=A*(-1); 8 | } 9 | int tenPow[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; 10 | int i=0; 11 | int noOfDigits=0; 12 | while(i<10 && A/tenPow[i]>0) i++; 13 | noOfDigits=i; 14 | if(noOfDigits==1){ 15 | if(negative) 16 | A*=-1; 17 | return A; 18 | } 19 | long long B=A, C=0,temp; 20 | for(i=0;iINT_MAX || C< INT_MIN) 28 | return 0; 29 | return (int)C; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /HackerEarth/Barcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define IN(L) scanf("%d",&L) 3 | #define mod 1000000007 4 | using namespace std; 5 | 6 | int main(){ 7 | int N,K,i,j; 8 | int brushes[1001]; 9 | IN(N);IN(K); 10 | for(i=0;i 2 | #define FOR(i,n) for(i=0;i>1; 31 | if(ispossible(A,n,mid,k)){ 32 | val=mid; 33 | r=mid-1; 34 | }else l=mid+1; 35 | } 36 | printf("%d\n",val); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /InterviewBit/Array/PASCAL2.cpp: -------------------------------------------------------------------------------- 1 | /*Given an index k, return the kth row of the Pascal’s triangle. 2 | 3 | *Pascal’s triangle : To generate A[C] in row R, sum up A’[C] and A’[C-1] from previous row R - 1. 4 | */ 5 | 6 | 7 | vector Solution::getRow(int A) { 8 | // Do not write main() function. 9 | // Do not read input, instead use the arguments to the function. 10 | // Do not print the output, instead return values as specified 11 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 12 | vector v(A+1,0); 13 | v[0]=1; 14 | int pre1,previous; 15 | for(int i=1;i 2 | using namespace std; 3 | #define slang "FEHC" 4 | using namespace std; 5 | void swap(char &a,char &b){ 6 | char c=a; 7 | a=b; 8 | b=c; 9 | } 10 | void manyChefs(string &s){ 11 | int i,j; 12 | int n=s.length(); 13 | for(i=0;i>T; 31 | while(T--){ 32 | cin>>s; 33 | manyChefs(s); 34 | cout< 2 | using namespace std; 3 | 4 | struct Point{ 5 | float x; 6 | float y; 7 | }; 8 | 9 | bool comp(Point p1, Point p2){ 10 | if(p1.x p2.x) 13 | return 0; 14 | else{ 15 | if(p1.y 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | map< int, map > > > mymap; 7 | int T,A,B,C,D,Q; 8 | string s,p,q; 9 | cin>>s; 10 | scanf("%d",&Q); 11 | while(Q--){ 12 | scanf("%d%d%d%d",&A,&B,&C,&D); 13 | if(mymap[A].find(B)!=mymap[A].end() && mymap[A][B].find(C)!=mymap[A][B].end() && 14 | mymap[A][B][C].find(D)!=mymap[A][B][C].end()){ 15 | if(mymap[A][B][C][D]) 16 | printf("Yes\n"); 17 | else printf("No\n"); 18 | }else{ 19 | p=s.substr(A-1,B-A+1); 20 | q=s.substr(C-1,D-C+1); 21 | if(p==q){ 22 | mymap[A][B][C][D]=1; 23 | printf("Yes\n"); 24 | }else { 25 | mymap[A][B][C][D]=0; 26 | printf("No\n"); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /InterviewBit/Trees/CARTESIAN.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int maxindex(int b,int e,vector &A){ 11 | int i; 12 | int maxi=b; 13 | for(i=b+1;i<=e;i++){ 14 | if(A[maxi] &A){ 20 | if(b>e) 21 | return NULL; 22 | int j=maxindex(b,e,A); 23 | TreeNode *treenode=new TreeNode(A[j]); 24 | treenode->left=makeTree(b,j-1,A); 25 | treenode->right=makeTree(j+1,e,A); 26 | return treenode; 27 | 28 | } 29 | TreeNode* Solution::buildTree(vector &A) { 30 | return makeTree(0,A.size()-1,A); 31 | } 32 | -------------------------------------------------------------------------------- /Spoj/EC_CONB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int noOfBits(long long c) 5 | { 6 | int i=0; 7 | while(1) 8 | { 9 | long long q=pow(2,i); 10 | if(q>c) 11 | return i; 12 | else if(q==c) 13 | return i+1; 14 | i++; 15 | } 16 | } 17 | void method(int c) 18 | { 19 | int n=noOfBits(c); 20 | int s=pow(2,n-1); 21 | int *a=new int [n]; 22 | for(int i=0;i>(n-i-1); 26 | c=c<<1; 27 | } 28 | int ans=0; 29 | for(int i=0;i>b; 39 | int *f=new int[b]; 40 | for(int i=0;i>f[i]; 42 | for(int i=0;i 2 | #define IN(X) scanf("%d",&X) 3 | using namespace std; 4 | int D[101][101]; 5 | int main(){ 6 | int T,N,M,X,Y,C,S,A,H; 7 | int i,j,k; 8 | IN(T); 9 | while(T--){ 10 | IN(N);IN(M); 11 | for(i=0;i<=N;i++) 12 | for(j=0;j<=N;j++) 13 | D[i][j]=9901; 14 | 15 | for(i=0;i<=N;i++) 16 | D[i][i]=0; 17 | while(M--){ 18 | IN(X);IN(Y);IN(C); 19 | D[X][Y]=min(D[X][Y],C); 20 | D[Y][X]=D[X][Y]; 21 | } 22 | 23 | for(k=1;k<=N;k++){ 24 | for(i=1;i<=N;i++){ 25 | for(j=1;j<=N;j++){ 26 | D[i][j]=min(D[i][j],D[i][k]+D[k][j]); 27 | } 28 | } 29 | } 30 | IN(S);IN(A);IN(H); 31 | int ans=INT_MIN; 32 | for(i=1;i<=N;i++){ 33 | if(i!=S && i!=H && i!=A){ 34 | ans=max(ans,D[S][i]+2*D[i][A]+D[i][H]); 35 | } 36 | } 37 | printf("%d\n",ans ); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Spoj/DCEPC11B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | using namespace std; 4 | ll a_mul_b_mod_m(ll a, ll b, ll m){ 5 | ll x=0, y=a%m; 6 | while(b>0){ 7 | if(b%2==1) 8 | x=(x+y)%m; 9 | y=(y<<1)%m; 10 | b=b>>1; 11 | } 12 | return x; 13 | } 14 | ll a_power_p_mod_m(ll a, ll p, ll m){ 15 | ll x=1,y=a%m; 16 | while(p>0){ 17 | if(p%2==1) 18 | x=(x*y)%m; 19 | y=(y*y)%m; 20 | p=p>>1; 21 | } 22 | return x; 23 | } 24 | ll calculate_a(ll n,ll p,ll m){ 25 | ll x=1; 26 | while(n=p) 41 | answer=0; 42 | printf("%lld\n",answer); 43 | } 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /Spoj/PRIME1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | long long z=0; 7 | long long t; 8 | cin>>t; 9 | while(z>m; 14 | cin>>n; 15 | char *array1=new char[n-m+1]; 16 | for(long long i=0;i<=n-m;i++) 17 | array1[i]='0'; 18 | for(long long i=2;i<=sqrt(n);i++) 19 | { 20 | long long k=ceil(m/i); 21 | if(k==0||k==1) 22 | k=i; 23 | //if(array1[i]=='0') 24 | for(long long j=i*k;j<=n;j+=i) 25 | if(j-m>=0) 26 | array1[j-m]='1'; 27 | } 28 | for(long long i=m;i 2 | using namespace std; 3 | int main() 4 | { 5 | int t,n; 6 | bool flag; 7 | scanf("%d",&t); 8 | int a[1001]; 9 | int b[1001]; 10 | for(int j=0;jn-1) 19 | { 20 | flag=false; 21 | cout<<"NO"< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n,temp,i,sec=0; 10 | queue idealorder; 11 | deque callingorder; 12 | scanf("%d",&n); 13 | for(i=0;i 2 | #define IN(L) scanf("%d",&L) 3 | #define mod 1000000007 4 | #define ll long long 5 | using namespace std; 6 | int main(){ 7 | ll N,K,i,j; 8 | scanf("%lld%lld",&N,&K); 9 | ll A[1001][1001]; 10 | memset(A,0,sizeof(A)); 11 | A[0][0]=1; 12 | for(i=1;i<=N;i++){ 13 | A[i][0]=A[i][i]=1; 14 | for(j=1;j 2 | using namespace std; 3 | int main() 4 | { 5 | int l; 6 | cin>>l; 7 | cout<>n; 13 | int list[n]; 14 | for(int i=0;i>list[i]; 16 | int array[101]; 17 | for(int i=0;i<101;i++) 18 | array[i]=0; 19 | for(int i=0;i > &A) { 3 | int i,j,c; 4 | int m=A.size(); 5 | int n=A[0].size(); 6 | int C[m][m]; 7 | int maxArea=0; 8 | int NC[m][m]; 9 | memset(C,0,sizeof(C)); 10 | 11 | for(c=0;c mymap; 6 | int i; 7 | int maxLength=0; 8 | int currentLength=0; 9 | int p=0,q=0; 10 | int n=A.length(); 11 | while(q &A) { 2 | int n=A.size(),i,j; 3 | set myset; 4 | myset.insert(A.begin(),A.end()); 5 | vector B; 6 | for(auto it=myset.begin();it!=myset.end();++it) 7 | B.push_back(*it); 8 | int m=B.size(); 9 | vector< int > v(n,0); 10 | 11 | if(A[0]==B[0]) 12 | v[0]=1; 13 | for(i=1;i0) i++; 14 | noOfDigits=i; 15 | if(noOfDigits==1) 16 | return true; 17 | int B=A; 18 | int C=0; 19 | int temp; 20 | for(i=0;i &A) { 5 | int n=A.size(); 6 | if(n==0) 7 | return 0; 8 | int m=A[n-1]; 9 | int i; 10 | int freq=1; 11 | int p=0; 12 | for(i=1;i=2) 21 | A[i]=m+1; 22 | } 23 | i=0; 24 | while(i &A) { 5 | int n=A.size(); 6 | if(n==0) 7 | return 0; 8 | int m=A[n-1]; 9 | int i; 10 | int freq=1; 11 | int p=0; 12 | for(i=1;i=3) 21 | A[i]=m+1; 22 | } 23 | i=0; 24 | while(i1) 31 | return 1; 32 | return 0; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /InterviewBit/String/PALINDROME.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 3 | *Example: 4 | *"A man, a plan, a canal: Panama" is a palindrome. 5 | *"race a car" is not a palindrome. 6 | *Return 0 / 1 ( 0 for false, 1 for true ) for this problem 7 | */ 8 | 9 | int Solution::isPalindrome(string A) { 10 | string temp=""; 11 | int i=0; 12 | int n=A.length(); 13 | for(i=0;i=0 && A[i]-'a'<26) 15 | temp+=A[i]; 16 | else if(A[i]-'0'>=0 && A[i]-'0'<=9) 17 | temp+=A[i]; 18 | else if(A[i]-'A'>=0 && A[i]-'A'<26){ 19 | A[i]=A[i]-'A'+'a'; 20 | temp+=A[i]; 21 | } 22 | } 23 | n=temp.length(); 24 | for(i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int d; 9 | cin>>d; 10 | while(d--) 11 | { 12 | string a, b; 13 | cin>>a; 14 | cin>>b; 15 | for(int i=0;i0) 34 | { 35 | cout< 2 | #include 3 | using namespace std; 4 | long long numberSteps(long long n,long long m ) 5 | { 6 | if(n%2==0) 7 | { 8 | if(m==n-2&&m>=0&&n>=0) 9 | return 2*n-2; 10 | else if(m==n&&n>=0) 11 | return 2*n; 12 | else return -1; 13 | } 14 | else 15 | { 16 | if(m==n-2&&m>=0&&n>=0) 17 | return 2*n-3; 18 | else if(m==n&&n>=0) 19 | return 2*n-1; 20 | else return -1; 21 | } 22 | return -1; 23 | } 24 | int main() 25 | { 26 | long long t,z=0; 27 | cin>>t; 28 | while(z>n; 32 | cin>>m; 33 | long long s=numberSteps(n,m); 34 | if(s>=0) 35 | cout< 7 | #include 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;a>1; 18 | if(array[mid]==x) 19 | return mid; 20 | else if(array[mid] 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b) for(a=0;a 2 | #define MAX 100009 3 | #define mod 1000000007 4 | #define ll long long 5 | #define ini(x) scanf("%d",&x); 6 | #define inl(x) scanf("%lld",&x); 7 | #define type double 8 | using namespace std; 9 | ll g[MAX][3]; 10 | ll dp[MAX][3]; 11 | const ll inf=(ll)1<<60; 12 | 13 | int main(){ 14 | int n,i,j,k,T,N,M; 15 | ini(n); 16 | k=1; 17 | while(n!=0){ 18 | for(i=0;i &v){ 7 | if(n==1){ 8 | v.push_back(0); 9 | v.push_back(1); 10 | return; 11 | } 12 | generateGrayCode(n-1,v); 13 | int m=v.size(); 14 | int two=1; 15 | int i=1; 16 | while(i=0;i--) 21 | v.push_back(v[i]|two); 22 | 23 | } 24 | vector Solution::grayCode(int A) { 25 | vector v; 26 | if(A==0){ 27 | v.push_back(0); 28 | return v; 29 | } 30 | generateGrayCode(A,v); 31 | return v; 32 | } 33 | -------------------------------------------------------------------------------- /InterviewBit/String/ROMAN2INT.cpp: -------------------------------------------------------------------------------- 1 | /* Given a roman numeral, convert it to an integer.*/ 2 | int value(char A){ 3 | switch(A){ 4 | case 'M' : 5 | return 1000; 6 | case 'D' : 7 | return 500; 8 | case 'C' : 9 | return 100; 10 | case 'L': 11 | return 50; 12 | case 'X' : 13 | return 10; 14 | case 'V' : 15 | return 5; 16 | case 'I' : 17 | return 1; 18 | default: 19 | return 0; 20 | } 21 | } 22 | int Solution::romanToInt(string A) { 23 | 24 | int previous=0; 25 | int current=0; 26 | int ans=0; 27 | int i=0; 28 | for(i=0;iprevious){ 31 | ans=ans-2*previous; 32 | } 33 | ans+=current; 34 | previous=current; 35 | } 36 | return ans; 37 | } 38 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/rotateArrayReversalAlgo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write a program to rotate an array by d elements 3 | *In this implementation we rotate counter-clockwise 4 | *Time=O(n) space O(1) 5 | */ 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b,c) for(a=c;a 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int t,n; 7 | 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | 12 | scanf("%d",&n); 13 | int *a=new int [n]; 14 | for(int i=0;i 9 | #define IN(a) scanf("%d",&a) 10 | #define FOR(a,b,c) for(a=c;a=0){ 17 | if(max 6 9 | */ 10 | int Solution::maxArea(vector &A) { 11 | int n=A.size(); 12 | int p=0,q=n-1; 13 | int maxA=0; 14 | while(pnext; 17 | n++; 18 | } 19 | current=A; 20 | if(n <= B){ 21 | A=A->next; 22 | free(current); 23 | return A; 24 | } 25 | n=n-B-1; 26 | while(n--){ 27 | current=current->next; 28 | } 29 | previous=current; 30 | current=current->next; 31 | previous->next=current->next; 32 | current->next=NULL; 33 | free(current); 34 | return A; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Spoj/VFRIENDS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | 6 | 7 | long long t,m,p,count; 8 | int n,temp; 9 | bool flag; 10 | int a[100001]; 11 | long long sum[100001]; 12 | scanf("%lld",&t); 13 | //int *c=new int[100001]; 14 | while(t--) 15 | { 16 | scanf("%d",&n); 17 | m=0; 18 | for(int i=n;i>=1;i--) 19 | { 20 | scanf("%d",&a[i]); 21 | m+=a[i]; 22 | } 23 | if(m%2==0) 24 | { 25 | for(int k=1;k<=n;k++) 26 | { 27 | sum[k]=sum[k-1]+a[k]; 28 | p=k*(k-1); 29 | count =0; 30 | for(int j=k+1;j<=n;j++) 31 | { 32 | temp=a[j]; 33 | if(kp+count) 38 | { 39 | flag=false; 40 | break; 41 | } 42 | else flag=true; 43 | } 44 | } 45 | else 46 | flag=false; 47 | if(flag) 48 | printf("HAPPY\n"); 49 | else 50 | printf("SAD\n"); 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /Algorithms/extendedEuclid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 1= xa+yb for the numbers a,b 3 | * x=a inverse mod b 4 | * y=b inverse mod a 5 | * Ax+By=GCD(A,B)=GCD(B,A%B) 6 | */ 7 | class ExtendedEuclid{ 8 | public: 9 | ExtendedEuclid():x(0),y(0),gcd(0){} 10 | int aInverseModB(int,int); 11 | int getGcd(int,int); 12 | private: 13 | int x,y; 14 | int gcd; 15 | void calExtendedEuclid(int,int); 16 | }; 17 | void ExtendedEuclid :: calExtendedEuclid(int A,int B){ 18 | if(B==0){ 19 | gcd=A; 20 | x=1; 21 | y=0; 22 | return; 23 | } 24 | calExtendedEuclid(B,A%B); 25 | int temp=x; 26 | x=y; 27 | y=temp-((int)(A/B))*y; 28 | } 29 | int ExtendedEuclid :: aInverseModB(int A,int B){ 30 | calExtendedEuclid(A,B); 31 | if(gcd!=1) 32 | return -1; //inverse do not exist 33 | if(x<0) 34 | return x+B; 35 | return x; 36 | } 37 | int ExtendedEuclid :: getGcd(int A,int B){ 38 | calExtendedEuclid(A,B); 39 | return gcd; 40 | } 41 | -------------------------------------------------------------------------------- /InterviewBit/Backtracking/SUBSET2.cpp: -------------------------------------------------------------------------------- 1 | /* Given a collection of integers that might contain duplicates, S, return all possible subsets. */ 2 | void sst(vector< vector > &ans, vector &A, 3 | vector &row,int i){ 4 | if(i>=A.size()) 5 | return; 6 | 7 | int lastIndex=i+1; 8 | while(lastIndex > Solution::subsetsWithDup(vector &A) { 20 | vector< vector > matrix; 21 | vector row; 22 | sort(A.begin(),A.end()); 23 | matrix.push_back(row); 24 | sst(matrix,A,row,0); 25 | sort(matrix.begin(),matrix.end()); 26 | return matrix; 27 | } 28 | -------------------------------------------------------------------------------- /InterviewBit/Array/Positive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an unsorted integer array, find the first missing positive integer 3 | */ 4 | 5 | /* 6 | *turning array into frequency array 7 | */ 8 | 9 | 10 | int Solution::firstMissingPositive(vector &A) { 11 | // Do not write main() function. 12 | // Do not read input, instead use the arguments to the function. 13 | // Do not print the output, instead return values as specified 14 | // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details 15 | int n=A.size(); 16 | int i=0,index; 17 | for(i=0;in) 19 | A[i]=0; 20 | i=0; 21 | while(i0){ 23 | index=A[i]-1; 24 | if(A[index]>=0) 25 | A[i]=A[index]; 26 | else A[i]=0; 27 | A[index]=-1; 28 | }else i++; 29 | } 30 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main(){ 8 | int T,N,i,k,j,l=21; 9 | string A[10]; 10 | cin>>T; 11 | string s,ans="",temp; 12 | bool present; 13 | while(T--){ 14 | l=21; 15 | cin>>N; 16 | ans=""; 17 | for(i=0;i>A[i]; 19 | if(A[i].length()0;i--){ 25 | for(j=0;jans.length()) 34 | ans=s; 35 | else if(s.length()==ans.length() && s0) 40 | break; 41 | } 42 | cout< &DQ){ 7 | if(DQ.size()==0) 8 | return; 9 | auto it=DQ.begin(); 10 | while(fact[n-1] DQ; 20 | int i; 21 | for(i=1;i<=n;i++) 22 | DQ.push_back(i); 23 | fact[0]=1; 24 | for(i=1;i<13;i++) 25 | fact[i]=fact[i-1]*i; 26 | string s=""; 27 | auto it=DQ.begin(); 28 | while(n>13){ 29 | s.append(to_string(*it)); 30 | n--; 31 | DQ.erase(it); 32 | it=DQ.begin(); 33 | } 34 | permute(s,n,k,DQ); 35 | return s; 36 | } 37 | -------------------------------------------------------------------------------- /InterviewBit/Trees/LCA.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int callca(TreeNode *A,int val1,int val2){ 11 | if(!A) 12 | return -1; 13 | if(A->val==val1 || A->val==val2) 14 | return A->val; 15 | int l=callca(A->left,val1,val2); 16 | int r=callca(A->right,val1,val2); 17 | if(l!=-1 && r!=-1) 18 | return A->val; 19 | else if(l!=-1) 20 | return l; 21 | else return r; 22 | } 23 | bool present(TreeNode *A,int a){ 24 | if(!A) 25 | return 0; 26 | if(A->val==a) 27 | return 1; 28 | return (present(A->left,a) | present(A->right,a)); 29 | } 30 | int Solution::lca(TreeNode* A, int val1, int val2) { 31 | if(present(A,val1) && present(A,val2)) 32 | return callca(A,val1,val2); 33 | return -1; 34 | } 35 | -------------------------------------------------------------------------------- /InterviewBit/Link-List/REVERSELIST.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 | ListNode* Solution::reverseBetween(ListNode* A, int m, int n) { 10 | if(m==n) 11 | return A; 12 | ListNode *previous,*current,*front,*start=NULL,*end=NULL; 13 | previous=NULL; 14 | current=A; 15 | front=A->next; 16 | int i=1; 17 | while(front && inext; 26 | if(i>=m) 27 | current->next=previous; 28 | i++; 29 | } 30 | if(start) 31 | start->next=front; 32 | if(end) 33 | end->next=current; 34 | else A=current; 35 | return A; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Algorithms/kadane'sAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write an efficient C++ program to find the sum of contiguous subarray, 3 | *within a one-dimensional array of numbers which has the largest sum 4 | *time=O(n) space=O(1) no additional space 5 | *uses kadane's algorithm returns 0 if all elements are negative 6 | */ 7 | #include 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;a x and index(x) is smallest 4 | *Time=O(logn) & space O(1) 5 | *input is taken until eof is encountered or ctrl+D in linux 6 | */ 7 | #include 8 | #include 9 | #define IN(a) scanf("%d",&a) 10 | #define FOR(a,b) for(a=0;a>1; 19 | if(array[mid]<=x) 20 | l=mid+1; 21 | else r=mid-1; 22 | } 23 | if(r==n-1) 24 | return -1; 25 | return r+1; 26 | } 27 | int main(){ 28 | int i; // loop variable 29 | int n; // no of elements in given array 30 | int x; //element to find 31 | IN(n); 32 | array=new int[n]; 33 | FOR(i,n) 34 | IN(array[i]); 35 | while(IN(x)!=EOF) 36 | printf("%d\n",ceiling(n,x)); 37 | } 38 | -------------------------------------------------------------------------------- /Spoj/PIR.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class VolumeOfTetrahedron { 3 | public static double roundoff( double n) 4 | { 5 | return (n*10000+.49); 6 | } 7 | 8 | public static void main(String[] args) { 9 | Scanner c=new Scanner(System.in); 10 | int t=c.nextInt(); 11 | int z=0; 12 | while(z > Solution::levelOrder(TreeNode* A) { 11 | queue Q; 12 | queue L; 13 | if(A){ 14 | Q.push(A); 15 | L.push(0); 16 | } 17 | vector> v; 18 | vector r; 19 | while(!Q.empty()){ 20 | TreeNode *T=Q.front(); 21 | int l=L.front(); 22 | if(v.size()!=l+1){ 23 | v.push_back(r); 24 | } 25 | if(T->left){ 26 | Q.push(T->left); 27 | L.push(l+1); 28 | } 29 | if(T->right){ 30 | Q.push(T->right); 31 | L.push(l+1); 32 | } 33 | v[l].push_back(T->val); 34 | Q.pop(); 35 | L.pop(); 36 | } 37 | return v; 38 | } 39 | -------------------------------------------------------------------------------- /HackerEarth/OnlyIntegersAllowed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Point{ 4 | long long x,y; 5 | }; 6 | long long gcd(long long a,long long b){ 7 | if(b==0) 8 | return a; 9 | return gcd(b,a%b); 10 | } 11 | long long foo(Point p1, Point p2){ 12 | long long a=abs(p2.y-p1.y); 13 | long long b=abs(p2.x-p1.x); 14 | return gcd(a,b)-1; 15 | 16 | } 17 | int main() 18 | { 19 | int N,i; 20 | scanf("%d",&N); 21 | Point arr[N]; 22 | for(i=0;i>1; 33 | for(i=0;i>1; 40 | printf("%lld\n",area+1-N); 41 | } 42 | -------------------------------------------------------------------------------- /InterviewBit/Backtracking/PALINPART.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given a string s, partition s such that every string of the partition is a palindrome. 3 | *Return all possible palindrome partitioning of s. 4 | */ 5 | bool isPalindrome(string s){ 6 | int n=s.length(); 7 | for(int i=0;i > &v,vector &row,string &s,int l,int n){ 13 | if(l==n){ 14 | v.push_back(row); 15 | return; 16 | } 17 | string temp=""; 18 | for(int i=l;i > Solution::partition(string A) { 28 | vector > ans; 29 | vector row; 30 | partition1(ans,row,A,0,A.length()); 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /InterviewBit/Two-Pointers/3SUM.cpp: -------------------------------------------------------------------------------- 1 | /* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. 2 | Return the sum of the three integers. 3 | */ 4 | int closest(vector &A,int start,int end,int B){ 5 | int diff=INT_MAX; 6 | int sum; 7 | int ans; 8 | int p=start,q=end; 9 | while(q>p){ 10 | sum=A[p]+A[q]; 11 | if(abs(B-sum)B)q--; 16 | else p++; 17 | } 18 | return ans; 19 | } 20 | int Solution::threeSumClosest(vector &A, int B) { 21 | int n=A.size(); 22 | sort(A.begin(),A.end()); 23 | int value; 24 | int ans=0; 25 | int diff=INT_MAX; 26 | for(int i=0;iabs(value-B)){ 30 | ans=value; 31 | diff=abs(value-B); 32 | } 33 | } 34 | return ans; 35 | } 36 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/MAXPROD.cpp: -------------------------------------------------------------------------------- 1 | int B(vector A){ 2 | int i,j,n=A.size(); 3 | if(n==1) 4 | return A[0]; 5 | long long k=A[0]; 6 | for(i=1;i0) 9 | return k; 10 | long long l=1,r=1; 11 | i=0; 12 | while(A[i]>0){ 13 | l*=A[i]; 14 | i++; 15 | } 16 | l*=A[i]; 17 | i=n-1; 18 | while(A[i]>0){ 19 | r*=A[i]; 20 | i--; 21 | } 22 | r*=A[i]; 23 | return (int)max(k/l,k/r); 24 | } 25 | int Solution::maxProduct(const vector &A) { 26 | int ans=A[0],j=A[0]; 27 | vector v; 28 | int i; 29 | for(i=0;i0) 35 | ans=max(ans,B(v)); 36 | v.clear(); 37 | } 38 | } 39 | if(v.size()>0) 40 | ans=max(ans,B(v)); 41 | return max(ans,j); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Spoj/TOANDFRO.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ToAndFro { 3 | 4 | public static void main(String[] args) 5 | { 6 | Scanner c=new Scanner(System.in); 7 | int w=1; 8 | while(w==1) 9 | { 10 | int n=c.nextInt(); 11 | if(n==0) 12 | break; 13 | String t=c.next(); 14 | int m=(int)(t.length()/n)+1; 15 | char a[][]=new char[m][n]; 16 | for(int i=0;i=0&&k > &v,vector &row,vector &A,int B,int i){ 6 | if(B==0){ 7 | v.push_back(row); 8 | return; 9 | } 10 | if(i==A.size() || B<0) 11 | return; 12 | row.push_back(A[i]); 13 | generateCombinations(v,row,A,B-A[i],i); 14 | row.pop_back(); 15 | generateCombinations(v,row,A,B,i+1); 16 | } 17 | vector > Solution::combinationSum(vector &A, int B) { 18 | vector< vector > v; 19 | vector row; 20 | vector b; 21 | sort(A.begin(),A.end()); 22 | b.push_back(A[0]); 23 | for(int i=1;inext; 16 | if(!hare) 17 | return NULL; 18 | hare=hare->next; 19 | tortoise=tortoise->next; 20 | } 21 | while(hare && hare!=tortoise); 22 | if(!hare) 23 | return NULL; 24 | int n=0; 25 | do{ 26 | hare=hare->next; 27 | n++; 28 | }while(tortoise!=hare); 29 | hare=A; 30 | tortoise=A; 31 | while(n--) 32 | hare=hare->next; 33 | while(hare!=tortoise){ 34 | hare=hare->next; 35 | tortoise=tortoise->next; 36 | } 37 | return hare; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /InterviewBit/Backtracking/SUBSET.cpp: -------------------------------------------------------------------------------- 1 | /* Given a set of distinct integers, S, return all possible subsets. */ 2 | void subsetcombine(vector< vector > &ans,vector &A, int i){ 3 | int n=A.size(); 4 | if(i==n) 5 | return; 6 | vector row; 7 | subsetcombine(ans,A,i+1); 8 | int m=ans.size(); 9 | for(int j=0;j > Solution::subsets(vector &A) { 21 | vector< vector > matrix; 22 | sort(A.begin(),A.end()); 23 | subsetcombine(matrix,A,0); 24 | vector > ans; 25 | vector row; 26 | ans.push_back(row); 27 | while(!matrix.empty()){ 28 | ans.push_back(matrix[matrix.size()-1]); 29 | matrix.pop_back(); 30 | } 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /InterviewBit/Stacks-And-Queues/BRACES.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *write program to validate if the input string has redundant braces ? 3 | *Return 0 if not redundant else 1 4 | */ 5 | 6 | int Solution::braces(string A) { 7 | stack mystack; 8 | int i; 9 | int n=A.length(); 10 | bool redundant=0; 11 | bool express; 12 | for(i=0;i 2 | #define mod 1000003 3 | #define mod2 1000000007 4 | #define ll long long 5 | #define max 10000001 6 | #define base 101 7 | using namespace std; 8 | 9 | int *arr=new int[max]; 10 | int *phi=new int[max]; 11 | void findFactors(){ 12 | int i,j; 13 | for(i=0;i > Solution::threeSum(vector &A) { 7 | int n=A.size(); 8 | vector v(3,0); 9 | vector< vector > B; 10 | sort(A.begin(),A.end()); 11 | int i,j; 12 | int p,q,r; 13 | for(i=2;i0 && B[B.size()-1][0]==v[0]&&B[B.size()-1][1]==v[1]&& 22 | B[B.size()-1][2]==v[2]); 23 | else B.push_back(v); 24 | p++; 25 | q--; 26 | }else if(r>A[p]+A[q]) p++; 27 | else q--; 28 | } 29 | } 30 | } 31 | return B; 32 | } 33 | -------------------------------------------------------------------------------- /Spoj/MYQ1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define gc getchar_unlocked 4 | using namespace std; 5 | void scanint(int &x) 6 | { 7 | register int c = gc(); 8 | x = 0; 9 | for(;(c<48 || c>57);c = gc()); 10 | for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;} 11 | } 12 | int main(){ 13 | int T,x,r,n; 14 | char type,dir; 15 | scanint(T); 16 | while(T--){ 17 | scanint(x); 18 | x--; 19 | r=ceil((double)x/5); 20 | n=x-((r-1)*5); 21 | if(r%2==0){ 22 | if(n<4) 23 | dir='R'; 24 | else dir='L'; 25 | if(n==1||n==5) 26 | type='W'; 27 | else if(n==2) 28 | type='M'; 29 | else if(n==3||n==4) 30 | type='A'; 31 | else; 32 | 33 | }else{ 34 | if(n>2) 35 | dir='R'; 36 | else dir='L'; 37 | if(n==1||n==5) 38 | type='W'; 39 | else if(n==4) 40 | type='M'; 41 | else if(n==2||n==3) 42 | type='A'; 43 | else; 44 | } 45 | if(x==0) 46 | printf("poor conductor\n"); 47 | else printf("%d %c %c\n",r,type,dir); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/floorInASortedArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Returns floor of a element in a sorted array 3 | *floor(x) <= x 4 | *Time=O(logn) & space O(1) 5 | *input is taken until eof is encountered or ctrl+D in linux 6 | */ 7 | #include 8 | #include 9 | #define IN(a) scanf("%d",&a) 10 | #define FOR(a,b) for(a=0;a>1; 19 | if(array[mid]==x){ 20 | if(mid!=0 && array[mid-1]==x) 21 | r=mid-1; 22 | else return mid; 23 | } 24 | else if(array[mid] 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | map mymap; 7 | int Q,i,X; 8 | 9 | scanf("%d",&Q); 10 | while(Q--){ 11 | scanf("%d",&i); 12 | if(i==1){ 13 | scanf("%d",&X); 14 | if(mymap.find(X)==mymap.end()) 15 | mymap[X]=1; 16 | else mymap[X]+=1; 17 | }else if(i==2){ 18 | scanf("%d",&X); 19 | if(mymap.find(X)!=mymap.end()){ 20 | if(mymap[X]==1) 21 | mymap.erase(X); 22 | else mymap[X]-=1; 23 | }else printf("-1\n"); 24 | }else if(i==3){ 25 | if(mymap.empty()) 26 | printf("-1\n"); 27 | else{ 28 | map ::iterator it=mymap.end(); 29 | it--; 30 | printf("%d\n",it->first); 31 | } 32 | }else if(i==4){ 33 | if(mymap.empty()) 34 | printf("-1\n"); 35 | else{ 36 | map ::iterator it=mymap.begin(); 37 | printf("%d\n",it->first); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Codechef/HELLO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define FOR(a,b) for(a=0;a temp_cost){ 21 | new_cost=temp_cost; 22 | plani=i; 23 | } 24 | } 25 | return (plani+1); 26 | } 27 | int main() 28 | { 29 | int i; 30 | int T; //test cases 31 | float D; //default rate 32 | int U; //no of minutes 33 | int N; //no of plans 34 | scanf("%d",&T); 35 | while(T--){ 36 | scanf("%f%d%d",&D,&U,&N); 37 | FOR(i,N) 38 | scanf("%d%f%d",&plans[i].M,&plans[i].R,&plans[i].C); 39 | printf("%d\n",calculateAnswer(N,U,D) ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /HackerEarth/ChanduAndHisGirlfriend.c: -------------------------------------------------------------------------------- 1 | #include 2 | void merge(int A[],int l,int r){ 3 | int mid=(l+r)>>1; 4 | int Aux[r-l+1]; 5 | int p=l,q=mid+1,i=0,j; 6 | while(p<=mid && q<=r){ 7 | if(A[p]<=A[q]){ 8 | Aux[i]=A[p]; 9 | p++; 10 | }else{ 11 | Aux[i]=A[q]; 12 | q++; 13 | } 14 | i++; 15 | } 16 | while(p<=mid){ 17 | Aux[i]=A[p]; 18 | i++; 19 | p++; 20 | } 21 | while(q<=r){ 22 | Aux[i]=A[q]; 23 | i++; 24 | q++; 25 | } 26 | for(j=0;j>1; 35 | split(A,l,mid); 36 | split(A,mid+1,r); 37 | merge(A,l,r); 38 | } 39 | int main() 40 | { 41 | int i,n,T; 42 | int A[100000]; 43 | scanf("%d",&T); 44 | while(T--){ 45 | scanf("%d",&n); 46 | for(i=0;i=0;i--) 50 | printf("%d ",A[i]); 51 | printf("\n"); 52 | } 53 | return 0; 54 | } 55 | 56 | -------------------------------------------------------------------------------- /InterviewBit/Stacks-And-Queues/EVALEXP.cpp: -------------------------------------------------------------------------------- 1 | /* evaluate rpn */ 2 | int evalRPN(vector &A) { 3 | stack mystack; 4 | int n=A.size(); 5 | int a=0,b=0; 6 | for(int i=0;i1){ 8 | a=stoi(A[i],nullptr,10); 9 | mystack.push(a); 10 | 11 | } 12 | else if(A[i][0]-'0'<=9 &&A[i][0]-'0'>=0) 13 | mystack.push(stoi(A[i],nullptr,10)); 14 | else{ 15 | a=mystack.top(); 16 | mystack.pop(); 17 | b=mystack.top(); 18 | mystack.pop(); 19 | if(A[i]=="/"){ 20 | if(a==0) 21 | return 0; 22 | b=b/a; 23 | } 24 | else if(A[i]=="*") 25 | b=b*a; 26 | else if(A[i]=="+") 27 | b=b+a; 28 | else b=b-a; 29 | mystack.push(b); 30 | } 31 | } 32 | b=mystack.top(); 33 | mystack.pop(); 34 | return b; 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Spoj/PTIME.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int powerFinder(int i,int n) 6 | { 7 | long long c=i; 8 | int sum=0; 9 | int m=1; 10 | while(m!=0) 11 | { 12 | m=n/c; 13 | sum+=m; 14 | c*=i; 15 | } 16 | return sum; 17 | } 18 | int main() 19 | { 20 | int n; 21 | cin>>n; 22 | int *a=new int[n+1]; 23 | for(int i=0;i<=n;i++) 24 | a[i]=0; 25 | for(int i=2;i<=sqrt(n);i++) 26 | for(int j=i*i;j<=n;j+=i) 27 | a[j]=1; 28 | int *array1=new int[n]; 29 | int l=0; 30 | for(int i=2;i<=n;i++) 31 | { 32 | if(a[i]==0){ 33 | array1[l]=i; 34 | l++; 35 | } 36 | } 37 | int *a2=new int [l]; 38 | for(int i=0;i 7 | #include 8 | #include 9 | #define IN(a) scanf("%d",&a) 10 | #define FOR(a,b) for(a=0;a=end) 20 | return; 21 | int pivot=array[start]; 22 | int l=start+1,r=end; 23 | int i; 24 | while(l<=r){ 25 | if(array[l]<=pivot) 26 | l++; 27 | else if(array[r]>pivot) 28 | r--; 29 | else swap(array[l],array[r]); 30 | } 31 | swap(array[start],array[l-1]); 32 | quicksort(start,l-2); 33 | quicksort(l,end); 34 | 35 | } 36 | int main(){ 37 | int i; // loop variable 38 | int n; // no of elements in given array 39 | IN(n); 40 | array=new int[n]; 41 | FOR(i,n) 42 | IN(array[i]); 43 | quicksort(0,n-1); 44 | FOR(i,n) 45 | printf("%d ",array[i]); 46 | printf("\n"); 47 | } 48 | -------------------------------------------------------------------------------- /InterviewBit/Trees/BINTREE2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int index(int s,int end,vector &inorder,int val){ 11 | for(int i=s;i<=end;i++) 12 | if(inorder[i]==val) 13 | return i; 14 | return -1; 15 | } 16 | TreeNode* makeTree(int &i,int b,int e,vector &inorder,vector &preorder){ 17 | if(b>e){ 18 | i-=1; 19 | return NULL; 20 | } 21 | TreeNode* A=new TreeNode(preorder[i]); 22 | if(b==e) 23 | return A; 24 | int j=index(b,e,inorder,preorder[i]); 25 | i+=1; 26 | A->left=makeTree(i,b,j-1,inorder,preorder); 27 | i+=1; 28 | A->right=makeTree(i,j+1,e,inorder,preorder); 29 | return A; 30 | 31 | } 32 | TreeNode* Solution::buildTree(vector &preorder, vector &inorder) { 33 | int i=0; 34 | return makeTree(i,0,preorder.size()-1,inorder,preorder); 35 | } 36 | -------------------------------------------------------------------------------- /Codechef/ORACLCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define FOR(i,a,b) for(i=a;i0 ){ 19 | count=0; 20 | FOR(i,0,n){ 21 | if(checkSubSeq(arr[i],s.substr(0,countS))) 22 | count++; 23 | else break; 24 | } 25 | if(count!=n) 26 | countS--; 27 | } 28 | return countS; 29 | } 30 | int main(){ 31 | int T,i,n,l,ans; 32 | scanf("%d",&T); 33 | string A,B; 34 | string arr[100]; 35 | while(T--){ 36 | A=""; 37 | B=""; 38 | scanf("%d",&n); 39 | FOR(i,0,n) 40 | cin>>arr[i]; 41 | l=arr[0].length(); 42 | FOR(i,0,l){ 43 | A+='a'; 44 | B+='b'; 45 | } 46 | ans=min(minCurse(A,n,arr),minCurse(B,n,arr)); 47 | printf("%d\n",ans); 48 | } 49 | } -------------------------------------------------------------------------------- /InterviewBit/Trees/BINTREE1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int index(int s,int end,vector &inorder,int val){ 11 | for(int i=s;i<=end;i++) 12 | if(inorder[i]==val) 13 | return i; 14 | return -1; 15 | } 16 | TreeNode* makeTree(int &i,int b,int e,vector &inorder,vector &postorder){ 17 | if(b>e){ 18 | i+=1; 19 | return NULL; 20 | } 21 | TreeNode* A=new TreeNode(postorder[i]); 22 | if(b==e) 23 | return A; 24 | int j=index(b,e,inorder,postorder[i]); 25 | i-=1; 26 | A->right=makeTree(i,j+1,e,inorder,postorder); 27 | i-=1; 28 | A->left=makeTree(i,b,j-1,inorder,postorder); 29 | return A; 30 | 31 | } 32 | TreeNode* Solution::buildTree(vector &inorder, vector &postorder) { 33 | int i=postorder.size()-1; 34 | return makeTree(i,0,i,inorder,postorder); 35 | } 36 | -------------------------------------------------------------------------------- /Spoj/COMDIV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 1000001 3 | using namespace std; 4 | int *numbers=new int [N]; 5 | int *noOfFactors=new int [N]; 6 | int gcd(int a,int b){ //euclidean algorithm 7 | int temp; 8 | if(b>a){ //always a>b 9 | temp=a; 10 | a=b; 11 | b=temp; 12 | } 13 | while(b!=0){ 14 | temp=a%b; 15 | a=b; 16 | b=temp; 17 | } 18 | return a; 19 | } 20 | int retlog(int &a,int n){ //log a base n 21 | int count=0; 22 | while(a%n==0){ 23 | count++; 24 | a/=n; 25 | } 26 | return count; 27 | } 28 | void sieve(){ 29 | int i,j; 30 | bool array[N]; 31 | for(i=0;i 8 | #include 9 | #include 10 | #define IN(a) scanf("%d",&a) 11 | #define FOR(a,b) for(a=0;a 2 | using namespace std; 3 | int find(int a[],int b){ 4 | if(a[b]==-1) 5 | return b; 6 | int key=b,val=b; 7 | while(val!=-1){ 8 | key=val; 9 | val=a[key]; 10 | } 11 | int parent=key; 12 | key=b;val=b; 13 | while(val!=-1){ 14 | key=val; 15 | val=a[key]; 16 | if(val!=-1) 17 | a[key]=parent; 18 | } 19 | return parent; 20 | 21 | } 22 | int main() 23 | { 24 | map mymap; 25 | int N,Q,a,b,p1,p2,i; 26 | scanf("%d%d",&N,&Q); 27 | int arr[N+1]; 28 | for(i=1;i<=N;i++){ 29 | arr[i]=-1; 30 | mymap[i]=i; 31 | } 32 | while(Q--){ 33 | scanf("%d%d",&i,&a); 34 | p1=find(arr,a); 35 | 36 | if(i==1){ 37 | scanf("%d",&b); 38 | p2=find(arr,b); 39 | if(p1!=p2){ 40 | arr[p1]=p2; 41 | mymap.erase(p1); 42 | } 43 | }else if(i==2){ 44 | mymap[p1]=a; 45 | }else printf("%d\n",mymap[p1]); 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /InterviewBit/Greedy/SEATS.cpp: -------------------------------------------------------------------------------- 1 | #define mod 10000003 2 | #define ll long long 3 | ll hops(string A,int si){ 4 | int i,k=si,n=A.length(); 5 | ll count=0; 6 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAX 1000000 8 | #define ll long long 9 | #define FOR(i,a,n) for(i=a;i>a>>b; 17 | if(abs(i-k)%2==1){ 18 | i++; 19 | c=sqrt(2)*(a+b)-sqrt(6)*(a-b); 20 | d=sqrt(2)*(a-b)+sqrt(6)*(a+b); 21 | a=c; 22 | b=d; 23 | } 24 | if(k>=i){ 25 | numpower=(k-i)*2; 26 | }else{ 27 | denopower=(i-k)*2; 28 | } 29 | if(s>=0) 30 | denopower+=s; 31 | else numpower+=abs(s); 32 | a+=b; 33 | if(denopower>=numpower){ 34 | denopower-=numpower; 35 | while(denopower>0 && abs(a)>=0.01){ 36 | a/=2; 37 | denopower--; 38 | } 39 | printf("%0.5f\n",a); 40 | }else{ 41 | numpower-=denopower; 42 | while(numpower>0){ 43 | a*=2; 44 | numpower--; 45 | } 46 | printf("%0.5f\n",a); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Spoj/UPDATEIT.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | author Manu 3 | algorithm : BIT 4 | */ 5 | 6 | #include 7 | #define MAX 10001 8 | #define ll long long 9 | using namespace std; 10 | 11 | ll bit[MAX]; 12 | void update(int i, ll x){ 13 | int index=i+1; 14 | while(index<=MAX){ 15 | bit[index]+=x; 16 | index=index+(index&(-index)); 17 | } 18 | } 19 | ll query(int x){ 20 | ll ans=0; 21 | int index=x+1; 22 | while(index>0){ 23 | ans+=bit[index]; 24 | index=index-(index&(-index)); 25 | } 26 | return ans; 27 | } 28 | 29 | int main(){ 30 | int t,i,j,T; 31 | scanf("%d",&T); 32 | 33 | for(t=0;t 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;a 7 | #include 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;a> function(int n){ 13 | if(n==0){ 14 | vector> v2; 15 | return v2; 16 | } 17 | vector v1(n,0); 18 | vector> v(n,v1); 19 | v[0][0]=1; 20 | for(int i=1;ii;j--) 28 | v[i].pop_back(); 29 | for(int i=0;i &A) { 15 | int i,j; 16 | int n=A.size(); 17 | int evenCount=0,oddCount=0; 18 | int ans=0,bit=0,place=1; 19 | for(i=0;i<32;i++){ 20 | bit=0; 21 | evenCount=0; 22 | oddCount=0; 23 | for(j=0;j 7 | #include 8 | using namespace std; 9 | void printDistinctElements(int *arr,int n,int k){ 10 | unordered_map mymap; 11 | int i,p; 12 | int distinct=0; 13 | for(i=0;i>n; 40 | int *array=new int[n]; 41 | for(int i=0;i>array[i]; 43 | } 44 | cin>>k; 45 | printDistinctElements(array,n,k); 46 | } 47 | -------------------------------------------------------------------------------- /InterviewBit/DynamicProgramming/DECODEWAYS.cpp: -------------------------------------------------------------------------------- 1 | int Solution::numDecodings(string A) { 2 | int q,p,ans; 3 | int i,n=A.length(); 4 | 5 | if(n==1){ 6 | if(A[i]-'0'<=9 && A[i]-'0'>0) 7 | return 1; 8 | return 0; 9 | } 10 | if(A[0]-'0'>0 &&A[0]-'0'<=9) 11 | q=1; 12 | else return 0; 13 | if(A[0]=='1'){ 14 | if(A[1]-'0'>0 && A[1]-'0'<=9) 15 | p=2; 16 | else p=1; 17 | } 18 | else if(A[0]=='2'){ 19 | if(A[1]-'0'>0 && A[1]-'0'<=6) 20 | p=2; 21 | else p=1; 22 | }else p=1; 23 | 24 | if(n==2) 25 | return p; 26 | i=2; 27 | while(i=1 && A[i]-'0'<=9){ 34 | if(A[i-1]=='1' ||(A[i-1]=='2' && A[i]-'0'<=6) ) 35 | ans=p+q; 36 | else ans=p; 37 | q=p; 38 | p=ans; 39 | } 40 | i++; 41 | } 42 | return ans; 43 | } 44 | -------------------------------------------------------------------------------- /InterviewBit/String/SortedPermutationRank.cpp: -------------------------------------------------------------------------------- 1 | long long fact[256]; 2 | struct s{ 3 | char a; 4 | bool seen; 5 | }; 6 | int get(s array[],char a,int n){ 7 | int x=0; 8 | for (int i=0;iarray[i].a){ 10 | x++; 11 | } 12 | if (array[i].a==a) 13 | array[i].seen=true; 14 | } 15 | 16 | return x; 17 | } 18 | int answer(string A){ 19 | 20 | 21 | long long ans=0; 22 | long long mod=1000003; 23 | s array[A.length()]; 24 | for (int i=0;i 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;anext)) 13 | return 1; 14 | ListNode *hare=A; 15 | ListNode *tort=A; 16 | ListNode *B=NULL; 17 | while(hare){ 18 | hare=hare->next; 19 | if(!hare){ 20 | tort=tort->next; 21 | break; 22 | } 23 | hare=hare->next; 24 | B=tort; 25 | tort=tort->next; 26 | } 27 | if(B) 28 | B->next=NULL; 29 | B=tort; 30 | ListNode *C=NULL; 31 | ListNode *D=B; 32 | ListNode *E=B; 33 | while(D){ 34 | E=D->next; 35 | D->next=C; 36 | C=D; 37 | D=E; 38 | } 39 | 40 | while(A || C){ 41 | if(!A || !C) 42 | return 0; 43 | if(A->val!=C->val) 44 | return 0; 45 | A=A->next; 46 | C=C->next; 47 | } 48 | return 1; 49 | } 50 | -------------------------------------------------------------------------------- /InterviewBit/Link-List/MERGE.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 | /* Merge Two Sorted Lists */ 10 | ListNode* Solution::mergeTwoLists(ListNode* A, ListNode* B) { 11 | if(!A) 12 | return B; 13 | if(!B) 14 | return A; 15 | ListNode *currentB=B; 16 | ListNode *currentA=A; 17 | ListNode *previousA; 18 | if(A->val > B->val){ 19 | B=currentB->next; 20 | currentB->next=A; 21 | A=currentB; 22 | currentB=B; 23 | currentA=A; 24 | } 25 | while(currentA && currentB){ 26 | if(currentA->val <= currentB->val){ 27 | previousA=currentA; 28 | currentA=currentA->next; 29 | }else{ 30 | previousA->next=B; 31 | B=currentB->next; 32 | currentB->next=currentA; 33 | previousA=currentB; 34 | currentB=B; 35 | } 36 | } 37 | if(!currentA) 38 | previousA->next=B; 39 | return A; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /InterviewBit/Stacks-And-Queues/RAIN.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 3 | */ 4 | int Solution::trap(const vector &A) { 5 | stack mystack; 6 | int temp=0; 7 | int ans=0; 8 | int i=1,j=0; 9 | mystack.push(A[0]); 10 | for(i=0;iA[j]) 12 | j=i; 13 | } 14 | i=0; 15 | while(i<=j){ 16 | if(A[i]>=mystack.top()){ 17 | ans+=temp; 18 | temp=0; 19 | mystack.pop(); 20 | mystack.push(A[i]); 21 | }else temp+=mystack.top()-A[i]; 22 | i++; 23 | } 24 | while(!mystack.empty()) mystack.pop(); 25 | mystack.push(A[A.size()-1]); 26 | i=A.size()-2; 27 | temp=0; 28 | while(i>=j){ 29 | if(A[i]>=mystack.top()){ 30 | ans+=temp; 31 | temp=0; 32 | mystack.pop(); 33 | mystack.push(A[i]); 34 | }else temp+=mystack.top()-A[i]; 35 | i--; 36 | } 37 | return ans; 38 | } 39 | -------------------------------------------------------------------------------- /Spoj/MKMONEY.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | double interestGreaterThanCent(double a) 4 | { 5 | int b=100*a; 6 | return (double)b/100; 7 | } 8 | double interest(double a,double b) 9 | { 10 | return interestGreaterThanCent((double)a*b/100); 11 | } 12 | 13 | void computeCompoundInterest(double a,double b,int c,int j,double d) 14 | { 15 | int n1=(int)a; 16 | double frac=a-n1; 17 | int m=a; 18 | double fraction=frac; 19 | double intrst; 20 | for(int i=0;i>a>>b>>c; 42 | if(a==0&&b==0&&c==0) 43 | break; 44 | d=(double)b/c; 45 | computeCompoundInterest(a,d,c,i,b); 46 | i++; 47 | } 48 | } 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /HackerEarth/MonkAndETF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define mod 1000000007 3 | #define ll long long 4 | 5 | using namespace std; 6 | ll arr[100000]; 7 | ll phi[100000]; 8 | 9 | double factors(ll l, ll r, int K){ 10 | ll N=r-l+1; 11 | ll i,j; 12 | for(i=0;i1) 33 | phi[(int)i]-=phi[(int)i]/arr[(int)i]; 34 | } 35 | 36 | double prob=0; 37 | for(int k=0;k0) 44 | c+='1'; 45 | reverseString(c); 46 | return c; 47 | } 48 | -------------------------------------------------------------------------------- /InterviewBit/Link-List/REORDERLIST.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 | /* 11 | *Given a singly linked list 12 | *L: L0 → L1 → … → Ln-1 → Ln, 13 | *reorder it to: 14 | *L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → … 15 | */ 16 | ListNode* Solution::reorderList(ListNode* A) { 17 | stack mystack; 18 | ListNode *current=A,*fwd; 19 | ListNode *half=A; 20 | bool yes=1; 21 | while(current){ 22 | current=current->next; 23 | if(yes){ 24 | half=half->next; 25 | yes=0; 26 | }else yes=1; 27 | } 28 | while(half){ 29 | mystack.push(half); 30 | half=half->next; 31 | } 32 | current=A; 33 | fwd=current->next; 34 | while(!mystack.empty()){ 35 | half=mystack.top(); 36 | current->next=half; 37 | half->next=fwd; 38 | current=fwd; 39 | fwd=fwd->next; 40 | mystack.pop(); 41 | } 42 | current->next=NULL; 43 | return A; 44 | } 45 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/sumOfTwoNumbersInArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write a C++ program that, given an array A[] of n numbers and another number x, 3 | *determines whether or not there exist two elements in S whose sum is exactly x. 4 | *time=O(nlogn) if array is not sorted else O(n) 5 | */ 6 | #include 7 | #include 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;abegin){ 16 | if(array[begin]+array[end]==x) 17 | return true; 18 | else if(array[begin]+array[end]>x) 19 | end--; //move end pointer backwards 20 | else begin++; //move begin pointer forward 21 | } 22 | return false; 23 | } 24 | int main(){ 25 | int n; // no of elements in given array 26 | int i; // loop variable 27 | IN(n); 28 | int *array=new int[n]; 29 | FOR(i,n) 30 | IN(array[i]); 31 | int x; //sum to find 32 | IN(x); 33 | if(function(array,n,x)) 34 | printf("SUM EXIST\n"); 35 | else printf("SUM DOESNT EXIST\n"); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Spoj/MAXNUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void pop(int &p) 8 | { 9 | if(p>0) 10 | p--; 11 | } 12 | void push( char m,char *a,int &p,int len) 13 | { 14 | if(p0) 27 | { 28 | if((int)x[i]<=(int)a[p-1]) 29 | { 30 | push(x[i],a,p,len-k); 31 | i++; 32 | } 33 | else if(len-i>len-k-p) 34 | pop(p); 35 | else 36 | { 37 | push(x[i],a,p,len-k); 38 | i++; 39 | } 40 | } 41 | 42 | if(p==0) 43 | { 44 | push(x[i],a,p,len-k); 45 | i++; 46 | } 47 | } 48 | 49 | } 50 | int main() 51 | { 52 | int t; 53 | cin>>t; 54 | while(t--) 55 | { 56 | string x; 57 | int k; 58 | cin>>x; 59 | scanf("%d",&k); 60 | char *a=new char[x.length()-k]; 61 | a[0]=x[0]; 62 | if(x.length()!=k) 63 | { 64 | mainmethod(k,x,a,x.length()); 65 | for(int i=0;i 2 | #define MAX 1001 3 | using namespace std; 4 | void initialize(double **A,int rows,int columns){ 5 | int i,j; 6 | for(i=0;i 2 | #define MAX 3000 3 | #define mod 100000007 4 | #define ll long long 5 | #define ini(x) scanf("%d",&x); 6 | #define inl(x) scanf("%lld",&x); 7 | #define ind(x) scanf("%lf",&x); 8 | #define type double 9 | using namespace std; 10 | vector prime; 11 | ll dp[7994][5]; 12 | void sieve(){ 13 | int i,j; 14 | bool p[10001]; 15 | for(i=2;i<10001;i++) 16 | p[i]=1; 17 | p[0]=p[1]=0; 18 | for(i=2;i*i<=10001;i++) 19 | if(p[i]) 20 | for(j=i*i;j<10001;j+=i) 21 | p[j]=0; 22 | prime.push_back(2); 23 | for(i=2;i<7994;i++) 24 | if(p[i] and i%4 ==1){ 25 | prime.push_back(i); 26 | //cout<=0) 37 | dp[i][j]=dp[i-j][j]; 38 | dp[i][j]+=dp[i][j-1]; 39 | } 40 | } 41 | } 42 | int main() 43 | { 44 | sieve();filldp(); 45 | int t; 46 | ini(t); 47 | while(t--){ 48 | int n,k; 49 | ini(n);ini(k); 50 | printf("%lld\n",dp[prime[n-1]][k]); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Spoj/LASTDIG.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | char a2[4]={'6','2','4','8'}; 6 | char a3[4]={'1','3','9','7'}; 7 | char a4[2]={'6','4'}; 8 | char a7[4]={'1','7','9','3'}; 9 | char a8[4]={'6','8','4','2'}; 10 | char a9[2]={'1','9'}; 11 | int t,z=0; 12 | cin>>t; 13 | while(z>a; 17 | cin>>b; 18 | int s; 19 | if(a<10) 20 | s=a; 21 | else s=a%10; 22 | if(b>0){ 23 | switch (s){ 24 | case 0: 25 | cout<<0; 26 | break; 27 | case 1: 28 | cout<<1; 29 | break; 30 | case 2: 31 | cout< 2 | #define mod 1000000007 3 | #define ll long long 4 | #define FOR(a,b) for(a=0;a>1); 9 | } 10 | int solve(ll n1,ll n2){ 11 | int i=0,j=0,k=0; 12 | while(n1>ten[i]) i++; 13 | while(n2>ten[j]) j++; 14 | ll ans=0,temp; 15 | if(i==j){ 16 | ans=(getSum(n2)-getSum(n1)+n1)*i; 17 | ans%=mod; 18 | return (int)ans; 19 | } 20 | temp=getSum(ten[i]-1)-getSum(n1)+n1; 21 | temp%=mod; 22 | temp*=i; 23 | temp%=mod; 24 | ans=(ans+temp)%mod; 25 | temp=getSum(n2)-getSum(ten[j-1])+ten[j-1]; 26 | temp%=mod; 27 | temp*=j; 28 | temp%=mod; 29 | ans=(ans+temp)%mod; 30 | for(k=i+1;k 2 | #define MAX 1001 3 | using namespace std; 4 | long long a[19][163][1459]; 5 | bool seen[19][163][1459]; 6 | int dig[19]; 7 | long long gcd(long long a,long long b){ 8 | if(b==0) 9 | return a; 10 | return gcd(b,a%b); 11 | } 12 | long long dp(int idx,int sum,int sqsum,bool small){ 13 | if(idx==-1){ 14 | if(gcd(sum,sqsum)==1) 15 | return 1; 16 | return 0; 17 | } 18 | if(small && a[idx][sum][sqsum]!=-1) 19 | return a[idx][sum][sqsum]; 20 | long long ans=0; 21 | int end; 22 | if(small){ 23 | end=9; 24 | }else end=dig[idx]; 25 | 26 | for(int i=0;i<=end;i++){ 27 | ans+=dp(idx-1,sum+i,sqsum+i*i,small || i0){ 36 | dig[len++]=N%10; 37 | N/=10; 38 | } 39 | return dp(len-1,0,0,0); 40 | } 41 | int main(){ 42 | memset(a,-1,sizeof(a)); 43 | int T; 44 | long long l; 45 | long long r; 46 | scanf("%d",&T); 47 | while(T--){ 48 | scanf("%lld%lld",&l,&r); 49 | printf("%lld\n",solve(r)-solve(l-1)); 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /InterviewBit/BinarySearch/SEARCH2D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write an efficient algorithm that searches for a value in an m x n matrix. 3 | *This matrix has the following properties 4 | *Integers in each row are sorted from left to right. 5 | *The first integer of each row is greater than or equal to the last integer of the previous row. 6 | */ 7 | 8 | int Solution::searchMatrix(vector > &A, int B) { 9 | int m=A.size(); 10 | int n=A[0].size(); 11 | int searchRow=-1; 12 | int l=0,r=n-1,mid; 13 | int i,j; 14 | if(B A[m-1][n-1]) 15 | return 0; 16 | l=0; r=m-1; 17 | /* search the fittest row */ 18 | while(l<=r){ 19 | mid=(l+r)>>1; 20 | if(A[mid][0]==B) 21 | return 1; 22 | else if(A[mid][0]>1; 31 | if(A[searchRow][mid]==B) 32 | return 1; 33 | else if(A[searchRow][mid]> &v, vector &row, 15 | TreeNode *current,int sum,int B){ 16 | if(!current) 17 | return; 18 | if(!current->left && !current->right){ 19 | sum+=current->val; 20 | if(sum==B){ 21 | row.push_back(current->val); 22 | v.push_back(row); 23 | row.pop_back(); 24 | } 25 | return; 26 | } 27 | sum+=current->val; 28 | row.push_back(current->val); 29 | findPaths(v,row,current->left,sum,B); 30 | findPaths(v,row,current->right,sum,B); 31 | row.pop_back(); 32 | } 33 | vector > Solution::pathSum(TreeNode* root, int sum) { 34 | vector> v; 35 | vector row; 36 | findPaths(v,row,root,0,sum); 37 | return v; 38 | } 39 | -------------------------------------------------------------------------------- /Codechef/BANROB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #define MAX 1000000 8 | #define ll long long 9 | #define FOR(i,a,n) for(i=a;i>M>>p; 27 | i=0; 28 | while(maxVal>= 0.001 && i=0;i--){ 40 | if(i%2==0){ 41 | A[i].frnd=A[i+1].frnd; 42 | A[i].chef=A[i].val-A[i].frnd; 43 | }else{ 44 | A[i].chef=A[i+1].chef; 45 | A[i].frnd=A[i].val-A[i].chef; 46 | } 47 | } 48 | printf("%0.3f %0.3f\n",A[0].chef,A[0].frnd); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Algorithms/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write a program for mergesort 3 | *Time=O(nlog(n)) & space O(n) 4 | */ 5 | #include 6 | #define IN(a) scanf("%d",&a) 7 | #define FOR(a,b) for(a=0;a>1; 38 | if(a==b) 39 | return; 40 | split(a,mid); 41 | split(mid+1,b); 42 | merge(a,mid,b); 43 | 44 | } 45 | int main(){ 46 | int i; // loop variable 47 | int n; // no of elements in given array 48 | IN(n); 49 | array=new int[n]; 50 | FOR(i,n) 51 | IN(array[i]); 52 | split(0,n-1); 53 | FOR(i,n) 54 | printf("%d " ,array[i]); 55 | printf("\n"); 56 | } 57 | -------------------------------------------------------------------------------- /HackerEarth/HasanAndTrip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 3000 3 | #define mod 100000007 4 | #define ll long long 5 | #define ini(x) scanf("%d",&x); 6 | #define inl(x) scanf("%lld",&x); 7 | #define ind(x) scanf("%lf",&x); 8 | #define type double 9 | using namespace std; 10 | double inf=-1000000000; 11 | 12 | struct Point{ 13 | double x,y; 14 | }; 15 | 16 | double dist(Point p1, Point p2){ 17 | double x=p1.x-p2.x; 18 | x*=x; 19 | double y=p1.y-p2.y; 20 | y*=y; 21 | double distan= x+y; 22 | distan=sqrt(distan); 23 | return distan; 24 | } 25 | 26 | 27 | double solve(int N, double happiness[], Point points[]){ 28 | int i,j,k; 29 | double dp[N]; 30 | dp[N-1]=happiness[N-1]; 31 | for(i=N-2;i>=0;i--){ 32 | dp[i]=inf; 33 | for(j=i+1;j 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | vector temp; 7 | vector v(100001,0); 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,k; 12 | scanf("%d%d",&n,&k); 13 | if(k>n/2) 14 | { 15 | printf("-1\n"); 16 | } 17 | else if(k==0) 18 | { 19 | for(int i=1;i<=n;i++) 20 | printf("%d ",i); 21 | printf("\n"); 22 | } 23 | else 24 | { 25 | for(int i=1;i<=k;i++) 26 | { 27 | for(int j=i;j<=n;j+=k) 28 | temp.push_back(j); 29 | for(int j=0;j=1) 33 | swap(temp[temp.size()-1],temp[temp.size()-2]); 34 | int l=i; 35 | for(int j=0;j 8 | #include 9 | #include 10 | #define IN(a) scanf("%d",&a) 11 | #define FOR(a,b) for(a=0;a0){ 19 | ei=array[i]-1; 20 | if(array[ei]>0){ 21 | array[i]=array[ei]; 22 | array[ei]=-1; 23 | } 24 | else { 25 | array[ei]+=-1; 26 | i++; 27 | } 28 | }else i++; 29 | } 30 | FOR(i,n){ 31 | if(array[i]>0) 32 | array[i]=0; 33 | else array[i]*=-1; 34 | } 35 | } 36 | int main(){ 37 | int i; // loop variable 38 | int n; // no of elements in given array 39 | IN(n); 40 | array=new int[n]; 41 | FOR(i,n) 42 | IN(array[i]); 43 | frequency(n); 44 | FOR(i,n) 45 | printf("%d ",array[i]); 46 | printf("\n"); 47 | } 48 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/rotateArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Write a program to rotate an array by d elements 3 | *In this implementation we rotate counter-clockwise and use juggling algo 4 | *Time=O(n) space O(1) 5 | */ 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b) for(a=0;a0){ 14 | y=a%b; a=b; b=y; 15 | } 16 | return a; 17 | } 18 | void rotate(int *array,int n,int d){ 19 | int i,j; //loop variable 20 | int set=gcd(n,d); //gives number of sets 21 | int iterations=n/set; //gives no of elements in a set 22 | int temp; 23 | FOR(i,set){ //for each set 24 | temp=array[i]; 25 | FOR(j,iterations-1) 26 | array[(i+j*d)%n]=array[(i+j*d+d)%n]; //swap elements 27 | array[(i+(iterations-1)*d)%n]=temp; 28 | } 29 | } 30 | int main(){ 31 | int i; // loop variable 32 | int n; // no of elements in given array 33 | int d; // number of elements to rotate by 34 | IN(n); 35 | int *array=new int[n]; 36 | FOR(i,n) 37 | IN(array[i]); 38 | IN(d); 39 | rotate(array,n,d); 40 | FOR(i,n) 41 | printf("%d ",array[i]); 42 | printf("\n"); 43 | } 44 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/checkConsecutiveElements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *Given an unsorted array of numbers, write a function that returns true if array consists of consecutive numbers. 3 | *Time=O(n) & space O(n) 4 | */ 5 | #include 6 | #include 7 | #define IN(a) scanf("%d",&a) 8 | #define FOR(a,b) for(a=0;a mystack; 12 | bool check(int *array,int n){ 13 | int i; //loop variable 14 | int max=array[0],min=array[0]; 15 | FOR(i,n){ 16 | if(maxarray[i]) 19 | min=array[i]; 20 | } 21 | if(max-min+1!=n) 22 | return false; 23 | bool visited[n]; //All elements should be distinct 24 | FOR(i,n) 25 | visited[i]=0; 26 | FOR(i,n) 27 | visited[array[i]-min]=1; 28 | FOR(i,n) 29 | if(!visited[i]) 30 | return false; 31 | return true; 32 | 33 | } 34 | int main(){ 35 | int i; // loop variable 36 | int n; // no of elements in given array 37 | IN(n); 38 | int *array=new int[n]; 39 | FOR(i,n) 40 | IN(array[i]); 41 | if(check(array,n)) 42 | printf("All elements are consecutive\n"); 43 | else printf("All elements are not consecutive\n"); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /GeeksForGeeks/Array/twoElementsWhoseSumIsClosestToZero.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | *An Array of integers is given, both +ve and -ve. 3 | *You need to find the two elements such that their sum is closest to zero 4 | *Time=O(nlog(n)) & space O(1) 5 | */ 6 | #include 7 | #include 8 | #define IN(a) scanf("%d",&a) 9 | #define FOR(a,b) for(a=0;a0) 36 | r--; 37 | else l++; 38 | } 39 | printf("%d %d ",array[a],array[b]); 40 | return min; 41 | } 42 | int main(){ 43 | int i; // loop variable 44 | int n; // no of elements in given array 45 | IN(n); 46 | array=new int[n]; 47 | FOR(i,n) 48 | IN(array[i]); 49 | printf("%d\n",function(n)); 50 | } 51 | -------------------------------------------------------------------------------- /InterviewBit/String/LONGESTPALIN.cpp: -------------------------------------------------------------------------------- 1 | /* Given a string S, find the longest palindromic substring in S. */ 2 | string Solution::longestPalindrome(string A) { 3 | int i,j; 4 | int n=A.length(); 5 | if(n==1) 6 | return A; 7 | string B=""; 8 | int p,q,l=0,start,end; 9 | for(i=0;i=0 && q l){ 13 | l=q-p+1; 14 | start=p; 15 | end=q; 16 | } 17 | p--;q++; 18 | } 19 | if(B.length()=0 && q l){ 28 | l=q-p+1; 29 | start=p; 30 | end=q; 31 | } 32 | p--;q++; 33 | } 34 | if(B.length() 2 | #define IN(L) scanf("%d",&L) 3 | #define MAX 1000001 4 | using namespace std; 5 | int main(){ 6 | int i,j; 7 | int cubes[19]; 8 | for(i=0;i<19;i++) 9 | cubes[i]=i*i*i; 10 | 11 | int a[500][19]; 12 | int b[500]; 13 | int Total[4001]; 14 | int c[4001][500]; 15 | memset(c,0,sizeof(c)); 16 | int N,T; 17 | IN(T); 18 | while(T--){ 19 | IN(N); 20 | for(i=0;i=0 && a[i][j]<4001){ 39 | ans+=Total[a[i][j]]-c[a[i][j]][i]; 40 | } 41 | } 42 | } 43 | ans=ans/3; 44 | long long t=N*(N-1)*(N-2); 45 | t/=6; 46 | 47 | double prob=(double)ans/(double)t; 48 | printf("%0.9lf\n",prob); 49 | memset(Total,0,sizeof(Total)); 50 | for(i=0;i