Posts

AtCoder Roulette

  #include < bits / stdc ++. h > using namespace std ;   const int high = 205 ;   int ar [ high ][ high ], ans [ high ], allC [ high ], mp [ high ][ high ];   struct data { int indx ; int c ; } sarr [ high ];   void sol ( int N ) { int C , clen = 0 ; for ( int i = 0 ; i < N ; i ++) { cin >> C ; allC [ clen ++] = C ; for ( int j = 0 ; j < C ; j ++) { cin >> ar [ i ][ j ]; } } int X , minC = 37 , slen = 0 ; cin >> X ; for ( int i = 0 ; i < N ; i ++) { for ( int j = 0 ; j < allC [ i ]; j ++) { if ( ar [ i ][ j ] == X ) { if ( mp [ i ][ j ] == 0 ) { mp [ i ][ j ] = 1 ; sarr [ slen ]. indx = i + 1 ; sarr [ slen ++]. c = allC [ i ]; if ( minC > allC [ i ]) { minC = allC [ i ];

AtCoder 3.14

#include < bits / stdc ++. h > using namespace std ;   void sol ( int N , string pi ) { int len = pi . length (); N = 2 + N ; for ( int i = 0 ; i < N ; i ++) { cout << pi [ i ]; } }   int main () { string pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679" ; int N ; cin >> N ; sol ( N , pi ); return 0 ; }

AtCode A - To Be Saikyo

 Problem Description Link:  https://atcoder.jp/contests/abc313/tasks/abc313_a Accepted Solution #include <bits/stdc++.h> using namespace std ; int sol ( int N ) {     int i , a , mx = - 1 , first = 0 , cnt = 0 ;     cin >> first ;     mx = max ( mx , first );     for ( i = 1 ; i < N ; i ++ ) {         cin >> a ;         if ( a == first ) {             cnt += 1 ;         }         mx = max ( mx , a );     }         int x = mx - first ;     if ( cnt == 0 && mx != first ) {         return x + 1 ;     }     else if ( cnt == 0 && mx == first ) {         return 0 ;     }     else if ( cnt >= 1 && cnt != ( N - 1 )) {         return x + 1 ;     }     else {         return x + 1 ;     } } int main () {     int N ;     cin >> N ;     int ans = sol ( N );     cout << ans << " \n " ;     return 0 ; }

LeetCode Roman to Integer

 Problem Description Link:  https://leetcode.com/problems/roman-to-integer/ Accepted Solution /* The solution to the problem is straightforward. You have to find the exceptional numbers, such as 4, 9, 40, 90, 400, and 900, carefully and do simply for the rest. */ class Solution { public:     int getNum ( char ch ) {     if (ch == 'I' )     {         return 1 ;     }     else if (ch == 'V' )     {         return 5 ;     }     else if (ch == 'X' )     {         return 10 ;     }     else if (ch == 'L' )     {         return 50 ;     }     else if (ch == 'C' )     {         return 100 ;     }     else if (ch == 'D' )     {         return 500 ;     }     else if (ch == 'M' )     {         return 1000 ;     }     return 0 ;     }     int romanToInt ( string s ) {                 string roman = s;         int i= 0 , len = roman . length (), num= 0 ;     for (i= 1 ; i<len; i++)     {         //cout << getNum

LeetCode Reverse Integer

 Problem Description:  https://leetcode.com/problems/reverse-integer/description/ Accepted Solution /* I have learned some crucial things during solving the problem. For example, if you want to check integer overflows for several operations, you have to manage the following conditions: int a = <something>;  int x = <something>; if (x < 0 && a > INT_MAX + x) // a - x would overflow   // `a - x` would overflow if (x > 0 && a < INT_MIN + x) // a - x would underflow if (a == -1 && x == INT_MIN) // a * x can overflow if (x == -1 && a == INT_MIN) // a * x (or a / x) can overflow if (x != 0 && a > INT_MAX / x) // a * x would overflow if (x != 0 && a < INT_MIN / x) // a * x would underflow Hence, check the appropriate conditions in perfect positions at your code. Also, keep your attention when you are dealing with a negative number. Moreover, a negative number is unsuitable for direct calculation or div

LeetCode Palindrome Number

 Problem Description Link:  https://leetcode.com/problems/palindrome-number/ Accepted Solution /* The solution is easy, and I believe everyone knows the process. However, one problem is that x is a number. If it is a string, in that case, we can get the result after reversing it. Therefore, to solve the problem, we have to find the digits by modulating 10, store them in an array, and finally compare from both sides. If we find any discrepancy, we can tell it is not a palindrome. Note that any negative integer would never be a palindrome since it should contain a minus (-) sign. */ class Solution { public:     bool isPalindrome ( int x ) {         int digit [ 50 ], len= 0 ;         bool f = true ;         if (x < 0 ) {             return false ;         }         while (x> 0 )         {             digit [len++] = x% 10 ;             x/= 10 ;         }         for ( int i= 0 , j=len- 1 ; i<len; i++, j--)         {             if ( digit [i] != digit [j])             {  

LeetCode Add Two Numbers

 Problem Description Link:  https://leetcode.com/problems/add-two-numbers/description/ Accepted Solution /*  The solution technique is simple. You are given two linked lists. Find which is the larger or smaller. If both of them are similar in length, no problem. However, if you find any discrepancy in length, you have to insert extra zeros just back to the numbers. Finally, reverse each linked list, perform a fundamental summation, and store the results in another linked list, as you must return a linked list from the desired function. For good understanding, I have utilized two arrays while performing summation. Although the process is straight, brainstorming is necessary for coding. */ /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     struct ListNode *next;  * };  */ void insert_data ( struct ListNode ** head , int data ) {     struct ListNode *ptr, *tmp;     tmp = ( struct ListNode*) malloc ( sizeof ( struct ListNode));         ptr = *head;