Algorithm Math


http://www.geeksforgeeks.org/count-words-whose-th-letter-either-1-th-th-i1-th-letter-given-word/
Given a string str. The task is to count the words having the same length as str and each letter at the i-th position is either (i-1)-th, i-th, or (i+1)-th position letter of str.
Note: For the first letter consider i-th and (i+1)-th position letter of W. And for last letter consider (i-1)-th and i-th position letter of str.
For any letter at index i, except first and last letter, there are three possible letter i.e (i-1)th, ith or (i+1)th letter of given words. So, if three of them are distinct, we have 3 possibilities. If two of them are same, we have 2 possibilities. And if all are same we have only 1 possibility.
So, traverse the given words and find the possibility of each letter and multiply them.
Similarly, for first letter check the distinct letter at first and second position. And for last position check the distinct letter at last and second last position.
int countWords(char str[], int len)
{
    int count = 1;
    // If word contain single letter, return 1.
    if (len == 1)
        return count;
    // Checking for first letter.
    if (str[0] == str[1])
        count *= 1;
    else
        count *= 2;
    // Traversing the string and multiplying
    // for combinations.
    for (int j=1; j<len-1; j++)
    {
        // If all three letters are same.
        if (str[j] == str[j-1] && str[j] == str[j+1])
            count *= 1;
        // If two letter are distinct.
        else if (str[j] == str[j-1] ||
                 str[j] == str[j+1] ||
                 str[j-1] == str[j+1])
            count *= 2;
        // If all three letter are distinct.
        else
            count *= 3;
    }
    // Checking for last letter.
    if (str[len - 1] == str[len - 2])
        count *= 1;
    else
        count *= 2;
    return count;
}
http://www.geeksforgeeks.org/maximize-sum-n-x-n-upper-left-sub-matrix-given-2n-x-2n-matrix/
Given a 2N x 2N matrix of integers. You are allowed to reverse any row or column any number of times and in any order. The task is to calculate the maximum sum of the upper-left N X N submatrix i.e the sum of elements of submatrix from (0, 0) to (N – 1, N – 1).

To maximize the sum of upper-left submatrix, observe, for each cell of the top-left submatrix, there are four candidates, meaning the corresponding cells in the top-left, top-right, bottom-left, and bottom-right submatrices that it can be swapped with.
Now, observe for each cell, wherever it is, we can swap it with the corresponding candidate value in the top-left submatrix without changing the order of the other cells in the top-left submatrix.The diagram show for an instance where the maximum value of the 4 candidates is in the top-right submatrix. If its is in the bottom-left or bottom-right submatrices, we can first reverse a row or column to put it in the top-right submatrix and then follow the same sequence of operations as shown in diagram.
In this matrix, let say a26 is the maximum of the 4 candidates and a23 must be swapped with a26without changing the order of the cells in the top-left submatrix.
int maxSum(int mat[R][C])
{
  int sum = 0;
  for (int i = 0; i < R/2; i++)
    for (int j = 0; j < C/2; j++)
    {
      int r1 = i;
      int r2 = R - i - 1;
      int c1 = j;
      int c2 = C - j - 1;
       
      // We can replace current cell [i, j]
      // with 4 cells without changing affecting
      // other elements.
      sum += max(max(mat[r1][c1], mat[r1][c2]),
                 max(mat[r2][c1], mat[r2][c2]));
    }
     
    return sum;
}
http://www.geeksforgeeks.org/queries-sum-prime-factor-counts-range/
There are queries. Each query is of the form of L and R. The task is to output sum of number of prime factors of each number in the given range of each query.
The idea is to use the Sieve of Eratosthenes method for counting the number of prime factor of composite numbers. Just like, the inner loop of Sieve of Eratosthenes is used to mark composite number. We can use it for incrementing the prime factor of numbers. Instead of marking each array cell as 0 or 1, we can store the number of prime number of that index. And then for each query, find the sum of array from L to R.

void sieve(int count [])
{
    for (int i = 2; i*i <= MAX; i++)
    {
        // if i is prime
        if (count[i] == 0)
        {
            for (int j = 2*i; j < MAX; j+=i)
                count[j]++;
            // setting number of prime
            // factor of a prime number.
            count[i] = 1;
        }
    }
}
// Returns sum of counts of prime factors in
// range from l to r. This function mainly
// uses count[] which is filled by Sieve()
int query(int count[], int l, int r)
{
    int sum = 0;
    // finding the sum of number of prime
    // factor of numbers in a range.
    for (int i = l; i <= r; i++)
        sum += count[i];
    return sum;
}
http://www.geeksforgeeks.org/reversible-numbers/
Program To check if a number is reversible or not
void checkReversible (int n)
{
    int rev = 0, rem;
    // Calculate reverse of n
    int flag = n;
    while (flag)
    {
        rem = flag % 10;
        rev *= 10;
        rev += rem;
        flag /= 10;
    }
    // Calculate sum of number
    // and its reverse
    int sum = rev + n;
    // Check for reverse number
    // reach digit must be odd
    while (sum && (rem % 2 != 0))
    {
        rem = sum % 10;
        sum /= 10;
    }
    if (sum == 0)
        cout << "Reversible Number";
    else
        cout << "Non-Reversible Number";
}

Program To count total reversible numbers upto n
  • 1 digit number: Any one digit number will add to itself, which always be an even number, And thus there are no solutions.
  • 2 digits number: Both digits must be odd.
    • If a+b > 10 ,then we have a carryover and thus the first digit of the result will have a different parity than the second digit.
    • So, Solutions can only be formed where a+b < 10 and a + b is odd. So, total 20 such numbers are possible.
  • 3 digits number:
    • The middle digit needs to be added to itself. That means that the third digit must have a carryover and be odd.
    • Since the third digit is odd the first digit is odd as well if the second digit does not have a carryover, which happens when the second digit is less than 5, which gives us 20 choices for the first/third digit set and 5 options for the middle.So, totals 100 pairs.
  • 4 digits number: There are two pairs, say the inner and outer pair.
    • If the inner pair has carryover then the outer pair must also have carryover.
    • Otherwise, the two inner pairs will have different parity.
    • If the inner pair has carryover then the outer pair will have different parity since the first digit will end up with a carry over which the last digit would not get.
    • Therefore we have solutions only when none of the pairs have carry over.
    • In total: For the outer pair, this gives us the 20 choices we have seen in the two digit case. And it gives us 30 cases for the inner pair since they can also contain a zero.
      Or in total we get 20*30 = 600 solutions.
  • 5, 9, 13.. digits number: No solution as 1-digit number.
  • 6, 8, 10.. digits number: Same as 2-digits number i.e. if n = 2*k then total solution = 20*30^(k-1).
  • 7, 11, 15.. digits number: Same as 3-digits number i.e if n = 4k + 3 then total solution = 100*500^(k).
Generalizing the Solution:
  • All even numbered digits(2, 4, 6, 8..) have the same formula so we can generalize
    that for some integer k such that n = 2k we have 20*30^(k-1) solutions
    which represents the outer pair along with all the inner pairs.
  • For n (3, 7, 11..) of form 4k + 3 (k is an integer), we have that the middle digit and
    the outer pair gives us 5 and 20 options, as in the case of 3 digit number.
    Then we have sets of internal pairs which gives us 20 and 25 solutions.
    So that means we can generalize it to 20*5*(20*25)^(k) = 100*500^(k).
  • For n of form 4k+ 1 which means 1, 5, 9.. none of these have any solutions.

void countReversible (int n)
{
    int count = 0;
    // Calculate counts of
    // reversible number of 1 to n-digits
    for ( int i = 1; i <= n; i++)
    {
        // All four possible cases and their formula
        switch (i % 4)
        {
        // for i of form 2k
        case 0:
        case 2:
            count += 20 * pow( 30, ( i / 2 - 1));
            break;
        // for i of form 4k + 3
        case 3:
            count += 100 * pow ( 500, i / 4 );
            break;
        // for i of form 4k + 1 no solution
        case 1:
            break;
        }
    }
    cout << count;
}
http://www.geeksforgeeks.org/program-for-goldbachs-conjecture-two-primes-with-given-sum/
Goldbach’s conjecture is one of the oldest and best-known unsolved problems in number theory of mathematics. Every even integer greater than 2 can be expressed as the sum of two primes.

  1. Find the prime numbers using Sieve of Sundaram
  2. Check if entered number is an even number greater than 2 or not, if no return.
  3. If yes, then one by one subtract a prime from N and then check if the difference is also a prime, if yes then express it as a sum.

// Utility function for Sieve of Sundaram
void sieveSundaram()
{
    // In general Sieve of Sundaram, produces primes smaller
    // than (2*x + 2) for a number given number x. Since
    // we want primes smaller than MAX, we reduce MAX to half
    // This array is used to separate numbers of the form
    // i + j + 2*i*j from others where 1 <= i <= j
    bool marked[MAX/2 + 100] = {0};
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 2*i+1
    for (int i=1; i<=(sqrt(MAX)-1)/2; i++)
        for (int j=(i*(i+1))<<1; j<=MAX/2; j=j+2*i+1)
            marked[j] = true;
    // Since 2 is a prime number
    primes.push_back(2);
    // Print other primes. Remaining primes are of the
    // form 2*i + 1 such that marked[i] is false.
    for (int i=1; i<=MAX/2; i++)
        if (marked[i] == false)
            primes.push_back(2*i + 1);
}
// Function to perform Goldbach's conjecture
void findPrimes(int n)
{
    // Return if number is not even or less than 3
    if (n<=2 || n%2 != 0)
    {
        cout << "Invalid Input \n";
        return;
    }
    // Check only upto half of number
    for (int i=0 ; primes[i] <= n/2; i++)
    {
        // find difference by subtracting current prime from n
        int diff = n - primes[i];
        // Search if the difference is also a prime number
        if (binary_search(primes.begin(), primes.end(), diff))
        {
            // Express as a sum of primes
            cout << primes[i] << " + " << diff << " = "
                 << n << endl;
            return;
        }
    }
}
http://www.geeksforgeeks.org/number-maximum-number-prime-factors/
Given an integer N. The task is to find a number that is smaller than or equal to N and has maximum prime factors. In case there are two or more numbers with same maximum number of prime factors, find the smallest of all.
Use sieve method to count number of prime factor of each number less than N. And find the minimum number having maximum count.
int maxPrimefactorNum(int N)
{
    int arr[N + 5];
    memset(arr, 0, sizeof(arr));
    // Sieve of eratosthenes method to count
    // number of prime factors.
    for (int i = 2; i*i <= N; i++)
    {
        if (!arr[i])
            for (int j = 2*i; j <= N; j+=i)
                arr[j]++;
        arr[i] = 1;
    }
    int maxval = 0, maxint = 1;
    // Finding number having maximum number
    // of prime factor.
    for (int i = 1; i <= N; i++)
    {
        if (arr[i] > maxval)
        {
            maxval = arr[i];
            maxint = i;
        }
    }
    return maxint;
}

Generate all prime number before N using Sieve. Now, multiply consecutive prime numbers (starting from first prime number) one after another until the product is less than N.
int maxPrimefactorNum(int N)
{
    bool arr[N + 5];
    memset(arr, true, sizeof(arr));
    // Sieve of eratosthenes
    for (int i = 3; i*i <= N; i += 2)
    {
        if (!arr[i])
            for (int j = i*i; j <= N; j+=i)
                arr[j] = false;
    }
    // Storing prime numbers.
    vector<int> prime;
    prime.push_back(2);
    for(int i = 3; i <= N; i += 2)
        if(arr[i])
            prime.push_back(i);
    // Generating number having maximum prime factors.
    int i = 0, ans = 1;
    while (ans*prime[i] <= N && i < prime.size())
    {
        ans *= prime[i];
        i++;
    }
    return ans;
}

http://www.geeksforgeeks.org/nearest-element-least-one-common-prime-factor/
Given an array arr[], find nearest element for every element such that there is at least one common prime factor. In output, we need to print position of closest element.
Input: arr[] = {2, 9, 4, 3, 13}
Output: 3 4 1 2 -1
Explanation : 
Closest element for 1st element is 3rd. 
=>Common prime factor of 1st and 3rd elements
  is 2.
Closest element for 2nd element is 4th.
=>Common prime factor of 2nd and 4th elements
  is 3.

We find prime factors of all array elements. To quickly find prime factors, we use Sieve of Eratosthenes. For every element, we consider all prime factors and keep track of closest element with common factor.
Time complexity: O(MAX * log(log (MAX) ) )
Auxiliary space: O(MAX)
const int MAX = 100001;
const int INF = INT_MAX;
int primedivisor[MAX], dist[MAX], pos[MAX], divInd[MAX];
vector<int> divisors[MAX];
// Pre-computation of smallest prime divisor
// of all numbers
void sieveOfEratosthenes()
{
    for (int i=2; i*i < MAX; ++i)
    {
        if (!primedivisor[i])
            for (int j = i*i; j < MAX; j += i)
                primedivisor[j] = i;
    }
    // Prime number will have same divisor
    for (int i = 1; i < MAX; ++i)
        if (!primedivisor[i])
            primedivisor[i] = i;
}
// Function to calculate all divisors of
// input array
void findDivisors(int arr[], int n)
{
    for (int i=0; i<MAX; ++i)
        pos[i] = divInd[i] = -1, dist[i] = INF;
    for (int i=0; i<n; ++i)
    {
        int num = arr[i];
        while (num > 1)
        {
            int div = primedivisor[num];
            divisors[i].push_back(div);
            while (num % div == 0)
                num /= div;
        }
    }
}
void nearestGCD(int arr[], int n)
{
    // Pre-compute all the divisors of array
    // element by using prime factors
    findDivisors(arr, n);
    // Traverse all elements,
    for (int i=0; i<n; ++i)
    {
        // For every divisor of current element,
        // find closest element.
        for (auto &div: divisors[i])
        {
            // Visit divisor if not visited
            if (divInd[div] == -1)
                divInd[div] = i;
            else
            {
                // Fetch the index of visited divisor
                int ind = divInd[div];
                // Update the divisor index to current index
                divInd[div] = i;
                // Set the minimum distance
                if (dist[i] > abs(ind-i))
                {
                    // Set the min distance of current
                    // index 'i' to nearest one
                    dist[i] = abs(ind-i);
                    // Add 1 as indexing starts from 0
                    pos[i] = ind + 1;
                }
                if (dist[ind] > abs(ind-i))
                {
                    // Set the min distance of found index 'ind'
                    dist[ind] = abs(ind-i);
                    // Add 1 as indexing starts from 0
                    pos[ind] = i + 1;
                }
            }
        }
    }
}
Common prime factor will only exist if GCD of these two numbers will greater than 1. Simple brute force is to run the two loops one inside the another and iterate one by one from each index to both the sides simultaneously and find the gcd which is greater than 1. Whenever we found the answer then just break the loop and the print. If we reached the end of array after traversing both the sides then simply print -1.
void nearestGcd(int arr[], int n)
{
    // Loop covers the every element of arr[]
    for (int i=0; i<n; ++i)
    {
        int closest = -1;
        // Loop that covers from 0 to i-1 and i+1
        // to n-1 indexes simultaneously
        for (int j=i-1, k=i+1; j>0 || k<=n; --j, ++k)
        {
            if (j>=0 && __gcd(arr[i], arr[j]) > 1)
            {
                closest = j+1;
                break;
            }
            if (k<n && __gcd(arr[i], arr[k])>1)
            {
                closest = k+1;
                break;
            }
        }
        // print position of closest element
        cout << closest << " ";
    }
}
http://www.geeksforgeeks.org/summation-gcd-pairs-n/
Given a number N, find sum of all GCDs that can be formed by selecting all the pairs from 1 to N.

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) å°ŗå–ę³• (11) ꌑꈘē؋åŗč®¾č®”ē«žčµ› (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) ē©·ē«­ęœē“¢ (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) č“ę”„ęÆ (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts