├── .gitignore ├── A Very Big Sum.cpp ├── Compare the Triplets.cpp ├── LICENSE ├── Mini-Max Sum.cpp ├── Plus Minus.cpp ├── README.md ├── README.mdmaximizing-xor.c ├── README2.md ├── Simple Array Sum.cpp ├── Solve Me First.cpp ├── Staircase.cpp ├── a-very-big-sum.cpp ├── algorithmic-crush.cpp ├── almost-sorted.cpp ├── alternating-characters.c ├── anagram.c ├── angry-professor.c ├── balanced-contest-or-not.cpp ├── bday-gift.c ├── bear-and-workbook.cpp ├── bigger-is-greater.c ├── bitwise-product.cpp ├── candies.cpp ├── cavity-map.c ├── chocolate-feast.c ├── closest-numbers.c ├── coin-change.cpp ├── common-child.cpp ├── connecting-towns.c ├── correctness-and-the-loop-invariant.c ├── count-the-divisors.c ├── counter-game.cpp ├── counting-sort-1.cpp ├── counting-sort-2.cpp ├── counting-sort-3.cpp ├── cut-the-sticks.c ├── detecting-valid-latitude-and-longitude-pairs-key-line.c ├── detecting-valid-latitude-and-longitude-pairs.c ├── diagonal-difference.cpp ├── dijkstrashortreach.cpp ├── divisible-sum-pairs.cpp ├── diwali-lights.c ├── encryption.c ├── equal-stacks.cpp ├── equal.cpp ├── extra-long-factorials.py ├── fibonacci-modified.py ├── filling-jars.c ├── find-digits.c ├── find-hackerrank.c ├── find-median.c ├── find-point.c ├── fixing-bubble-sort.cpp ├── fizzbuzz.c ├── flipping-bits.c ├── flowers.c ├── floyd-city-of-blinding-lights.cpp ├── full-counting-sort.cpp ├── funny-string.cpp ├── game-of-thrones.cpp ├── gemstones.c ├── grid-challenge.c ├── halloween-party.c ├── handshake.c ├── icecream-parlor.c ├── insertionsort-part1.c ├── insertionsort-part2.c ├── introduction-to-tutorial-challenges.c ├── is-fibo.c ├── journey-to-the-moon.cpp ├── kangaroo.cpp ├── larrys-array.cpp ├── lonely-integer-fill-the-key-line.c ├── lonely-integer.c ├── make-it-anagram.c ├── manasa-and-stones.c ├── mark-and-toys.c ├── max-min.c ├── maximise-sum.cpp ├── minimum-distances.cpp ├── minimum-draws.c ├── missing-numbers.c ├── modified-kaprekar-number.c ├── new-year-chaos.cpp ├── new-year-party.cpp ├── newyear-game.cpp ├── pairs.cpp ├── palindrome-index.c ├── play-game.cpp ├── plus-minus.cpp ├── polynomial-and-its-roots.cpp ├── primsmstsub.cpp ├── priyanka-and-toys.c ├── quicksort1.cpp ├── quicksort3.cpp ├── quicksort4.cpp ├── red-john-is-back.cpp ├── restaurant.c ├── reverse-game.c ├── run-length-encoding.cpp ├── running-time-of-algorithms.c ├── sam-and-substrings.cpp ├── sansa-and-xor.cpp ├── saying-hi.c ├── service-lane.c ├── sherlock-and-array.c ├── sherlock-and-divisors.cpp ├── sherlock-and-minimax.cpp ├── sherlock-and-pairs.cpp ├── sherlock-and-planes.c ├── sherlock-and-squares.c ├── sherlock-and-the-beast.c ├── sherlock-and-valid-string.cpp ├── snakes-and-ladders.cpp ├── solve-me-first.c ├── solve-me-second.c ├── song-of-pi.c ├── stack-overflow-revisited.c ├── staircase.cpp ├── strange-code.cpp ├── strange-grid.c ├── string-reduction.cpp ├── string-similarity.cpp ├── taum-and-bday.c ├── the-chosen-one.cpp ├── the-grid-search.cpp ├── the-longest-common-subsequence.cpp ├── the-longest-increasing-subsequence.cpp ├── the-love-letter-mystery.c ├── the-maximum-subarray.c ├── three-loops.cpp ├── time-conversion.cpp ├── trignometric-ratios.c ├── two-arrays.cpp ├── unbounded-knapsack.cpp ├── utopian-tree.c └── valid-pan-format.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | -------------------------------------------------------------------------------- /A Very Big Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() { 9 | long long int i,sum=0; 10 | long long int a[10],n; 11 | scanf("%lld", &n); 12 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | int a[100],b[100],i,j=0,k=0; 11 | for(i=0;i<3;i++) 12 | { scanf("%d",&a[i]); 13 | } 14 | for(i=0;i<3;i++) 15 | {scanf("%d",&b[i]); 16 | } 17 | for(i=0;i<3;i++) 18 | { if(a[i]>b[i]) 19 | {j++; 20 | } 21 | else if(a[i] 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /Mini-Max Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main(){ 9 | long int a,b, c, d; 10 | long int e,max,min,ma=0,mi=0,sum_max=0,sum_min=0; 11 | long int no[5]; 12 | int i; 13 | scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e); 14 | no[0]=a; 15 | no[1]=b; 16 | no[2]=c; 17 | no[3]=d; 18 | no[4]=e; 19 | max=a; 20 | min=a; 21 | for(i=1;i<5;i++) 22 | { 23 | if(no[i]>max) 24 | { 25 | max=no[i]; 26 | ma=i; 27 | } 28 | if(no[i] 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | void plusMinus(int arr_size, int* arr) { 10 | // Complete this function 11 | } 12 | 13 | int main() { 14 | int n; 15 | scanf("%i", &n); 16 | int *arr = malloc(sizeof(int) * n); 17 | for (int arr_i = 0; arr_i < n; arr_i++) { 18 | scanf("%i",&arr[arr_i]); 19 | } 20 | plusMinus(n, arr); 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hackerranksolutions 2 | 3 | This repo contains HackerRank algorithmic solutions. 4 | 5 | All the programs are written in C or C++. 6 | 7 | ### star, fork, contribute :) 8 | 9 | 10 | #### My hackerrank profile : [msdeep14](https://www.hackerrank.com/msdeep14) 11 | -------------------------------------------------------------------------------- /README.mdmaximizing-xor.c: -------------------------------------------------------------------------------- 1 | //maximizing-xor 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int maxXor(int l, int r) { 9 | int ans = 0 ,i,j,temp=0; 10 | for(i=l;i<=r;i++) 11 | { 12 | for(j=i;j<=r;j++) 13 | { 14 | temp = (i ^j); 15 | if(ans 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() { 10 | int a[1000],n,i,sum=0; 11 | scanf("%d",&n); 12 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | int c; 7 | int solveMeFirst(int a, int b) { 8 | c=a+b; 9 | return c; 10 | 11 | } 12 | int main() { 13 | int num1,num2; 14 | scanf("%d %d",&num1,&num2); 15 | int sum; 16 | sum = solveMeFirst(num1,num2); 17 | printf("%d",sum); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Staircase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | int main() { 9 | int n,i,j; 10 | 11 | scanf("%d",&n); 12 | for(i=1;i<=n;++i) 13 | { 14 | for(j=1;j<=n;++j) 15 | {if(j<=(n-i)) 16 | printf(" "); 17 | else 18 | printf("#"); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /a-very-big-sum.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/a-very-big-sum 2 | //mandeep singh @msdeep14 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | long long int i,n,arr[20],sum=0; 8 | cin>>n; 9 | for(i=0;i>arr[i]; 11 | for(i=0;i 3 | using namespace std; 4 | long long int arr[10000005]; 5 | int main() 6 | { 7 | long long int n,m,a,b,k; 8 | cin>>n>>m; 9 | for(int i=0;i>a>>b>>k; 11 | arr[a]+=k; 12 | if(b+1<=n) arr[b+1]-=k; 13 | } 14 | long long int ans=0,max=0; 15 | 16 | for(int i=1;i<=n;i++){ 17 | ans=ans+arr[i]; 18 | if(ans>max) max=ans; 19 | } 20 | cout< 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n,l=1,r,i; 7 | int arr[100005]; 8 | cin>>n; 9 | for(i=1;i<=n;i++){ 10 | cin>>arr[i]; 11 | } 12 | int store[3]; 13 | int count=0; 14 | int x=0; 15 | int flag1=0; 16 | int flag2=0; 17 | for(i=1;iarr[i+1]){ 19 | count++; 20 | } 21 | if(count==1 && flag1==0){ 22 | store[x++]=i; 23 | flag1=1;} 24 | else if(count==2 && flag2==0) {store[x++]=i+1; 25 | flag2=1;} 26 | if(count>2) break; 27 | } 28 | int flag=0; 29 | if(count==2){ 30 | swap(arr[store[0]],arr[store[1]]); 31 | flag=1; 32 | } 33 | if(flag==0){ 34 | for(i=1;iarr[i+1]){ 36 | l=i; 37 | break; 38 | } 39 | } 40 | for(i=l;iarr[i+1]) break; 51 | } 52 | if(i!=n) cout<<"no"; 53 | else if(l==r) cout<<"yes"; 54 | else if(l==r-1) cout<<"yes\nswap "< 3 | #include 4 | int main() 5 | { 6 | int i,j,n,count=0,flag=0,t; 7 | char str[100000]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | count=0; 12 | scanf("%s",str); 13 | n=strlen(str); 14 | for(i=0;i 4 | #include 5 | int main() 6 | { 7 | int i,t,j,k,n,flag=0,n1,n2,count=0; 8 | char str[10000],str1[10000],str2[10000]; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | count=0; 13 | flag=0; 14 | scanf("%s",str); 15 | fflush(stdin); 16 | n=strlen(str); 17 | if(n%2!=0) 18 | { 19 | printf("-1\n"); 20 | } 21 | else 22 | { 23 | j=0; 24 | for(i=0;i<(n/2);i++) 25 | { 26 | str1[j]=str[i]; 27 | j++; 28 | } 29 | n1=strlen(str1); 30 | j=0; 31 | for(i=(n/2);i 3 | int main() 4 | { 5 | int i,j,t,n,k,a,count=0; 6 | scanf("%d",&t); 7 | while(t--) 8 | { 9 | count=0; 10 | scanf("%d%d",&n,&k); 11 | for(i=0;i 4 | using namespace std; 5 | void insertion(int a[], int s) 6 | { 7 | int i, j; 8 | int current; 9 | 10 | for(i = 1; i< s; i++) 11 | { 12 | current = a[i]; 13 | for(j = i-1; j>=0; j--) 14 | { 15 | if(current < a[j]) 16 | a[j+1] = a[j]; 17 | else 18 | break; 19 | } 20 | a[j+1] = current; 21 | } 22 | } 23 | int main() 24 | { 25 | int i,j,n,k,l,val1[5],val2[5],flag; 26 | int b[5],d[5],b1[5],d1[5]; 27 | for(i=0;i<5;i++) 28 | { 29 | cin>>b[i]; 30 | cin>>d[i]; 31 | } 32 | for(i=0;i<5;i++) 33 | { 34 | b1[i]=b[i]; 35 | d1[i]=d[i]; 36 | } 37 | insertion(b1,5); 38 | insertion(d1,5); 39 | //check values 40 | k=0; 41 | for(i=0;i<5;i++) 42 | { 43 | for(j=0;j<5;j++) 44 | { 45 | if(b1[i]==b[j]) 46 | { 47 | val1[k]=j; 48 | k++; 49 | break; 50 | } 51 | } 52 | } 53 | k=0; 54 | for(i=0;i<5;i++) 55 | { 56 | for(j=0;j<5;j++) 57 | { 58 | if(d1[i]==d[j]) 59 | { 60 | val2[k]=j; 61 | k++; 62 | break; 63 | } 64 | } 65 | } 66 | for(i=0;i<5;i++) 67 | { 68 | flag=0; 69 | if(val1[i]==val2[i]) 70 | { 71 | //do nothing; 72 | flag=1; 73 | } 74 | else 75 | break; 76 | } 77 | if(flag==1) 78 | cout<<"1"; 79 | else 80 | cout<<"0"; 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /bday-gift.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/bday-gift 2 | //mandeep singh @msdeep14 3 | #include 4 | int main() 5 | { 6 | int i,j,k,n,sum=0; 7 | scanf("%d",&n); 8 | for(i=0;i 3 | using namespace std; 4 | int main(){ 5 | int n,k; 6 | int t[105]; 7 | cin>>n>>k; 8 | for(int i=0;i>t[i]; 9 | int prev=0,temp=0,j=0,page=1; 10 | int sp=0; 11 | for(int i=0;iprev && page<=j) sp++; 19 | } 20 | else if(temp>k){ 21 | j+=k; 22 | if(page>prev && page<=j) sp++; 23 | prev+=k; 24 | temp-=k; 25 | } 26 | page++; 27 | } 28 | } 29 | cout< 3 | #include 4 | #include 5 | 6 | int biggerisgreater(char arr[]) 7 | { 8 | int length,j,i; 9 | char temp; 10 | length = strlen(arr); 11 | if (length == 0) 12 | { 13 | printf("no answer\n"); 14 | return 0; 15 | } 16 | 17 | i = length - 1; 18 | while (i > 0 && arr[i - 1] >= arr[i]) 19 | i--; 20 | if (i == 0) 21 | { 22 | printf("no answer\n"); 23 | return 0; 24 | } 25 | j = length - 1; 26 | 27 | while (arr[j] <= arr[i - 1]) 28 | j--; 29 | 30 | temp = arr[i - 1]; 31 | arr[i - 1] = arr[j]; 32 | arr[j] = temp; 33 | j = length - 1; 34 | 35 | while (i < j) 36 | { 37 | temp = arr[i]; 38 | arr[i] = arr[j]; 39 | arr[j] = temp; 40 | i++; 41 | j--; 42 | } 43 | printf("%s\n",arr); 44 | return 1; 45 | } 46 | int main() 47 | { 48 | long int t; 49 | scanf("%ld",&t); 50 | char *str; 51 | str=(char*)malloc(t*sizeof(char)); 52 | for(int i=0;i 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | typedef unsigned int uint; 10 | 11 | int t; 12 | uint l, r; 13 | 14 | int main() { 15 | scanf("%d", &t); 16 | while (t--) { 17 | cin >> l >> r; 18 | uint res = 0; 19 | for (uint i = 0; i < 32; i++) { 20 | if (r - l + 1 == 1 && (l & 1)) 21 | res=res+pow(2,i); 22 | l >>= 1; r >>= 1; 23 | } 24 | cout << res << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /candies.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/candies 2 | //mandeep singh @msdeep14 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int n; 8 | long long int sum=0; 9 | int arr[100000]; 10 | int brr[100000]; 11 | cin>>n; 12 | for(int i=0;i>arr[i]; 15 | brr[i]=1; 16 | } 17 | sum=n; 18 | long long int temp; 19 | for(int i=0;i<(n-1);i++) 20 | { 21 | if(arr[i+1]>arr[i]) 22 | { 23 | temp=brr[i+1]; 24 | brr[i+1]=brr[i] + 1; 25 | sum+=brr[i+1]-temp; 26 | } 27 | } 28 | for(int i=n-1;i>0;i--) 29 | { 30 | if(arr[i-1]>arr[i] && brr[i-1]<=brr[i]) 31 | { 32 | temp=brr[i-1]; 33 | brr[i-1]= brr[i]+1; 34 | sum+=brr[i-1]-temp; 35 | } 36 | } 37 | cout< 4 | #include 5 | #include 6 | #include 7 | 8 | int main(){ 9 | int N; // size of map N x N 10 | char map[100][100] ; 11 | int i, j; 12 | 13 | scanf("%d", &N); 14 | for (i = 0; i < N; i++) 15 | scanf("%s", &map[i]); 16 | for (i = 0; i < N; i++) 17 | { 18 | for (j = 0; j < N; j++) 19 | { 20 | if (i == 0 || j == 0 || i == N - 1 || j == N - 1) 21 | printf("%c", map[i][j]); 22 | else if (map[i][j] > map[i - 1][j] && map[i][j] > map[i][j - 1] && map[i][j] > map[i][j + 1] && map[i][j] > map[i + 1][j]) 23 | printf("X"); 24 | else 25 | printf("%c", map[i][j]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /chocolate-feast.c: -------------------------------------------------------------------------------- 1 | //chocolate-feast 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | 9 | int t, n, c, m; 10 | scanf("%d", &t); 11 | while ( t-- ) 12 | { 13 | scanf("%d%d%d",&n,&c,&m); 14 | int answer = 0; 15 | answer=n/c; 16 | if(answer>=m) 17 | { 18 | int temp=answer; 19 | do{ 20 | temp-=m; 21 | answer++; 22 | temp++; 23 | }while(temp>=m); 24 | } 25 | 26 | printf("%d\n",answer); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /closest-numbers.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/closest-numbers 2 | //mandeep singh @msdeep14 3 | #include 4 | void insertion(int a[], int s) 5 | { 6 | int i, j; 7 | int current; 8 | 9 | for(i = 1; i< s; i++) 10 | { 11 | current = a[i]; 12 | for(j = i-1; j>=0; j--) 13 | { 14 | if(current < a[j]) 15 | a[j+1] = a[j]; 16 | else 17 | break; 18 | } 19 | a[j+1] = current; 20 | } 21 | } 22 | int main() 23 | { 24 | int i,j,n,flag=0,min; 25 | int arr[100000]; 26 | scanf("%d",&n); 27 | for(i=0;iarr[i+1]-arr[i]) 34 | { 35 | min=arr[i+1]-arr[i]; 36 | } 37 | } 38 | //we got the min difference now in the whole arr; 39 | for(i=0;i<(n-1);i++) 40 | { 41 | if(min==arr[i+1]-arr[i]) 42 | { 43 | printf("%d %d ",arr[i],arr[i+1]); 44 | } 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /coin-change.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/coin-change 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | /* 7 | int count(int s[],int m,int n) 8 | { 9 | if(n==0) 10 | return 1; 11 | if(n<0) 12 | return 0; 13 | if(m<=0 && n>=1) 14 | return 0; 15 | return count(s,m-1,n)+count(s,m,n-s[m-1]); 16 | } 17 | */ 18 | 19 | /* 20 | int count(int s[],int m,int n) 21 | { 22 | int table[n+1]; 23 | memset(table,0,sizeof(table)); 24 | table[0]=1; 25 | for(int i=0;i=0)?table[i-s[j]][j]: 0; 47 | y=(j>=1)?table[i][j-1]: 0; 48 | table[i][j]=x+y; 49 | } 50 | } 51 | return table[n][m-1]; 52 | } 53 | 54 | int main() 55 | { 56 | long long int m,n; 57 | long long int arr[10000]; 58 | cin>>n>>m; 59 | for(int i=0;i>arr[i]; 61 | long long int x=count(arr,m,n); 62 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | static long long int dp[5005][5005]; 13 | string A,B; 14 | cin>>A>>B; 15 | int N; 16 | N=A.length(); 17 | 18 | for (int i = 0; i <= N; ++i) 19 | dp[0][i] = dp[i][0] = 0; 20 | for (int i = 1; i <= N; ++i) 21 | for (int j = 1; j <= N; ++j) { 22 | dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 23 | if (A[i-1] == B[j-1]) 24 | dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1); 25 | } 26 | 27 | int answer = dp[N][N]; 28 | cout< 4 | int main() 5 | { 6 | long long int i,a,j,k,t,n; 7 | scanf("%lld",&t); 8 | while(t--) 9 | { 10 | j=1; 11 | scanf("%lld",&n); 12 | for(i=0;i<(n-1);i++) 13 | { scanf("%lld",&a); 14 | j=(j*a)%1234567; 15 | } 16 | printf("%lld\n",j); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /correctness-and-the-loop-invariant.c: -------------------------------------------------------------------------------- 1 | //correctness-and-the-loop-invariant 2 | #include 3 | int main() 4 | { 5 | int i,j,current,s,k; 6 | int arr[1010]; 7 | scanf("%d",&s); 8 | for(i=0;i=0; j--) 14 | { 15 | if(current < arr[j]) 16 | arr[j+1] = arr[j]; 17 | else 18 | break;//stop the loop 19 | } 20 | arr[j+1] = current;//insertion 21 | 22 | } 23 | for(k=0;k 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | int n, other; 9 | 10 | scanf("%d", &n); 11 | 12 | int result = 0; 13 | int j = 2; 14 | 15 | while(j <= n){ 16 | if(n%j == 0){ 17 | result = result + 1; 18 | other = n/j; 19 | result = result; 20 | } 21 | j++; 22 | } 23 | 24 | printf("%d", result+1); 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /counter-game.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/counter-game 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | unsigned long long int t,i,j,mov,count,temp,num,res,rem,flag=0; 9 | cin>>t; 10 | while(t--) 11 | { 12 | cin>>num; 13 | mov=1; 14 | while(num!=1) 15 | { 16 | flag=0; 17 | //if n is power of 2; 18 | /*if(((double)log(num)/(double)log(2))%2==0) 19 | { 20 | num=num-num/2; 21 | } 22 | */ 23 | // 24 | temp=num; 25 | while(temp>1) 26 | { 27 | rem=temp%2; 28 | temp=temp/2; 29 | if(rem!=0) 30 | { 31 | flag=1; 32 | break; 33 | } 34 | } 35 | if(flag==0) 36 | { 37 | num=num/2; 38 | } 39 | else 40 | { 41 | temp=num; 42 | count=0; 43 | while(temp>0) 44 | { 45 | temp=temp/2; 46 | count++; 47 | } 48 | //calculate power of power(2,count-1); 49 | res=pow(2,count-1); 50 | num=num-res; 51 | } 52 | if(num==1) 53 | { 54 | if(mov%2==0) 55 | cout<<"Richard\n"; 56 | else 57 | cout<<"Louise\n"; 58 | } 59 | mov++; 60 | } 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /counting-sort-1.cpp: -------------------------------------------------------------------------------- 1 | //mandeep singh @msdeep14 2 | //counting sort 1 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int i,j,n; 8 | int arrcount[100]; 9 | cin>>n; 10 | for(i=0;i<100;i++) 11 | { 12 | arrcount[i]=0; 13 | } 14 | int num; 15 | for(i=0;i>num; 18 | arrcount[num]++; 19 | } 20 | for(i=0;i<100;i++) 21 | { 22 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int i,j,n; 8 | int arrcount[100]; 9 | cin>>n; 10 | for(i=0;i<100;i++) 11 | { 12 | arrcount[i]=0; 13 | } 14 | int num; 15 | for(i=0;i>num; 18 | arrcount[num]++; 19 | } 20 | for(i=0;i<100;i++) 21 | { 22 | for(j=0;j 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n,x,sum=0,i,j; 9 | int arr[100]; 10 | char str[10]; 11 | for(i=0;i<100;i++) 12 | { 13 | arr[i]=0; 14 | } 15 | cin>>n; 16 | for(i=0;i>x; 19 | cin>>str; 20 | arr[x]++; 21 | 22 | } 23 | for(i=0;i<100;i++) 24 | { 25 | sum=sum+arr[i]; 26 | cout< 3 | int main() 4 | { 5 | unsigned int i,j,num,count=0,max,lim,min,k,m; 6 | unsigned int arr[1010], brr[1010]; 7 | 8 | scanf("%u",&num); 9 | 10 | //need to calculate total no. of cases 11 | //for that find the max no. among all array elements 12 | if(num==1000) 13 | { 14 | 15 | printf("1000"); 16 | } 17 | else 18 | { 19 | for(i=0;i1) 54 | { 55 | arr[i]=arr[i]-min; 56 | count++; 57 | } 58 | else if(arr[i]==1) 59 | { 60 | arr[i]=0; 61 | count++; 62 | } 63 | } 64 | brr[j]=count; 65 | } 66 | 67 | for(i=0;i 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | 9 | char x[100],y[100]; 10 | int t,i,flag; 11 | scanf("%d",&t); 12 | while(t) 13 | { 14 | flag=1; 15 | //****FILL THE MISSING LINE HERE**** 16 | scanf("%s",x); 17 | scanf("%s",y); 18 | 19 | int xlen=strlen(x); 20 | int ylen=strlen(y); 21 | 22 | if(x[1] == ' ' || y[0] == ' ' || x[xlen-2]== '.' || y[ylen-2]=='.' || y[ylen-2]==' ' || x[0]!= '(' || y[ylen-1]!= ')' || x[xlen-1]!= ',') 23 | flag=0; 24 | else if(x[1] == '+' || x[1] == '-') 25 | if(x[2]=='0' && x[3]!='.') 26 | flag=0; 27 | else if(y[0] == '+' || y[0] == '-') 28 | if(y[1]=='0' && y[2]!='.') 29 | flag=0; 30 | 31 | if(flag){ 32 | for(i=0;i90 || x1<-90) 39 | printf("Invalid\n"); 40 | else if(y1>180 || y1<-180) 41 | printf("Invalid\n"); 42 | else 43 | printf("Valid\n"); 44 | } 45 | else 46 | printf("Invalid\n"); 47 | 48 | t--; 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /detecting-valid-latitude-and-longitude-pairs.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/contests/magiclines/challenges/detecting-valid-latitude-and-longitude-pairs-key-line 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() { 9 | 10 | char x[100],y[100]; 11 | int t,i,flag; 12 | scanf("%d",&t); 13 | while(t) 14 | { 15 | flag=1; 16 | //missing line; 17 | scanf("%s",x); 18 | scanf("%s",y); 19 | 20 | int xlen=strlen(x); 21 | int ylen=strlen(y); 22 | 23 | if(x[1] == ' ' || y[0] == ' ' || x[xlen-2]== '.' || y[ylen-2]=='.' || y[ylen-2]==' ' || x[0]!= '(' || y[ylen-1]!= ')' || x[xlen-1]!= ',') 24 | flag=0; 25 | else if(x[1] == '+' || x[1] == '-') 26 | if(x[2]=='0' && x[3]!='.') 27 | flag=0; 28 | else if(y[0] == '+' || y[0] == '-') 29 | if(y[1]=='0' && y[2]!='.') 30 | flag=0; 31 | 32 | if(flag){ 33 | for(i=0;i90 || x1<-90) 40 | printf("Invalid\n"); 41 | else if(y1>180 || y1<-180) 42 | printf("Invalid\n"); 43 | else 44 | printf("Valid\n"); 45 | } 46 | else 47 | printf("Invalid\n"); 48 | 49 | t--; 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /diagonal-difference.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/diagonal-difference 2 | //mandeep singh @msdeep14 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int num,n,j=0,k=0,sq,i,sum1=0,sum2=0,diff,x; 8 | cin>>n; 9 | sq=n*n; 10 | for(i=0;i>num; 13 | if(k==n+1 || k==0) 14 | { 15 | sum1=sum1+num; 16 | k=0; 17 | } 18 | if(j==n-1 && i!=sq-1) 19 | { 20 | sum2=sum2+num; 21 | j=0; 22 | } 23 | k++; 24 | j++; 25 | } 26 | diff=sum1-sum2; 27 | if(diff<0) 28 | { 29 | diff=2*(-diff)+diff; 30 | } 31 | cout< 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | struct vec 9 | { 10 | int wt; 11 | int src; 12 | int dest; 13 | int track; 14 | }; 15 | 16 | class dijshktra 17 | { 18 | public: 19 | void creategraph(vector > &g,int v); 20 | void calpathcost(vector > &g,int strtver,int v); 21 | }; 22 | void dijshktra :: creategraph(vector > &g,int v) 23 | { 24 | int src,dest,cost; 25 | vec temp; 26 | cin>>src>>dest>>cost; 27 | temp.dest=dest; 28 | temp.src=src; 29 | temp.wt=cost; 30 | temp.track=INT_MAX; 31 | g[src-1].push_back(temp); 32 | vec temp1; 33 | temp1.dest=src; 34 | temp1.src=dest; 35 | temp1.wt=cost; 36 | temp1.track=INT_MAX; 37 | g[dest-1].push_back(temp1); 38 | 39 | } 40 | 41 | int check(int visited[], int i) 42 | { 43 | int flag; 44 | if(i==visited[i]) 45 | flag=0; 46 | else 47 | flag=1; 48 | return flag; 49 | } 50 | 51 | void dijshktra :: calpathcost(vector > &g,int strtver,int v) 52 | { 53 | int mindis[3000]; 54 | int store[3000]; 55 | int visited[3000]; 56 | int i,j,newvertex; 57 | int temp=strtver; 58 | int temp1=strtver; 59 | visited[0]=0; 60 | visited[strtver]=strtver; 61 | for(i=0;i<=v;i++) 62 | { 63 | visited[i]=INT_MAX; 64 | } 65 | for(i=1;i<=v;i++) 66 | { 67 | store[i]=0; 68 | } 69 | for(i=0;i<=v;i++) 70 | { 71 | mindis[i]=INT_MAX; 72 | } 73 | int x=0; 74 | mindis[strtver]=0; 75 | int mark=0; 76 | while(mark<(v)) 77 | { 78 | for(i=0;i=(g[strtver-1][i].wt + x) && ((check(visited,g[strtver-1][i].dest)) )) 82 | { 83 | mindis[g[strtver-1][i].dest]=g[strtver-1][i].wt + x; 84 | } 85 | } 86 | int min=INT_MAX; 87 | for(j=1;j<=v;j++) 88 | { 89 | if(min>mindis[j] && j!=strtver && check(visited,j)) 90 | { 91 | min=mindis[j]; 92 | newvertex=j; 93 | } 94 | } 95 | visited[newvertex]=newvertex; 96 | store[temp]=mindis[newvertex]; 97 | x=min; 98 | temp=strtver; 99 | strtver=newvertex; 100 | mark++; 101 | } 102 | for(i=1;i<=v;i++) 103 | { 104 | if(i!=temp1) 105 | { 106 | if(mindis[i]==INT_MAX) 107 | cout<<"-1 "; 108 | else 109 | cout< > g; 119 | int strtver,v; 120 | cin>>t; 121 | while(t--) 122 | { 123 | dijshktra dj; 124 | cin>>n>>m; 125 | g.resize(m); 126 | int i,j; 127 | for(i=0;i>strtver; 132 | dj.calpathcost(g,strtver,n); 133 | g.clear(); 134 | 135 | } 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /divisible-sum-pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | 26 | 27 | int main(){ 28 | int n; 29 | int k; 30 | int count=0; 31 | cin >> n >> k; 32 | vector a(n); 33 | for(int a_i = 0;a_i < n;a_i++){ 34 | cin >> a[a_i]; 35 | } 36 | for(int i=0;i 4 | int power(int k) 5 | { 6 | int i,j,ans=1; 7 | for(i=1;i<=k;i++) 8 | { 9 | ans=(ans*2)%100000; 10 | } 11 | return ans; 12 | } 13 | int main() 14 | { 15 | int i,j,t,n; 16 | scanf("%d",&t); 17 | while(t--) 18 | { 19 | scanf("%d",&n); 20 | printf("%d\n",power(n)-1); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /encryption.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/encryption 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | #include 6 | int main() 7 | { 8 | int i,j,col,row,ur,lr,len,c=0,r=0; 9 | int ans ; 10 | char str[100]; 11 | gets(str); 12 | //you need to remove spaces between the words of complete string; 13 | //if man was meant to stag on ground 14 | // is equivalent to ifmanwasmeanttostagonground 15 | len=strlen(str); 16 | lr=(int)sqrt(1.0*len); 17 | ur=(int)ceil(sqrt(1.0*len)); 18 | for(row=lr;row<=ur;row++) 19 | { 20 | for(col=row;col<=ur;col++) 21 | { 22 | if(row*col>=len) 23 | { 24 | ans=row*col; 25 | r=row; 26 | c=col; 27 | } 28 | } 29 | } 30 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define ll long long int 7 | using namespace std; 8 | ll arr[100005],brr[100005],crr[100005]; 9 | ll calmax(ll a,ll b,ll c){ 10 | if((a>=b &&a>=c)) return 1; 11 | else if(b>=a&&b>=c) return 2; 12 | else if(c>=a&&c>=b) return 3; 13 | else return 0; 14 | } 15 | int main(){ 16 | ll n1,n2,n3; 17 | 18 | ll a=0,b=0,c=0; 19 | cin>>n1>>n2>>n3; 20 | for(int i=0;i>arr[i]; 22 | a+=arr[i]; 23 | } 24 | for(int i=0;i>brr[i]; 26 | b+=brr[i]; 27 | } 28 | for(int i=0;i>crr[i]; 30 | c+=crr[i]; 31 | } 32 | ll flag=1; 33 | if(a==b&&b==c){ 34 | cout< 3 | #include 4 | #include 5 | using namespace std; 6 | int oper(int diff) 7 | { 8 | if (diff == 0) 9 | return 0; 10 | if (diff <= 2) 11 | return 1; 12 | if (diff <= 4) 13 | return 2; 14 | return oper(diff % 5) + diff / 5; 15 | } 16 | int main() { 17 | int t,n; 18 | int candy[10000]; 19 | cin>>t; 20 | while (t--) 21 | { 22 | cin>>n; 23 | int operations = INT_MAX, min = INT_MAX; 24 | for (int i=0;i>candy[i]; 27 | if (candy[i] < min) 28 | min = candy[i]; 29 | } 30 | for (int k = min; k>=min-2; k--) 31 | { 32 | int sum = 0; 33 | for (int i=0;i 3 | int main() 4 | { 5 | int n,m; 6 | long long int count=0; 7 | scanf("%d%d",&n,&m); 8 | while(m--) 9 | { 10 | long long int a,b,k; 11 | scanf("%lld%lld%lld",&a,&b,&k); 12 | count=count+(b-a+1)*k; 13 | } 14 | printf("%lld",(count/n)); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /find-digits.c: -------------------------------------------------------------------------------- 1 | //find digits 2 | #include 3 | int main() 4 | { 5 | int i,j,t,num,count=0,temp,rem=0; 6 | int arr[20],brr[20]; 7 | scanf("%d",&t); 8 | for(i=0;i0) 15 | { 16 | rem=temp%10; 17 | if(rem!=0) 18 | { 19 | if(arr[i]%rem==0) 20 | count++; 21 | } 22 | temp=temp/10; 23 | } 24 | brr[i]=count; 25 | } 26 | for(i=0;i 4 | #include 5 | int main() 6 | { 7 | int i,j,k,t,n,flag1,flag2; 8 | char str[5000]; 9 | char str1[10]={'h','a','c','k','e','r','r','a','n','k'}; 10 | scanf("%d",&t); 11 | gets(str); 12 | while(t--) 13 | { 14 | flag1=0; 15 | flag2=0; 16 | gets(str); 17 | n=strlen(str); 18 | for(i=0;i<10;i++) 19 | { 20 | if(str[i]==str1[i]) 21 | { 22 | //do nothing; 23 | } 24 | else 25 | { 26 | flag1=1; 27 | break; 28 | } 29 | } 30 | for(i=n-1,j=9;i>=(n-10),j>=0;i--,j--) 31 | { 32 | if(str[i]==str1[j]) 33 | { 34 | //do nothing; 35 | } 36 | else 37 | { 38 | flag2=1; 39 | break; 40 | } 41 | 42 | } 43 | if(flag1==0 && flag2==0) 44 | printf("0\n"); 45 | else if(flag1==0 && flag2==1) 46 | printf("1\n"); 47 | else if(flag1==1 && flag2==0) 48 | printf("2\n"); 49 | else 50 | printf("-1\n"); 51 | 52 | 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /find-median.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/find-median 2 | //mandeep singh @msdeep14 3 | #include 4 | void insertion(int a[], int s) 5 | { 6 | int i, j; 7 | int current; 8 | 9 | for(i = 1; i< s; i++) 10 | { 11 | current = a[i]; 12 | for(j = i-1; j>=0; j--) 13 | { 14 | if(current < a[j]) 15 | a[j+1] = a[j]; 16 | else 17 | break; 18 | } 19 | a[j+1] = current; 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | int i,j,n,k; 26 | int arr[1000000]; 27 | scanf("%d",&n); 28 | for(i=0;i 4 | int main() 5 | { 6 | int a,b,c,d,e,f,t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | scanf("%d%d%d%d",&a,&b,&c,&d); 11 | e=2*c-a; 12 | f=2*d-b; 13 | printf("%d %d\n",e,f); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /fixing-bubble-sort.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/contests/magic-lines-july-2015/challenges/fixing-bubble-sort 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | using namespace std; 25 | void bubble_sort (vector&a) { 26 | int n=a.size(); 27 | int i, t, s = 1; 28 | while (1) { 29 | if(s==0)break; 30 | s = 0; 31 | 32 | for (i = 1; i < n; i++) { 33 | if (a[i] < a[i - 1]) { 34 | t = a[i]; 35 | a[i] = a[i - 1]; 36 | a[i - 1] = t; 37 | s = 1; 38 | } 39 | } 40 | } 41 | } 42 | 43 | int main() { 44 | 45 | vectora; 46 | int n; 47 | cin>>n; 48 | for(int i=0;i>x; 52 | a.push_back(x); 53 | } 54 | bubble_sort(a); 55 | for(int i=0;i<(int)a.size();i++) 56 | { 57 | cout< 3 | int main() 4 | { 5 | int i; 6 | for(i=1;i<=100;i++) 7 | { 8 | if(i%3==0 && i%5==0) 9 | printf("\nFizzBuzz"); 10 | else if(i%5==0) 11 | printf("\nBuzz"); 12 | else if(i%3==0) 13 | printf("\nFizz"); 14 | else 15 | printf("\n%d",i); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /flipping-bits.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/flipping-bits 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | int main() 6 | { 7 | long long int i,j,t,num,n,rem,sum=0,power=1; 8 | long long int arr[32],brr[32]; 9 | scanf("%lld",&t); 10 | while(t--) 11 | { 12 | sum=0; 13 | power=1; 14 | scanf("%lld",&num); 15 | j=31; 16 | int pos=0; 17 | while(num>=1) 18 | { 19 | rem=num%2; 20 | arr[j]=rem; 21 | j--; 22 | num=num/2; 23 | pos++; 24 | } 25 | for(i=0;i<(32-pos);i++) 26 | { 27 | arr[i]=0; 28 | } 29 | for(i=0;i<32;i++) 30 | { 31 | if(arr[i]==0) 32 | arr[i]=1; 33 | else if(arr[i]==1) 34 | arr[i]=0; 35 | } 36 | for(i=31;i>=0;i--) 37 | { 38 | sum=sum+arr[i]*power; 39 | power=power*2; 40 | } 41 | printf("%lld\n",sum); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /flowers.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/flowers 2 | //mandeep singh 3 | //https://www.hackerrank.com/challenges/flowers 4 | //mandeep singh 5 | #include 6 | #include 7 | void insertion( long long int a[], long long int s) 8 | { 9 | long long int i, j; 10 | long long int current; 11 | 12 | for(i = 1; i< s; i++) 13 | { 14 | current = a[i]; 15 | for(j = i-1; j>=0; j--) 16 | { 17 | if(current < a[j]) 18 | a[j+1] = a[j]; 19 | else 20 | break; 21 | } 22 | a[j+1] = current; 23 | } 24 | } 25 | int main() 26 | { 27 | long long int i,j,m,k,n,cost=0; 28 | long long int arr[100000]; 29 | scanf("%lld%lld",&n,&k); 30 | 31 | for(i=0;i 4 | #define INT_MAX 99999; 5 | using namespace std; 6 | int main() 7 | { 8 | int i,j; 9 | int d[401][401]; 10 | int m,n,a,b,wt,q,src,dest; 11 | cin>>n>>m; 12 | for(i=1;i<=n;i++) 13 | { 14 | for(j=1;j<=n;j++) 15 | { 16 | if(i==j) 17 | d[i][j]=0; 18 | else 19 | d[i][j]=INT_MAX; 20 | } 21 | } 22 | for(i=0;i>src>>dest>>wt; 25 | d[src][dest]=wt; 26 | } 27 | for(int k=1;k<=n;k++) 28 | { 29 | for(i=1;i<=n;i++) 30 | { 31 | for(j=1;j<=n;j++) 32 | { 33 | if(d[i][k]+d[k][j]>q; 42 | while(q--) 43 | { 44 | cin>>a>>b; 45 | if(d[a][b]==99999) 46 | cout<<"-1"< 2 | #include 3 | #include 4 | using namespace std; 5 | struct s 6 | { 7 | int num ; 8 | char st[10]; 9 | int half; 10 | }; 11 | 12 | void merge(vector& arr, int l, int m, int r) 13 | { 14 | int i, j, k; 15 | int n1 = m - l + 1; 16 | int n2 = r - m; 17 | 18 | /* create temp arrays */ 19 | vector L, R; 20 | L.resize(n1); 21 | R.resize(n2); 22 | /* Copy data to temp arrays L[] and R[] */ 23 | for (i = 0; i < n1; i++) 24 | L[i] = arr[l + i]; 25 | for (j = 0; j < n2; j++) 26 | R[j] = arr[m + 1+ j]; 27 | 28 | /* Merge the temp arrays back into arr[l..r]*/ 29 | i = 0; // Initial index of first subarray 30 | j = 0; // Initial index of second subarray 31 | k = l; // Initial index of merged subarray 32 | while (i < n1 && j < n2) 33 | { 34 | if (L[i].num <= R[j].num) 35 | { 36 | arr[k] = L[i]; 37 | i++; 38 | } 39 | else 40 | { 41 | arr[k] = R[j]; 42 | j++; 43 | } 44 | k++; 45 | } 46 | 47 | /* Copy the remaining elements of L[], if there 48 | are any */ 49 | while (i < n1) 50 | { 51 | arr[k] = L[i]; 52 | i++; 53 | k++; 54 | } 55 | 56 | /* Copy the remaining elements of R[], if there 57 | are any */ 58 | while (j < n2) 59 | { 60 | arr[k] = R[j]; 61 | j++; 62 | k++; 63 | } 64 | } 65 | 66 | /* l is for left index and r is right index of the 67 | sub-array of arr to be sorted */ 68 | void mergeSort(vector& arr, int l, int r) 69 | { 70 | if (l < r) 71 | { 72 | // Same as (l+r)/2, but avoids overflow for 73 | // large l and h 74 | int m = l+(r-l)/2; 75 | 76 | // Sort first and second halves 77 | mergeSort(arr, l, m); 78 | mergeSort(arr, m+1, r); 79 | 80 | merge(arr, l, m, r); 81 | } 82 | } 83 | 84 | 85 | //bool acompare(s lhs, s rhs) { return lhs.num < rhs.num; } 86 | int main() 87 | { 88 | vector arr; 89 | 90 | int n; 91 | cin>>n; 92 | arr.resize(n+1); 93 | for(int i=0;i>arr[i].num>>arr[i].st; 96 | if(i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main() { 11 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 12 | int t; 13 | string s, r; 14 | cin>>t; 15 | while(t--) 16 | { 17 | int count=0; 18 | cin>>s; 19 | r=s; 20 | reverse(r.begin(),r.end()); 21 | for(int i=1;i 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int i,j,k,even=0,odd=0,n,flag=0; 9 | char str[100000]; 10 | char abcstr[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 11 | int abccount[26]; 12 | for(i=0;i<26;i++) 13 | { 14 | abccount[i]=0; 15 | } 16 | cin>>str; 17 | n=strlen(str); 18 | k=0; 19 | for(i=0;i 3 | #include 4 | int searchchar(char [], char); 5 | int main() 6 | { 7 | int len,n,i=0,j,count=0,flag=-1,check=-1,k; 8 | char str[100][100]; 9 | scanf("%d",&n); 10 | for(i=0;i 4 | int main() 5 | { 6 | int i,j,k,t,n,flag=0; 7 | char str[100][100]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | flag=0; 12 | scanf("%d",&n); 13 | for(i=0;i=0; j--) 27 | { 28 | if(current < str[k][j]) 29 | str[k][j+1] = str[k][j]; 30 | else 31 | break; 32 | } 33 | str[k][j+1] = current; 34 | } 35 | } 36 | //now check for rows if they are sorted or not; 37 | for(i=0;i 3 | int main() 4 | { 5 | int t; 6 | scanf("%d",&t); 7 | while(t--){ 8 | unsigned long k,a,b; 9 | scanf("%ld",&k); 10 | a = k/2; 11 | b = k-a; 12 | printf("%ld\n",a*b); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /handshake.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/handshake 2 | //mandeep singh 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i,j,k,n,t,sum=0; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | sum=0; 12 | scanf("%d",&n); 13 | for(i=0;i 4 | #include 5 | int main() 6 | { 7 | int i,j,k,flag=0,n,m,x,y,t; 8 | int arr[100000]; 9 | arr[0]=0; 10 | scanf("%d",&t); 11 | while(t--) 12 | { 13 | flag=0; 14 | scanf("%d%d",&m,&n); 15 | for(i=1;i<=n;i++) 16 | { 17 | scanf("%d",&arr[i]); 18 | } 19 | for(i=1;i<=n;i++) 20 | { 21 | for(j=i+1;j<=n;j++) 22 | { 23 | if(arr[i]+arr[j]==m) 24 | { 25 | x=i; 26 | y=j; 27 | flag=1; 28 | break; 29 | } 30 | } 31 | if(flag==1) 32 | { 33 | break; 34 | } 35 | } 36 | printf("%d %d",x,y); 37 | printf("\n"); 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /insertionsort-part1.c: -------------------------------------------------------------------------------- 1 | //insertion sort part-1 2 | #include 3 | int main() 4 | { 5 | int i,j,pos,n,flag,k; 6 | int arr[1010]; 7 | scanf("%d",&n); 8 | for(i=0;i0;i--) 12 | { 13 | flag=0; 14 | if(temp0) 23 | { 24 | for(j=0;j0) 30 | { 31 | arr[pos-1]=temp; 32 | for(k=0;k 3 | int main() 4 | { 5 | int i,j,current,s,k; 6 | int arr[1010]; 7 | scanf("%d",&s); 8 | for(i=0;i=0; j--) 14 | { 15 | if(current < arr[j]) 16 | arr[j+1] = arr[j]; 17 | else 18 | break;//stop the loop 19 | } 20 | arr[j+1] = current;//insertion 21 | 22 | for(k=0;k 3 | int binarySearch(int a[], int s, int val) 4 | { 5 | int x,y; 6 | int mid; 7 | 8 | x = 0; 9 | y = s-1; 10 | 11 | while(x <= y) 12 | { 13 | mid = (x+y)/2; 14 | 15 | //compare 16 | if(val == a[mid]) 17 | {//match found 18 | return mid;//position (index) 19 | } 20 | else if(val < a[mid]) 21 | { 22 | y = mid-1; 23 | } 24 | else if(val > a[mid]) 25 | { 26 | x = mid +1; 27 | } 28 | } 29 | 30 | return -1; //not found 31 | } 32 | int main() 33 | { 34 | int n,v,i,pos; 35 | int arr[1010]; 36 | scanf("%d",&v); 37 | scanf("%d",&n); 38 | for(i=0;i 3 | int main() 4 | { 5 | long int i,j,num,flag,f,m,k,l,z; 6 | int t; 7 | long int arr[100000]; 8 | //store fibonnacci series in arr 9 | scanf("%d",&t); 10 | f=0; 11 | m=1; 12 | k=1; 13 | l=0; 14 | arr[0]=0; 15 | for(i=0;i<=10000;i++) 16 | { 17 | f=k+l; 18 | k=l; 19 | l=f; 20 | arr[m]=f; 21 | m++; 22 | } 23 | for(z=0;z 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | stack s; 9 | int dfs(vector > &g,int taken[],int val) 10 | { 11 | s.push(val); 12 | int count=1; 13 | taken[val]=val; 14 | while(!s.empty()) 15 | { 16 | int x=s.top(); 17 | 18 | for(int i=0;i > g; 42 | cin>>n>>k; 43 | g.resize(n+1); 44 | int taken[100004]; 45 | for(int i=0;i<=n;i++) 46 | { 47 | taken[i]=INT_MAX; 48 | } 49 | 50 | for(int i=0;i>a>>b; 53 | g[a].push_back(b); 54 | g[b].push_back(a); 55 | } 56 | for(int i=0;i 2 | #include 3 | #define ll long long int 4 | using namespace std; 5 | int main(){ 6 | double x1,x2,v1,v2,d; 7 | cin>>x1>>v1>>x2>>v2; 8 | if((x2==x1)) cout<<"YES"; 9 | else if((x2>x1&&v2>=v1) || (x1>x2 && v1>=v2) ) cout<<"NO"; 10 | else{ 11 | if(x2>=x1){ 12 | //d=(v1*(x1-x2))/(v2-v1); 13 | d=(2*x2-2*x1+v1-v2)/(v1-v2); 14 | if(d>=0 && floor(d)==d) cout<<"YES"; 15 | else cout<<"NO"; 16 | } 17 | else{ 18 | //d=(v2*(x2-x1))/(v1-v2); 19 | d=(2*x1-2*x2+v2-v1)/(v2-v1); 20 | if(d>=0 && floor(d)==d) cout<<"YES"; 21 | else cout<<"NO"; 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /larrys-array.cpp: -------------------------------------------------------------------------------- 1 | //hackerrank lary's array 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | int n,t; 7 | int arr[1005]; 8 | cin>>t; 9 | while(t--){ 10 | cin>>n; 11 | int flag=0; 12 | for(int i=0;i>arr[i]; 13 | for(int i=0;i=i;j--){ 15 | int a=arr[j]; 16 | int b=arr[j+1]; 17 | int c=arr[j+2]; 18 | if(c 3 | #include 4 | #include 5 | #include 6 | #include 7 | int lonelyinteger(int a_size, int* a) { 8 | int flag=1,i,k; 9 | for(i=0;i 3 | #include 4 | #include 5 | #include 6 | #include 7 | int lonelyinteger(int a_size, int* a) { 8 | int flag=1,i,k; 9 | for(i=0;i 3 | #include 4 | int main() 5 | { 6 | int i,j,k,count=0,n1,n2,flag=0; 7 | char str1[10000],str2[10000]; 8 | scanf("%s",str1); 9 | scanf("%s",str2); 10 | n1=strlen(str1); 11 | n2=strlen(str2); 12 | for(i=0;i 3 | int main() 4 | { 5 | 6 | int t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | int n,a,b,gt,lw; 11 | scanf("%d%d%d",&n,&a,&b); 12 | if(a>b) 13 | { 14 | gt =a ; 15 | lw = b; 16 | } 17 | else 18 | { 19 | lw = a; 20 | gt = b; 21 | } 22 | int ans = lw*(n-1); 23 | n--; 24 | int s = gt-lw; 25 | printf("%d ",ans); 26 | if(s>0) 27 | { 28 | while(n--) 29 | { 30 | ans =ans + s; 31 | printf("%d ",ans); 32 | } 33 | } 34 | printf("\n"); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /mark-and-toys.c: -------------------------------------------------------------------------------- 1 | //mark-and-toys 2 | #include 3 | #include 4 | #include 5 | #include 6 | //lets try by doing with the help of functions 7 | void sort( long long int a[], long long int s) 8 | { 9 | long long int current, t, x; 10 | for(t = 1; t< s; t++) 11 | { 12 | current = a[t]; 13 | for(x = t-1; x>=0; x--) 14 | { 15 | if(current < a[x]) 16 | a[x+1] = a[x]; 17 | else 18 | break; 19 | } 20 | a[x+1] = current; 21 | } 22 | } 23 | int main() { 24 | long long int n, k, i, count=0,j,sum=0,temp; 25 | long long int arr[100002]; 26 | scanf("%llu%llu", &n, &k); 27 | 28 | for(i=0; ik) 44 | { 45 | break; 46 | } 47 | } 48 | printf("%llu",count); 49 | return 0; 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /max-min.c: -------------------------------------------------------------------------------- 1 | //max-min 2 | #include 3 | #include 4 | void insertion(long long int a[], long long int s) 5 | { 6 | int i, j; 7 | int current; 8 | 9 | for(i = 1; i< s; i++) 10 | { 11 | current = a[i]; 12 | for(j = i-1; j>=0; j--) 13 | { 14 | if(current < a[j]) 15 | a[j+1] = a[j]; 16 | else 17 | break; 18 | } 19 | a[j+1] = current; 20 | } 21 | } 22 | int main() 23 | { 24 | long long int i,j,k,n,m=0,size,min; 25 | long long int arr[100000],brr[100000]; 26 | scanf("%lld",&n); 27 | scanf("%lld",&k); 28 | for(i=0;ibrr[i]) 44 | min=brr[i]; 45 | } 46 | printf("%lld",min); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /maximise-sum.cpp: -------------------------------------------------------------------------------- 1 | //hackerrank maximise-sum 2 | #include 3 | #include 4 | #include 5 | #define ll long long int 6 | using namespace std; 7 | int main(){ 8 | ll n,m,t; 9 | ll arr[100005]; 10 | ll p[100005]; 11 | set s; 12 | cin>>t; 13 | while(t--){ 14 | ll res=0; 15 | ll max_end=0; 16 | cin>>n>>m; 17 | for(int i=0;i>arr[i]; 18 | p[0]=arr[0]%m; 19 | s.insert(p[0]); 20 | res=p[0]; 21 | for(int i=1;i ::iterator prev; 25 | prev=s.upper_bound(p[i]); 26 | if(prev!=s.end()){ 27 | max_end=(p[i]-*prev+m)%m; 28 | } 29 | res=max(res,max_end); 30 | s.insert(p[i]); 31 | } 32 | 33 | cout< 2 | #include 3 | #include 4 | #include 5 | #define ll long long int 6 | using namespace std; 7 | int main(){ 8 | ll n; 9 | ll flag=0; 10 | ll arr[1005]; 11 | cin>>n; 12 | ll min=INT_MAX; 13 | for(int i=0;i>arr[i]; 14 | for(int i=0;i 4 | int main() 5 | { 6 | int i,j,t,n; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | scanf("%d",&n); 11 | printf("%d\n",n+1); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /missing-numbers.c: -------------------------------------------------------------------------------- 1 | //missing-numbers 2 | //mandeep singh 3 | 4 | #include 5 | #include 6 | #define SIZE 10001 7 | int main() 8 | { 9 | int a[SIZE]; 10 | int i,n,m; 11 | for( i = 0; i 9 | #include 10 | void karpekarnumber(int p, int q, char *arr) 11 | { 12 | long int i, num,j,k,m,d,sq,rem,flag=0,temp; 13 | for(i=p;i<=q;i++) 14 | { 15 | sq=i*i; 16 | d=0; 17 | temp=i; 18 | while(temp>0) 19 | { 20 | temp=temp/10; 21 | d++; 22 | } 23 | m=d-1; 24 | k=0; 25 | while(sq>0 && k 3 | using namespace std; 4 | int check(int arr[],int x,int n) 5 | { 6 | for(int i=x;i>=0;i--) 7 | { 8 | if(arr[i]==x) 9 | { 10 | return i; 11 | } 12 | } 13 | return 0; 14 | } 15 | int main() 16 | { 17 | int t,n; 18 | int arr[100000]; 19 | int brr[100000]; 20 | cin>>t; 21 | arr[0]=0; 22 | while(t--) 23 | { 24 | 25 | int bribe=0; 26 | cin>>n; 27 | int x; 28 | for(int i=0;i<=n;i++) 29 | { 30 | brr[i]=0; 31 | } 32 | for(int i=1;i<=n;i++) 33 | cin>>arr[i]; 34 | int flag=1; 35 | int z=0; 36 | 37 | int j=n; 38 | int i=n; 39 | for(int i=n;i>0;i--) 40 | { 41 | int y=1; 42 | while(y) 43 | { 44 | if(arr[j]!=i) 45 | { 46 | x=check(arr,j,n); 47 | int temp=arr[x+1]; 48 | arr[x+1]=arr[x]; 49 | arr[x]=temp; 50 | bribe+=1; 51 | brr[j]+=1; 52 | if(brr[j]>2) 53 | { 54 | cout<<"Too chaotic"< 3 | using namespace std; 4 | int main() 5 | { 6 | int n; 7 | int arr [100000]; 8 | cin>>n; 9 | for(int i=0;i>arr[i]; 11 | 12 | int x=arr[0]; 13 | int count=0; 14 | int sum=arr[0]; 15 | for(int i=1;i=arr[i]) 27 | { 28 | sum+=1; 29 | } 30 | if(sum 3 | using namespace std; 4 | int main() 5 | { 6 | int t,n,x; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n0=0; 11 | int n1=0; 12 | int n2=0; 13 | cin>>n; 14 | for(int i=0;i>x; 17 | if(x%3==0) n0++; 18 | else if(x%3==1) n1++; 19 | else if(x%3==2) n2++; 20 | } 21 | if(n1%2==0 && n2%2==0) cout<<"Koca\n"; 22 | else cout<<"Balsa\n"; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /pairs.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/pairs 2 | //mandeep singh @msdeep14 3 | #include 4 | using namespace std; 5 | void merge(int arr[], int l, int m, int r) 6 | { 7 | int i,j,k; 8 | int n1=m-l+1; 9 | int n2=r-m; 10 | int L[n1], R[n2]; 11 | for(i=0;i>n>>k; 63 | for(i=0;i>arr[i]; 65 | mergesort(arr,0,n-1); 66 | for(i=n-1;i>0;i--) 67 | { 68 | for(j=i-1;j>=0;j--) 69 | { 70 | if(arr[i]-arr[j]==k) 71 | count++; 72 | else if(arr[i]-arr[j]>k) 73 | break; 74 | } 75 | } 76 | cout< 3 | #include 4 | int main() 5 | { 6 | int i,j,k,t,n,flag; 7 | char str[100010]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | flag=0; 12 | scanf("%s",str); 13 | n=strlen(str); 14 | i=0; 15 | j=n-1; 16 | while(i 4 | #include 5 | #include 6 | #define ll long long 7 | using namespace std; 8 | int main() 9 | { 10 | ll int t,n; 11 | ll int score[100001]; 12 | ll int sum[100001]; 13 | ll int best[100001]; 14 | cin>>t; 15 | while(t--) 16 | { 17 | cin>>n; 18 | for(int i=1;i<=n;i++) 19 | { 20 | cin>>score[i]; 21 | } 22 | for(int i=1;i<=n+1;i++) 23 | { 24 | sum[i]=0; 25 | best[i]=0; 26 | } 27 | //memset(sum,0,n+1); 28 | //memset(best,0,n+1); 29 | for(int i=n;i>=1;i--) 30 | { 31 | sum[i]=score[i] + sum[i+1]; 32 | } 33 | best[n]=sum[n]; 34 | best[n-1]=sum[n-1]; 35 | best[n-2]=sum[n-2]; 36 | for(int i=n-3;i>=1;i--) 37 | { 38 | for(int j=1;j<=3;j++) 39 | { 40 | best[i]=max(best[i],sum[i]-best[i+j]); 41 | } 42 | } 43 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int i,j,countplus=0,countminus=0,countzero=0; 9 | int arr[200]; 10 | float n; 11 | cin>>n; 12 | for(i=0;i>arr[i]; 15 | if(arr[i]<0) 16 | countminus++; 17 | else if(arr[i]>0) 18 | countplus++; 19 | else 20 | countzero++; 21 | } 22 | printf("%.3f\n%.3f\n%.3f\n",(countplus/n),(countminus/n),(countzero/n)); 23 | } 24 | -------------------------------------------------------------------------------- /polynomial-and-its-roots.cpp: -------------------------------------------------------------------------------- 1 | ////https://www.hackerrank.com/contests/infinitum12/challenges/polynomial-and-its-roots 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | long long int n,a,i,j,sum,pr; 7 | long long int arr[3]; 8 | cin>>n; 9 | if(n>1) 10 | { 11 | for(i=0;i<=n;i++) 12 | { 13 | cin>>a; 14 | if(i==0) 15 | arr[0]=a; 16 | else if(i==(n-1)) 17 | arr[1]=a; 18 | else if(i==n) 19 | arr[2]=a; 20 | } 21 | sum=(-arr[1])/arr[2]; 22 | if(n%2==0) 23 | pr=(arr[0])/(arr[2]); 24 | else 25 | pr=(-arr[0])/(arr[2]); 26 | } 27 | else if(n==1) 28 | { 29 | for(i=0;i<=n;i++) 30 | { 31 | cin>>a; 32 | arr[i]=a; 33 | } 34 | sum=-(arr[0]/arr[1]); 35 | pr=-(arr[0]/arr[1]); 36 | } 37 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | struct vec 8 | { 9 | int dest; 10 | int src; 11 | int wt; 12 | int track; 13 | }; 14 | int check(int visited[],int dest,int n) 15 | { 16 | int flag=0,c=0; 17 | if(dest==visited[dest]) 18 | {c=0; 19 | flag=1; 20 | } 21 | if(flag==0) 22 | c=1; 23 | return c; 24 | } 25 | 26 | int main() 27 | { 28 | vector > g; 29 | int a,b,i,j,k,l,x; 30 | int visited[3000]; 31 | int sumwt=0; 32 | int n,m,wtt,strtver; 33 | int keeptrack[3000]; 34 | cin>>n>>m; 35 | g.resize(m+1); 36 | for(i=0;i<=n;i++) 37 | { 38 | visited[i]=-1; 39 | } 40 | vec temp; 41 | for(i=0;i>a>>b>>wtt; 44 | temp.dest=b; 45 | temp.src=a; 46 | temp.wt=wtt; 47 | temp.track=INT_MAX; 48 | g[a-1].push_back(temp); 49 | g[b-1].push_back(temp); 50 | } 51 | cin>>strtver; 52 | int min=INT_MAX; 53 | int node1; 54 | 55 | k=0; 56 | int node; 57 | keeptrack[k]=strtver; 58 | k++; 59 | int size=k; 60 | visited[strtver]=strtver; 61 | while(size!=n) 62 | { 63 | for(j=0;jg[strtver][i].wt) 69 | { 70 | if(check(visited,g[strtver][i].dest,n) ) 71 | { 72 | min=g[strtver][i].wt; 73 | node=g[strtver][i].dest; 74 | node1=strtver; 75 | x=i; 76 | } 77 | else if(check(visited,g[strtver][i].src,n)) 78 | { 79 | min=g[strtver][i].wt; 80 | node=g[strtver][i].src; 81 | node1=strtver; 82 | x=i; 83 | } 84 | } 85 | } 86 | } 87 | //when node is visited mark it in the visited array; 88 | visited[node]=node; 89 | sumwt=sumwt+g[node1][x].wt; 90 | keeptrack[k]=node; 91 | k++; 92 | min=INT_MAX; 93 | size=k; 94 | } 95 | cout< 4 | #include 5 | void insertion(int a[], int s) 6 | { 7 | int i, j; 8 | int current; 9 | 10 | for(i = 1; i< s; i++) 11 | { 12 | current = a[i]; 13 | for(j = i-1; j>=0; j--) 14 | { 15 | if(current < a[j]) 16 | a[j+1] = a[j]; 17 | else 18 | break; 19 | } 20 | a[j+1] = current; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int i,j,k,w,flag=0,n,unit,x; 27 | int arr[100000]; 28 | scanf("%d",&n); 29 | for(i=0;i=w) 48 | { 49 | i++; 50 | flag=1; 51 | } 52 | if(flag==0) 53 | break; 54 | 55 | } 56 | if(flag==0) 57 | { 58 | i++; 59 | } 60 | 61 | } 62 | 63 | printf("%d",unit); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /quicksort1.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/quicksort1 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n,i,j,k,temp; 9 | int arr[1000]; 10 | cin>>n; 11 | for(i=0;i>arr[i]; 14 | } 15 | j=n-1; 16 | for(i=n-1;i>0;i--) 17 | { 18 | if(arr[i]>arr[0]) 19 | { 20 | temp=arr[i]; 21 | arr[i]=arr[j]; 22 | arr[j]=temp; 23 | j--; 24 | } 25 | } 26 | temp=arr[j]; 27 | arr[j]=arr[0]; 28 | arr[0]=temp; 29 | 30 | for(i=0;i 4 | #include 5 | using namespace std; 6 | int partision(int a[],int start,int end,int size) 7 | { 8 | int temp; 9 | int pivot=a[end]; 10 | int pindex=start; 11 | for(int i=start;i>n; 44 | for(i=0;i>arr[i]; 46 | quicksort(arr,0,n-1,n); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /quicksort4.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/quicksort4 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | int qswap=0,iswap=0; 7 | int insertion(int a[], int s) 8 | { 9 | int i, j; 10 | int current; 11 | 12 | for(i = 1; i< s; i++) 13 | { 14 | current = a[i]; 15 | for(j = i-1; j>=0; j--) 16 | { 17 | if(current < a[j]) 18 | { a[j+1] = a[j]; 19 | iswap++; 20 | } 21 | else 22 | break; 23 | } 24 | a[j+1] = current; 25 | } 26 | return iswap; 27 | } 28 | 29 | 30 | int partision(int a[],int start,int end,int iswap) 31 | { 32 | int temp; 33 | int pivot=a[end]; 34 | int pindex=start; 35 | for(int i=start;i>n; 69 | for(i=0;i>arr[i]; 71 | for(i=0;i 4 | #include 5 | #include 6 | using namespace std; 7 | class solve 8 | { 9 | int f[41]; 10 | int numprime[41]; 11 | int prime[400000]; 12 | public: 13 | void set_data(); 14 | void run_testcase(); 15 | void seive(); 16 | void count_prime(); 17 | }; 18 | 19 | void solve :: count_prime() 20 | { 21 | int count=0; 22 | numprime[0]=0; 23 | for(int i=1;i<=41;i++) 24 | { 25 | for(int j=2;j<=f[i];j++) 26 | { 27 | if(prime[j]) 28 | { 29 | count++; 30 | } 31 | } 32 | numprime[i]=count; 33 | count=0; 34 | 35 | } 36 | } 37 | 38 | void solve :: run_testcase() 39 | { 40 | int t,n; 41 | cin>>t; 42 | while(t--) 43 | { 44 | cin>>n; 45 | cout< 3 | int main() 4 | { 5 | int i,c,t,max,sq,a,l,b,min; 6 | scanf("%d",&t); 7 | while(t--) 8 | { 9 | scanf("%d%d",&l,&b); 10 | if(l 4 | int main() 5 | { 6 | int i,j,k,m,n,l,t; 7 | int arr[100000]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | scanf("%d%d",&n,&k); 12 | arr[0]=n-1; 13 | j=n; 14 | l=2; 15 | for(i=1;i 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | string intToString(int a) 19 | { 20 | if(!a) return "0"; 21 | string ret; 22 | for(int i=a; i>0; i=i/10) 23 | ret.push_back((i%10)+48); 24 | reverse(ret.begin(),ret.end()); 25 | return ret; 26 | } 27 | string encode(string source) { 28 | string dest; 29 | for (int i = 0; i < (int)source.size(); i++) { 30 | int runLength = 1; 31 | while(source[i]==source[i+1]) 32 | { 33 | runLength++; 34 | i++; 35 | } 36 | dest+=source[i]; 37 | dest+=intToString(runLength); 38 | 39 | } 40 | return dest; 41 | } 42 | int main() 43 | { 44 | string s; 45 | cin>>s; 46 | cout< 4 | int main() 5 | { 6 | int i,j,current,s,k,m=0; 7 | int arr[1010]; 8 | scanf("%d",&s); 9 | for(i=0;i=0; j--) 15 | { 16 | if(current < arr[j]) 17 | { arr[j+1] = arr[j]; 18 | m++; 19 | } 20 | else 21 | break;//stop the loop 22 | } 23 | arr[j+1] = current;//insertion 24 | 25 | } 26 | printf("%d",m); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sam-and-substrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | #define MOD 1000000007 5 | int main() 6 | { 7 | string s; 8 | int arr[200001]; 9 | arr[1] = 1; 10 | for (int i = 2; i <= 200001; i++) 11 | arr[i] = ((long long)arr[i - 1] * 10 + 1) % MOD; 12 | 13 | cin >> s; 14 | int n = (int)s.size(); 15 | int sum = 0; 16 | for (int i = 1; i <= n; i++) 17 | { 18 | int digit = s[i - 1] - '0'; 19 | sum = ((long long)digit * i * arr[n - i + 1] + sum) % MOD; 20 | } 21 | cout < 8 | using namespace std; 9 | int main() 10 | { 11 | int i,j,k,t,n,ans; 12 | int arr[100000]; 13 | cin>>t; 14 | while(t--) 15 | { 16 | ans=0; 17 | cin>>n; 18 | for(i=0;i>arr[i]; 21 | } 22 | if(n%2==0) 23 | ans=0; 24 | else 25 | { 26 | //calculate xor of all odd index elements; 27 | ans=arr[0]; 28 | for(i=2;i 4 | #include 5 | int main() 6 | { 7 | int i,j,t,n; 8 | char str[1000]; 9 | scanf("%d",&t); 10 | gets(str); 11 | while(t--) 12 | { 13 | int flag1=0,flag2=0,flag3=0,flag4=0; 14 | gets(str); 15 | if(str[0]=='h' || str[0]=='H') 16 | flag1=1; 17 | if(str[1]=='i' || str[1]=='I') 18 | flag2=1; 19 | if(str[2]==' ' || str[2]==' ') 20 | flag3=1; 21 | if(str[3]!='d' || str[3]=='D') 22 | flag4=1; 23 | if(flag1==1 && flag2==1 && flag3==1 && flag4==1) 24 | puts(str); 25 | 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /service-lane.c: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/service-lane 2 | #include 3 | int main() 4 | { 5 | int a,b,n,i,j,k,l,min,t; 6 | int arr[100005]; 7 | 8 | 9 | scanf("%d%d",&n,&t); 10 | 11 | for(i=0;i 4 | int main() 5 | { 6 | int i,j,t,n,sum=0,sum1=0,sum2=0,flag=0; 7 | int arr[100000]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | flag=0; 12 | sum=0; 13 | sum1=0; 14 | sum2=0; 15 | scanf("%d",&n); 16 | for(i=0;i 4 | using namespace std; 5 | int main() 6 | { 7 | int i,j,t,n,count=0; 8 | cin>>t; 9 | while(t--) 10 | { 11 | count=0; 12 | cin>>n; 13 | for(i=1;i*i<=n;i++) if(n%i==0) 14 | { 15 | if(i%2==0) count++; 16 | if(i*i!=n && n/i%2==0) count++; 17 | } 18 | cout< 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int n,m,p,q; 13 | int a[100]; 14 | vector v; 15 | cin>>n; 16 | for(int i=0;i>a[i]; 19 | } 20 | cin>>p>>q; 21 | sort(a,a+n); 22 | for(int i=0;i<(n-1);i++) 23 | { 24 | if((a[i]+a[i+1])%2==0) 25 | v.push_back((a[i]+a[i+1])/2); 26 | else 27 | { 28 | v.push_back((a[i]+a[i+1])/2); 29 | v.push_back((a[i]+a[i+1])/2 + 1); 30 | } 31 | } 32 | v.push_back(p); 33 | v.push_back(q); 34 | sort(v.begin(),v.end()); 35 | 36 | int da=-1; 37 | int ans=0; 38 | for (int i=0;i= p && v[i] <= q) { 40 | int mini=INT_MAX; 41 | for (int j=0;j da) { 47 | da=mini; 48 | ans=v[i]; 49 | } 50 | } 51 | } 52 | cout< 4 | #include 5 | using namespace std; 6 | void merge(unsigned long long int arr[],unsigned long long int l,unsigned long long int m,unsigned long long int r) 7 | { 8 | unsigned long long int i,j,k; 9 | unsigned long long int n1=m-l+1; 10 | unsigned long long int n2=r-m; 11 | unsigned long long int L[n1], R[n2]; 12 | for(i=0;i>t; 64 | while(t--) 65 | { 66 | totalpair=0; 67 | flag=0; 68 | cin>>n; 69 | for(i=0;i>arr[i]; 71 | //now sort the arr; 72 | mergesort(arr,0,n-1); 73 | //array is sorted now; check for equal no.s; 74 | for(i=0;i 4 | int main() 5 | { 6 | int i,j,k,t,n,det; 7 | int a[12],b[3],c[3],d[3]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | for(i=0;i<12;i++) 12 | scanf("%d",&a[i]); 13 | b[0]=a[3]-a[0]; 14 | b[1]=a[4]-a[1]; 15 | b[2]=a[5]-a[2]; 16 | c[0]=a[6]-a[0]; 17 | c[1]=a[7]-a[1]; 18 | c[2]=a[8]-a[2]; 19 | d[0]=a[9]-a[0]; 20 | d[1]=a[10]-a[1]; 21 | d[2]=a[11]-a[2]; 22 | det=b[0]*(c[1]*d[2]-d[1]*c[2]) - c[0]*(b[1]*d[2]-d[1]*b[2]) + d[0]*(b[1]*c[2]-c[1]*b[2]); 23 | if(det==0) 24 | printf("YES\n"); 25 | else 26 | printf("NO\n"); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /sherlock-and-squares.c: -------------------------------------------------------------------------------- 1 | //sherlock-and-squares 2 | #include 3 | #include 4 | int main() 5 | { 6 | int t; 7 | scanf("%d",&t); 8 | while(t--) 9 | { 10 | int a, b;//a=min ,b=max 11 | scanf("%d%d",&a,&b); 12 | int count = 0, i=1; 13 | int square = pow(i, 2); 14 | while(square <= b) 15 | { 16 | if(square >= a) 17 | { 18 | count++; 19 | } 20 | i++; 21 | square = pow(i,2); 22 | } 23 | printf("%d\n",count); 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /sherlock-and-the-beast.c: -------------------------------------------------------------------------------- 1 | //sherlock-and-the-beast 2 | #include 3 | #include 4 | int main() 5 | { 6 | int i,j,a,b,n,flag=0,t,x,y; 7 | int *arr; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | scanf("%d",&n); 12 | arr=(int*)malloc(n*sizeof(int)); 13 | flag=0; 14 | for(b=0;b<=n;b+=5) 15 | { 16 | for(a=0;a<=n;a+=3) 17 | { 18 | if(n==a+b && a%3==0 && b%5==0) 19 | { 20 | x=a; 21 | y=b; 22 | flag=1; 23 | } 24 | } 25 | if(flag==1) 26 | break; 27 | } 28 | if(flag==0) 29 | printf("-1"); 30 | else 31 | { 32 | for(i=0;i 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | 12 | int main() { 13 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 14 | int nl,l,i; 15 | int count=0; 16 | int arr[26]={0}; 17 | char str[100000]; 18 | cin>>str; 19 | l=strlen(str); 20 | for(i=0;i1) 51 | cout<<"NO"< 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | struct vec 9 | { 10 | int wt; 11 | int src; 12 | int dest; 13 | int track; 14 | }; 15 | 16 | class dijshktra 17 | { 18 | public: 19 | void init(vector > &g); 20 | void creategraph(vector > &g,int v); 21 | void calpathcost(vector > &g,int strtver,int v,int s[]); 22 | }; 23 | 24 | void dijshktra :: init(vector > &g) 25 | { 26 | vec temp; 27 | int i,j,x; 28 | for(i=0;i<94;i++) 29 | { 30 | x=i+2; 31 | for(j=0;j<6;j++) 32 | { 33 | temp.dest=x; 34 | x++; 35 | temp.src=i+1; 36 | temp.wt=1; 37 | temp.track=INT_MAX; 38 | g[i].push_back(temp); 39 | } 40 | } 41 | int y=5; 42 | for(i=94;i<99;i++) 43 | { 44 | x=i+2; 45 | for(j=0;j > &g,int v) 60 | { 61 | int src,dest; 62 | vec temp; 63 | cin>>src>>dest; 64 | temp.dest=dest; 65 | temp.src=src; 66 | temp.wt=0; 67 | temp.track=INT_MAX; 68 | g[src-1].push_back(temp); 69 | } 70 | 71 | int check(int visited[], int i) 72 | { 73 | int flag; 74 | if(i==visited[i]) 75 | flag=0; 76 | else 77 | flag=1; 78 | return flag; 79 | } 80 | 81 | void dijshktra :: calpathcost(vector > &g,int strtver,int v,int s[]) 82 | { 83 | int mindis[3000]; 84 | int store[3000]; 85 | int visited[3000]; 86 | int i,j,newvertex; 87 | int newflag=0; 88 | int temp=strtver; 89 | int temp1=strtver; 90 | visited[0]=0; 91 | visited[strtver]=strtver; 92 | for(i=0;i<=v;i++) 93 | { 94 | visited[i]=INT_MAX; 95 | } 96 | for(i=1;i<=v;i++) 97 | { 98 | store[i]=0; 99 | } 100 | for(i=0;i<=v;i++) 101 | { 102 | mindis[i]=INT_MAX; 103 | } 104 | int x=0; 105 | mindis[strtver]=0; 106 | int mark=0; 107 | int tempwt=0; 108 | while(mark<(v)) 109 | { 110 | for(i=0;i=(g[strtver-1][i].wt + x) && ((check(visited,g[strtver-1][i].dest)) )) 114 | { 115 | mindis[g[strtver-1][i].dest]=g[strtver-1][i].wt + x; 116 | tempwt=g[strtver-1][i].wt + x; 117 | } 118 | 119 | } 120 | 121 | int min=INT_MAX; 122 | for(j=1;j<=v;j++) 123 | { 124 | if(min>mindis[j] && j!=strtver && check(visited,j)) 125 | { 126 | min=mindis[j]; 127 | newvertex=j; 128 | } 129 | } 130 | visited[newvertex]=newvertex; 131 | store[temp]=mindis[newvertex]; 132 | x=min; 133 | temp=strtver; 134 | strtver=newvertex; 135 | mark++; 136 | } 137 | 138 | if(mindis[i]==INT_MAX) 139 | cout<<"-1\n"; 140 | else 141 | cout< > g; 149 | int s[105]; 150 | int strtver,v; 151 | cin>>t; 152 | int src,dest; 153 | while(t--) 154 | { 155 | dijshktra dj; 156 | 157 | cin>>n; 158 | 159 | for(int y=0;y<105;y++) 160 | { 161 | s[y]=0; 162 | } 163 | g.resize(101); 164 | int i,j; 165 | for(i=0;i>m; 171 | for(i=0;i>src>>dest; 175 | s[src]=1; 176 | temp.dest=dest; 177 | temp.src=src; 178 | temp.wt=0; 179 | temp.track=INT_MAX; 180 | g[src-1].push_back(temp); 181 | 182 | } 183 | int countn=0; 184 | int maxn=0; 185 | int flagn=0; 186 | for(int i=0;i<101;i++){ 187 | if(s[i]==1) 188 | { 189 | countn++; 190 | flagn=1; 191 | } 192 | else{ 193 | flagn=0; 194 | } 195 | if(countn>maxn){ 196 | maxn=countn; 197 | } 198 | if(flagn==0){ 199 | countn=0; 200 | } 201 | } 202 | if(maxn>6) 203 | { 204 | cout<<"-1\n"; 205 | } 206 | else{ 207 | strtver=1; 208 | dj.calpathcost(g,strtver,100,s); 209 | } 210 | g.clear(); 211 | } 212 | return 0; 213 | } 214 | 215 | -------------------------------------------------------------------------------- /solve-me-first.c: -------------------------------------------------------------------------------- 1 | //solve me first 2 | #include 3 | #include 4 | #include 5 | #include 6 | int solveMeFirst(int a, int b) { 7 | return a+b; 8 | } 9 | int main() { 10 | int num1,num2; 11 | scanf("%d %d",&num1,&num2); 12 | int sum; 13 | sum = solveMeFirst(num1,num2); 14 | printf("%d",sum); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /solve-me-second.c: -------------------------------------------------------------------------------- 1 | //solve me second 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | int solveMeSecond(int a, int b) { 9 | return a+b; 10 | } 11 | int main() { 12 | int t,i; 13 | scanf("%d",&t); 14 | int num1,num2; 15 | int sum; 16 | for ( i = 0;i < t; i++ ) { 17 | scanf("%d %d",&num1,&num2); 18 | sum = solveMeSecond(num1,num2); 19 | printf("%d\n",sum); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /song-of-pi.c: -------------------------------------------------------------------------------- 1 | //song-of-pi 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i,j=0,n,t,m=0,k=0,totalelements,flag=0,l; 8 | char str[1000]; 9 | int pi[29] = {3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,3}; 10 | int brr[1000]; 11 | //can i have a large container of coffee right now 12 | scanf("%d",&t); 13 | fflush(stdin); 14 | gets(str); 15 | fflush(stdin); 16 | while(t--) 17 | { 18 | m=0; 19 | flag=0; 20 | gets(str); 21 | fflush(stdin); 22 | n=strlen(str); 23 | l=0; 24 | for(i=0;i 3 | 4 | #define max_n 5000000 5 | int mod=100000; 6 | int memo[max_n+5]; 7 | 8 | int fib(int n) 9 | { 10 | if(n==1)return 0; 11 | if(n==2) return 1; 12 | if(memo[n]>0)return memo[n]; 13 | memo[n]=(fib(n-1) + fib(n-2))%mod; 14 | return memo[n]; 15 | } 16 | int main() 17 | { 18 | int n,i; 19 | for(i=1;i<=5000000;i++) 20 | { 21 | fib(i); 22 | } 23 | 24 | scanf("%d",&n); 25 | printf("%d\n",memo[n]); 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /staircase.cpp: -------------------------------------------------------------------------------- 1 | //staircase 2 | //mandeep singh @msdeep14 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int i,j,k,l,n; 8 | cin>>n; 9 | for(i=1,j=n-1;i<=n,j>=0;i++,j--) 10 | { 11 | for(k=0;k 4 | #define ll long long int 5 | #define fi first 6 | #define se second 7 | #define pb push_back 8 | #define mp make_pair 9 | #define inf INT_MAX 10 | #define vi vector 11 | #define vl vector 12 | #define si set 13 | #define msi multiset 14 | #define fr0(i,a,b) for(int i=a;i=0;i--) 17 | #define frn1(i,a,b) for(int i=b;i>=a;i--) 18 | using namespace std; 19 | const int asize= 100005; 20 | const int mod = 1000000007; 21 | long long int mx=-inf; 22 | long long int mi=inf; 23 | int main(){ 24 | ll t,x,sum=0; 25 | cin>>t; 26 | sum=0; 27 | x=3; 28 | bool flag=true; 29 | while(flag==true){ 30 | sum+=x; 31 | if(t<=sum){ 32 | flag=false; 33 | break; 34 | } 35 | else x=2*x; 36 | } 37 | ll ans=sum-t; 38 | ans=1+ans; 39 | cout< 4 | int main() 5 | { 6 | long long int a,b,op; 7 | scanf("%lld%lld",&a,&b); 8 | if(a%2!=0) 9 | { 10 | op=(a/2)*10 + 2*(b-1); 11 | } 12 | else if(a%2==0) 13 | { 14 | op=(((a-1)/2)*10) + 1 + 2*(b-1); 15 | } 16 | printf("%lld",op); 17 | } 18 | -------------------------------------------------------------------------------- /string-reduction.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/string-reduction 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int numa,numb,numc,i,j,t,len,ans; 9 | char str[101]; 10 | cin>>t; 11 | while(t--) 12 | { 13 | numa=0; 14 | numb=0; 15 | numc=0; 16 | cin>>str; 17 | len=strlen(str); 18 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | /* Head ends here */ 21 | 22 | 23 | long long int stringSimilarity(char a[]) { 24 | long long int sum=0; 25 | long long int n=strlen(a); 26 | vector z(n); 27 | for(long long int i=1,r=0,l=0;ir){ 33 | l=i; 34 | r=i+z[i]-1; 35 | } 36 | } 37 | for(int i=1;i 4 | #include 5 | int main() 6 | { 7 | unsigned long long i,j,x,y,z,cost,costb,costw,b,w,t; 8 | scanf("%llu",&t); 9 | while(t--) 10 | { 11 | scanf("%llu%llu%llu%llu%llu",&b,&w,&x,&y,&z); 12 | costb=b*x; 13 | costw=w*y; 14 | cost=costb+ costw; 15 | if(x==y) 16 | printf("%llu\n",cost); 17 | else if(x 4 | #define ll long long int 5 | #define fe first 6 | #define se second 7 | #define pb push_back 8 | #define mp make_pair 9 | #define inf INT_MAX 10 | #define vi vector 11 | #define vl vector 12 | #define si set 13 | #define msi multiset 14 | #define fr0(i,a,b) for(int i=a;i=a;i--) 17 | #define frn1(i,a,b) for(int i=b;i>a;i--) 18 | using namespace std; 19 | const int asize= 100005; 20 | const int mod = 1000000007; 21 | long long int mx=-inf; 22 | long long int mi=inf; 23 | ll arr[asize]; 24 | ll gf[asize+5],gr[asize+5]; 25 | ll gcd(ll a,ll b){ 26 | while(a&&b){ 27 | if(a>=b) 28 | a=a%b; 29 | else 30 | b=b%a; 31 | } 32 | return a+b; 33 | } 34 | int main(){ 35 | ll n; 36 | cin>>n; 37 | fr0(i,0,n){ 38 | cin>>arr[i]; 39 | } 40 | if(n==1){ 41 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 8 | 9 | 10 | char a[1000][1000]; 11 | char b[1000][1000]; 12 | int i,j,t,l,x,y,x1,y1,z,w,m,n; 13 | 14 | cin>>t; 15 | for(l=0;l>x>>y; 17 | for(i=0;i>a[i][j]; 21 | } 22 | cin>>x1>>y1; 23 | for(i=0;i>b[i][j];} 27 | m=0; 28 | n=0; 29 | for(i=0;i<(x-x1+1);i++) 30 | { 31 | for(j=0;j<(y-y1+1);j++) 32 | 33 | { 34 | if(a[i][j]==b[0][0] && a[i+x1-1][j+y1-1]==b[x1-1][y1-1]) 35 | { 36 | z=i; 37 | m=0; 38 | for(z=z;z<(i+x1);z++) 39 | { 40 | n=0; 41 | for(w=j;w<(j+y1);w++) 42 | {if(a[z][w]!=b[m][n]) 43 | z=i+x1; 44 | else{ 45 | if(z==(i+x1-1) && w==(j+y1-1)){ 46 | cout<<"YES\n"; 47 | i=x-x1+2; 48 | j=y-y1+2;}} 49 | n++;} 50 | m++; 51 | } 52 | } 53 | }} 54 | if(i!=x-x1+3) 55 | cout<<"NO\n"; 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /the-longest-common-subsequence.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/dynamic-programming-classics-the-longest-common-subsequence 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int m[101][101]; 9 | int ver[101]; 10 | int hor[101]; 11 | int store[101]; 12 | int n,l; 13 | cin>>n>>l; 14 | ver[0]=0; 15 | hor[0]=0; 16 | for(int i=1;i<=n;i++) 17 | { 18 | cin>>hor[i]; 19 | } 20 | for(int i=1;i<=l;i++) 21 | { 22 | cin>>ver[i]; 23 | } 24 | for(int i=0;i<=l;i++) 25 | { 26 | for(int j=0;j<=n;j++) 27 | { 28 | m[i][j]=0; 29 | } 30 | } 31 | for(int i=1;i<=l;i++) 32 | { 33 | for(int j=1;j<=n;j++) 34 | { 35 | if(ver[i]==hor[j]) 36 | { 37 | m[i][j]=m[i-1][j-1]+1; 38 | } 39 | else 40 | { 41 | m[i][j]=max(m[i-1][j],m[i][j-1]); 42 | } 43 | } 44 | } 45 | int x,y; 46 | int z=0; 47 | for(int i=l,j=n;i>0 && j>0;j-=y,i-=x) 48 | { 49 | if(ver[i]==hor[j]) 50 | { 51 | store[z]=ver[i]; 52 | z++; 53 | x=1; 54 | y=1; 55 | } 56 | else 57 | { 58 | if(m[i][j-1]<=m[i-1][j]) 59 | { 60 | y=0; 61 | x=1; 62 | } 63 | else 64 | { 65 | x=0; 66 | y=1; 67 | } 68 | } 69 | } 70 | for(int i=z-1;i>=0;i--) 71 | { 72 | cout< 3 | #include 4 | #include 5 | using namespace std; 6 | int dp[1000005],arr[1000005]; 7 | int search(vector &v,int ele){ 8 | int x=0,y=v.size()-1,mid,index=0; 9 | while(x<=y){ 10 | mid=(x+y)/2; 11 | if(ele==v[mid]) return mid; 12 | else if(ele>v[mid]) x=mid+1 ,index=x; 13 | else if(ele>n; 20 | for(int i=0;i>arr[i]; 21 | vector v; 22 | v.push_back(arr[0]); 23 | for(int i=1;i=v[v.size()-1]) v.push_back(arr[i]); 25 | else{ 26 | int x=search(v,arr[i]); 27 | v[x]=arr[i]; 28 | } 29 | } 30 | cout<=0;j--) 38 | if(dp[j]+1>dp[i] && arr[i]>arr[j]) dp[i]=dp[j]+1; 39 | if(dp[i]>max) max=dp[i]; 40 | } 41 | cout< 3 | #include 4 | int main() 5 | { 6 | int i, n, len, count, j,t; 7 | char str[10000]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | scanf("%s",str); 12 | len = strlen(str); 13 | i = 0; 14 | j = len - 1; 15 | count = 0; 16 | while ( i < j) 17 | { 18 | if ( str[i] != str[j] ) 19 | { 20 | if ( str[j] > str[i] ) 21 | count = count + (str[j] -str[i]); 22 | else 23 | count = count + (str[i] -str[j]); 24 | } 25 | i++; 26 | j--; 27 | } 28 | printf("%d\n",count); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /the-maximum-subarray.c: -------------------------------------------------------------------------------- 1 | //the-maximum-subarray 2 | //mandeep singh 3 | #include 4 | #include 5 | int maxsubarray(int a[],int s) 6 | { 7 | int maxsofar=0,i,maxending=0; 8 | 9 | for(i=0;i0) 50 | sum=sum + arr[i]; 51 | } 52 | printf("%d",sum); 53 | 54 | printf("\n"); 55 | } 56 | else if(count==n) 57 | { 58 | max=arr[0]; 59 | for(i=0;i 3 | 4 | int main() 5 | { 6 | int n,sum; 7 | scanf("%d%d",&n,&sum); 8 | int ans=0,a,b,c; 9 | for(a=1;a<=n;a++) 10 | { 11 | for(b=1;b<=n;b++) 12 | { 13 | for(c=1;c<=n;c++) 14 | { 15 | if(a+b+c==sum){ 16 | ans++; 17 | } 18 | } 19 | 20 | } 21 | 22 | } 23 | printf("%d\n",ans); 24 | return 0; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /time-conversion.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/time-conversion 2 | //mandeep singh @msdeep14 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | int main() { 9 | int i,rem,x; 10 | char str[10]; 11 | cin>>str; 12 | 13 | if(str[8]=='P') 14 | { 15 | x=((str[0]-48)*10)+(str[1]-48); 16 | if(x!=12) 17 | {x=x+12; 18 | rem=(x%10)+48; 19 | str[1]=rem; 20 | x=(x/10)+48; 21 | str[0]=x;}} 22 | else 23 | { 24 | x=((str[0]-48)*10)+(str[1]-48);x=((str[0]-48)*10)+(str[1]-48); 25 | if(x==12) 26 | str[1]=48; 27 | str[0]=48; } 28 | for(i=0;i<8;i++) 29 | cout< 4 | #include 5 | int main() 6 | { 7 | int t; 8 | float s,c,x; 9 | scanf("%d",&t); 10 | while(t--) 11 | { 12 | scanf("%f",&x); 13 | s=( x-(pow(x,3))/6 + (pow(x,5))/120 - (pow(x,7))/5040 + (pow(x,9))/362880); 14 | printf("%.3f\n",s); 15 | c=( 1 - (pow(x,2))/2 + (pow(x,4))/24 - (pow(x,6))/720 + (pow(x,8))/40320); 16 | printf("%.3f\n",c); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /two-arrays.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/two-arrays 2 | #include 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int t,n,k; 8 | int arr[1000],brr[1000]; 9 | cin>>t; 10 | while(t--) 11 | { 12 | int flag=0; 13 | cin>>n>>k; 14 | for(int i=0;i>arr[i]; 16 | for(int i=0;i>brr[i]; 18 | sort(arr,arr+n); 19 | sort(brr,brr+n); 20 | for(int i=0;i=k) 23 | continue; 24 | else 25 | { 26 | flag=1; 27 | break; 28 | } 29 | } 30 | if(flag==1) 31 | { 32 | cout<<"NO\n"; 33 | } 34 | else 35 | { 36 | cout<<"YES\n"; 37 | } 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /unbounded-knapsack.cpp: -------------------------------------------------------------------------------- 1 | //https://www.hackerrank.com/challenges/unbounded-knapsack 2 | #include 3 | #include 4 | using namespace std; 5 | int count(int s[],int m,int n) 6 | { 7 | int table[n+1]; 8 | int x; 9 | memset(table,0,sizeof(table)); 10 | table[0]=1; 11 | for(int i=0;i=0;i--) 19 | { 20 | if(table[i]>0) 21 | { 22 | x=i; 23 | break; 24 | } 25 | } 26 | return x; 27 | } 28 | int main() 29 | { 30 | int t,m,k; 31 | int arr[2000]; 32 | cin>>t; 33 | while(t--) 34 | { 35 | cin>>m>>k; 36 | for(int i=0;i>arr[i]; 39 | } 40 | int x=count(arr,m,k); 41 | cout< 3 | int main() 4 | { 5 | int i,j,n,h,k,m,t,p; 6 | int test[15],val[500]; 7 | scanf("%d",&t); 8 | for(m=0;m 4 | int main() 5 | { 6 | int i,j,k,t,flag1,flag2,flag3; 7 | char str[10]; 8 | scanf("%d",&t); 9 | while(t--) 10 | { 11 | flag1=0; 12 | flag2=0; 13 | scanf("%s",str); 14 | for(i=0;i<5;i++) 15 | { 16 | 17 | if(str[i]<=90 && str[i]>=65) 18 | { 19 | //do nothing; 20 | } 21 | else 22 | { 23 | flag1=1; 24 | } 25 | } 26 | for(i=5;i<9;i++) 27 | { 28 | if(str[i]>=48 && str[i]<=57) 29 | { 30 | //do nothing; 31 | } 32 | else 33 | { 34 | flag2=1; 35 | } 36 | 37 | } 38 | flag3=0; 39 | if(str[9]>=65 && str[9]<=90) 40 | { 41 | //do nothing; 42 | } 43 | else 44 | flag3=1; 45 | 46 | if(flag1==0 && flag2==0 && flag3==0) 47 | printf("YES\n"); 48 | else 49 | printf("NO\n"); 50 | } 51 | } 52 | --------------------------------------------------------------------------------