forked from kokonior/HTML-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61code.java
More file actions
43 lines (32 loc) · 664 Bytes
/
61code.java
File metadata and controls
43 lines (32 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// C++14 program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the last
// remaining element from the sequence
int lastRemaining(int n, map<int, int> &dp)
{
// If dp[n] is already calculated
if (dp.find(n) != dp.end())
return dp[n];
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp[n] = 2 * (1 + n / 2 -
lastRemaining(n / 2, dp));
// Return the value of dp[n]
return dp[n];
}
// Driver Code
int main()
{
// Given N
int N = 5;
// Stores the
map<int, int> dp;
// Function call
cout << lastRemaining(N, dp);
return 0;
}
// This code is contributed by mohit kumar 29