• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games
You are here: Home / Archives for Count of Numbers in a Range where digit d occurs exactly K times

Count of Numbers in a Range where digit d occurs exactly K times

Count pairs of non-overlapping palindromic sub-strings of the given string

Leave a Comment


#include <bits/stdc++.h>

using namespace std;

#define N 100

  

void pre_process(bool dp[N][N], string s)

{

  

    

    int n = s.size();

  

    

    

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++)

            dp[i][j] = false;

    }

  

    

    for (int j = 1; j <= n; j++) {

  

        

        

        for (int i = 0; i <= n - j; i++) {

  

            

            if (j <= 2) {

  

                

                if (s[i] == s[i + j - 1])

                    dp[i][i + j - 1] = true;

            }

  

            

            else if (s[i] == s[i + j - 1])

                dp[i][i + j - 1] = dp[i + 1][i + j - 2];

        }

    }

}

  

int countPairs(string s)

{

  

    

    bool dp[N][N];

    pre_process(dp, s);

    int n = s.length();

  

    

    int left[n];

    memset(left, 0, sizeof left);

  

    

    int right[n];

    memset(right, 0, sizeof right);

  

    

    left[0] = 1;

  

    

    

    for (int i = 1; i < n; i++) {

  

        for (int j = 0; j <= i; j++) {

  

            if (dp[j][i] == 1)

                left[i]++;

        }

    }

  

    

    right[n - 1] = 1;

  

    

    

    for (int i = n - 2; i >= 0; i--) {

  

        right[i] = right[i + 1];

  

        for (int j = n - 1; j >= i; j--) {

  

            if (dp[i][j] == 1)

                right[i]++;

        }

    }

  

    int ans = 0;

  

    

    for (int i = 0; i < n - 1; i++)

        ans += left[i] * right[i + 1];

  

    return ans;

}

  

int main()

{

    string s = "abacaba";

    cout << countPairs(s);

  

    return 0;

}



Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, About Us, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, All Algorithms, All Data Structures, Analysis of Algorithms, Aptitude, Arrange first N natural numbers such that absolute difference between all adjacent elements > 1, Array, Backtracking, Binary Search Tree, Binary Tree, Bit Algorithms, Branch & Bound, C, Campus Ambassador Program, Campus Geek of the Month, Careers, Check if a word exists in a grid or not, Combinatorial, Company Prep, Company-wise, Competitive Programming, Compiler Design, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Contact Us, Contests, contribute.geeksforgeeks.org, Core Subjects ►, Count All Palindromic Subsequence in a given String, Count all Prime Length Palindromic Substrings, Count number of equal pairs in a string, count number of substrings that start and end with 1., Count Numbers with N digits which consists of odd number of 0's, Count of distinct substrings of a string using Suffix Array, Count of distinct substrings of a string using Suffix Trie, Count of Numbers in a Range divisible by m and having digit d in even positions, Count of Numbers in a Range where digit d occurs exactly K times, Count of Numbers in Range where first digit is equal to last digit of the number, Count of Numbers in Range where the number does not contain more than K non zero digits, Count of Palindromic substrings in an Index range, Count of sub-sets of size n with total element sum divisible by 3, Count of substrings of a binary string containing K ones, Count palindromic characteristics of a String, Count paths with distance equal to Manhattan distance, Count the number of special permutations, Count the number of vowels occurring in all the substrings of given string, Count unordered pairs (i, Courses, CS Subjects, CS Subjects ▼, CS Subjectwise ►, CSS, Data Structures, DBMS, Deletions of "01" or "10" in binary string to make it free from "01" or "10", Design Patterns, Digital Electronics, Divide and Conquer, DS ▼, Engg. Mathematics, Experienced Interviews, Find a palindromic string B such that given String A is a subsequense of B, Find the count of palindromic sub-string of a string in its sorted form, Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE Notes, GATE Official Papers, GBlog, Geek of the Month, Geek on the Top, Generating Lyndon words of length n, Geometric Algorithms, Given a binary string, Graph, Graph Algorithms, Greedy Algorithms, Hashing, Heap, HTML, HTML & XML, ide.geeksforgeeks.org, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO CS Exam, j) such that product of a[i] and a[j] is power of two, Java, Java Program to print distinct permutations of a string, JavaScript, jQuery, K length words that can be formed from given characters without repetition, Languages, Languages ►, Languages ▼, Last Minute Notes, LinkedList, Longest subsequence such that adjacent elements have at least one common digit, Machine Learning, Make Binary Search Tree, Make palindromic string non-palindromic by rearranging its letters, Mathematical Algorithms, Matrix, Maximum sum from three arrays such that picking elements consecutively from same is not allowed, Microprocessor, Minimum cuts required to convert a palindromic string to a different palindromic string, Multiple Choice Quizzes, Number of moves required to guess a permutation., Number of permutations of a string in which all the occurrences of a given character occurs together, Number of submatrices with all 1s, Number of submatrices with OR value 1, Number of trailing zeroes in base 16 representation of N!, Number of triangles that can be formed with given N points, Number of unique pairs in an array, Number of ways of scoring R runs in B balls with at most W wickets, Number of ways to go from one point to another in a grid, Number of ways to select a node from each connected component, Operating Systems, palindrome, Pattern Searching, Permutations of string such that no two vowels are adjacent, PHP, Placement Course, Practice, Practice Company Questions, Privacy Policy, Program Output, Program to calculate the value of nCr Efficiently, Program to calculate the value of nPr, Project, Puzzles, Python, Queries to check if substring[L…R] is palindrome or not, Queue, Quizzes ▼, Randomized Algorithms, Rearrange the string to maximize the number of palindromic substrings, Recommended: Please try your approach on, School Programming, Searching Algorithms, Skip to content, Software Engineering, Some rights reserved, Sorting Algorithms, SQL, Stack, Strings, Students ▼, Subjective Questions, substring, Suggest an Article, Sum of (maximum element - minimum element) for all the subsets of an array., Sum of all Submatrices of a Given Matrix, Sum of bitwise AND of all submatrices, Sum of Bitwise-OR of all Submatrices, Sum of XOR of all subarrays, Testimonials, Theory of Computation, Top Topics, Topic-wise, Topicwise ►, Total number of subsets in which the product of the elements is even, Tree based DS ►, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, Understanding The Coin Change Problem With Dynamic Programming, Video Tutorials, Videos, Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1, Ways to paint N paintings such that adjacent paintings don't have same colors, Web Technology, What’s Difference?, Write an Article, Write Interview Experience

Program to calculate the value of nPr

Leave a Comment


Given two numbers n and r, the task is to find the value of nPr.

nPr represents n permutation r which is calculated as n!/(n-k)!. Permutation refers to the process of arranging all the members of a given set to form a sequence. The number of permutations on a set of n elements is given by n!, where “!” represents factorial.

nPr = n! / (n - r)!

Program:

  

import java.util.*;

  

public class GFG {

  

    static int fact(int n)

    {

        if (n <= 1)

            return 1;

        return n * fact(n - 1);

    }

  

    static int nPr(int n, int r)

    {

        return fact(n) / fact(n - r);

    }

  

    public static void main(String args[])

    {

        int n = 5;

        int r = 2;

  

        System.out.println(n + "P" + r + " = "

                           + nPr(n, r));

    }

}


Optimization for multiple queries of nPr

If there are multiple queries for nPr, we may precompute factorial values and use same for every call. This would avoid computation of same factorial values again and again.




If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :


thumb_up
Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.


Post navigation


Previous

first_page Check if the given array is mirror-inverse










Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, About Us, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, All Algorithms, All Data Structures, Analysis of Algorithms, Aptitude, Arrange first N natural numbers such that absolute difference between all adjacent elements > 1, Array, Backtracking, Binary Search Tree, Binary Tree, Bit Algorithms, Branch & Bound, C, C Program for factorial of a number, C program to calculate the value of nPr, Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles, Campus Ambassador Program, Campus Geek of the Month, Careers, Check if the given array is mirror-inverse, Combinatorial, Company Prep, Company-wise, Competitive Programming, Compiler Design, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Contact Us, Contests, contribute.geeksforgeeks.org, contributed articles, Core Subjects ►, Count number of triplets with product equal to given number with duplicates allowed, Count Numbers with N digits which consists of odd number of 0's, Count of Numbers in a Range divisible by m and having digit d in even positions, Count of Numbers in a Range where digit d occurs exactly K times, Count of Numbers in Range where first digit is equal to last digit of the number, Count of Numbers in Range where the number does not contain more than K non zero digits, Count of sub-sets of size n with total element sum divisible by 3, Count ordered pairs of positive numbers such that their sum is S and XOR is K, Count paths with distance equal to Manhattan distance, Count the number of special permutations, Count unordered pairs (i, Count ways to distribute m items among n people, Courses, CS Subjects, CS Subjects ▼, CS Subjectwise ►, CSS, Data Structures, DBMS, Deletions of "01" or "10" in binary string to make it free from "01" or "10", Design Patterns, Digital Electronics, Divide and Conquer, DS ▼, Engg. Mathematics, Experienced Interviews, Factorial, Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE Notes, GATE Official Papers, GBlog, Geek of the Month, Geek on the Top, Generating Lyndon words of length n, Geometric Algorithms, Graph, Graph Algorithms, Greedy Algorithms, Hashing, Hashing in Java, Heap, HTML, HTML & XML, ide.geeksforgeeks.org, Implementing a Linked List in Java using Class, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO CS Exam, j) such that product of a[i] and a[j] is power of two, Java, Java Program for Cutting a Rod | DP-13, Java Program for Program for array rotation, Java Program for Program to calculate area of a Tetrahedron, Java Program for Program to find area of a circle, Java Program for QuickSort, Java Program to print distinct permutations of a string, Java Program to take Screenshots, Java Programs, JavaScript, jQuery, K length words that can be formed from given characters without repetition, Languages, Languages ►, Languages ▼, Last Minute Notes, LinkedList, Machine Learning, Mathematical Algorithms, Matrix, Microprocessor, Min Heap in Java, Minimum and Maximum number of pairs in m teams of n people, Multiple Choice Quizzes, Number of moves required to guess a permutation., Number of permutations of a string in which all the occurrences of a given character occurs together, Number of Simple Graph with N Vertices and M Edges, Number of submatrices with OR value 1, Number of trailing zeroes in base 16 representation of N!, Number of triangles that can be formed with given N points, Number of unique pairs in an array, Operating Systems, Pattern Searching, permutation, Permutations of string such that no two vowels are adjacent, PHP, Placement Course, Practice, Practice Company Questions, Privacy Policy, Program for factorial of a number, Program Output, Program to Add Two Complex Numbers, Program to calculate the value of nCr Efficiently, Program to calculate value of nCr, Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n), Program to validate a user using JSP, Project, Puzzles, Python, Queue, Quick Sort vs Merge Sort, Quizzes ▼, Randomized Algorithms, Recommended: Please try your approach on, Samsung Semiconductor Institute of Research(SSIR Software) Intern/FTE | Set-2, School Programming, Searching Algorithms, Skip to content, Software Engineering, Some rights reserved, Sorting Algorithms, SQL, Stack, Strings, Students ▼, Subjective Questions, Suggest an Article, Sum of all Submatrices of a Given Matrix, Sum of bitwise AND of all submatrices, Sum of Bitwise-OR of all Submatrices, Testimonials, Theory of Computation, Top Topics, Topic-wise, Topicwise ►, Total number of subsets in which the product of the elements is even, Tree based DS ►, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, Understanding The Coin Change Problem With Dynamic Programming, Video Tutorials, Videos, Walmart Labs Interview Experience ( On Campus FT + 6 month Internship ), Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1, Ways to paint N paintings such that adjacent paintings don't have same colors, Web Technology, What’s Difference?, Write an Article, Write Interview Experience

Primary Sidebar

Recent Posts

  • Find the minimum spanning tree with alternating colored edges
  • Engineering Chemistry Questions and Answers – Application of Colloids
  • MongoDB Beginners Tutorial
  • 10 Projects That Every Developer Should Lay Their Hands-On
  • Engineering Chemistry Questions and Answers – Detergents
  • Privacy Policy
  • About
  • Contact US

© 2019 ProProgramming
 Privacy Policy About Contact Us