-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e7ab0c
commit 53d0ab8
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG{ | ||
public static void main(String args[])throws IOException | ||
{ | ||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(in.readLine()); | ||
while(t-- > 0){ | ||
int N = Integer.parseInt(in.readLine()); | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.sequence(N)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution{ | ||
static long sequence(int n) | ||
{ | ||
long mod=1000000007; | ||
return func(n,1,0,1)%mod; | ||
} | ||
|
||
static long func(int n, int iterations, long sum , int start) | ||
{ | ||
if(n==0) | ||
{ | ||
return sum; | ||
} | ||
|
||
int it = iterations; | ||
|
||
long pro=1, mod=1000000007; | ||
|
||
while(it>0) | ||
{ | ||
pro=pro*start; | ||
pro%=mod; | ||
start++; | ||
it--; | ||
} | ||
|
||
sum+=pro; | ||
|
||
return func(n-1,iterations+1,sum,start); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution | ||
{ | ||
public int majorityElement(int[] nums) | ||
{ | ||
Integer ans = null; | ||
int count = 0; | ||
|
||
for (int num : nums) | ||
{ | ||
if (count == 0) | ||
ans = num; | ||
|
||
count += num == ans ? 1 : -1; | ||
} | ||
|
||
return ans; | ||
} | ||
} |