# Ways to choose balls such that at least one ball is chosen

Given an integer **N**, the task is to find the ways to choose some balls out of the given **N** balls such that at least one ball is chosen. Since the value can be large so print the value modulo **1000000007**.

**Example:**

Input:N = 2

Output:3

The three ways are “*.”, “.*” and “**” where ‘*’ denotes

the chosen ball and ‘.’ denotes the ball which didn’t get chosen.

Input:N = 30000

Output:165890098

**Approach:** There are **N** balls and each ball can either be chosen or not chosen. Total number of different configurations is **2 * 2 * 2 * … * N**. We can write this as **2 ^{N}**. But the state where no ball is chosen has to be subtracted from the answer. So, the result will be

**(2**.

^{N}– 1) % 1000000007Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MOD = 1000000007; ` ` ` `// Function to return the count of ` `// ways to choose the balls ` `int` `countWays(` `int` `n) ` `{ ` ` ` ` ` `// Calculate (2^n) % MOD ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `ans *= 2; ` ` ` `ans %= MOD; ` ` ` `} ` ` ` ` ` `// Subtract the only where ` ` ` `// no ball was chosen ` ` ` `return` `((ans - 1 + MOD) % MOD); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << countWays(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` `static` `int` `MOD = ` `1000000007` `; ` ` ` `// Function to return the count of ` `// ways to choose the balls ` `static` `int` `countWays(` `int` `n) ` `{ ` ` ` ` ` `// Calculate (2^n) % MOD ` ` ` `int` `ans = ` `1` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `ans *= ` `2` `; ` ` ` `ans %= MOD; ` ` ` `} ` ` ` ` ` `// Subtract the only where ` ` ` `// no ball was chosen ` ` ` `return` `((ans - ` `1` `+ MOD) % MOD); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(countWays(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `MOD ` `=` `1000000007` ` ` `# Function to return the count of ` `# ways to choose the balls ` `def` `countWays(n): ` ` ` ` ` `# Return ((2 ^ n)-1) % MOD ` ` ` `return` `(((` `2` `*` `*` `n) ` `-` `1` `) ` `%` `MOD) ` ` ` `# Driver code ` `n ` `=` `3` `print` `(countWays(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `MOD = 1000000007; ` ` ` `// Function to return the count of ` `// ways to choose the balls ` `static` `int` `countWays(` `int` `n) ` `{ ` ` ` ` ` `// Calculate (2^n) % MOD ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `ans *= 2; ` ` ` `ans %= MOD; ` ` ` `} ` ` ` ` ` `// Subtract the only where ` ` ` `// no ball was chosen ` ` ` `return` `((ans - 1 + MOD) % MOD); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(countWays(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

7

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Ways to arrange Balls such that adjacent balls are of different types
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- In how many ways the ball will come back to the first boy after N turns
- Number of ways of scoring R runs in B balls with at most W wickets
- Number of ways to choose K intersecting line segments on X-axis
- Number of ways to choose a pair containing an even and an odd number from 1 to N
- Find sum of f(s) for all the chosen sets from the given array
- Probability for three randomly chosen numbers to be in AP
- Check if N can be represented as sum of integers chosen from set {A, B}
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Puzzle | Weight of Heavy Ball
- Find the position of box which occupies the given ball
- Probability of getting a perfect square when a random number is chosen in a given range
- Two Balls Reachability Game

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.