Count Number of Peaks CodeChef Solution – Code Drive Solution
About Count Number of Peaks CodeChef Solution
- This is a coding contest based on algorithms, data structures, and problem-solving.
- Organizer: The contest is hosted by NIT Trichy.
- Prizes: NA
- Registrations for prizes: NA
Problem: Count Number of Peaks CodeChef Solution
You are given an integer NN where N≤10N≤10. Consider any array AA with length NN where each element can either be 00, 11, or 22, we define f(A)f(A) as the number of extrema in AA. You need to find the sum of f(A)f(A) over all possible arrays AA.
Note:
In an array AA, we consider AiAi as an extremum if it is strictly greater than both of its neighbors (i.e. Ai>Ai−1Ai>Ai−1 and Ai>Ai+1Ai>Ai+1), or if it is strictly smaller than both of its neighbors (i.e. Ai<Ai−1Ai<Ai−1 and Ai<Ai+1Ai<Ai+1). Note that first and last elements are not counted as extremum.
Extrema is the plural of extremum.
Input Format
- The first line of the input contains an integer TT – the number of test cases. The test cases then follow.
- The only line of each test case contains one integer NN
Output Format
For each test case, output on a single the sum of f(A)f(A) over all possible arrays AA.
Constraints
- 1≤T≤101≤T≤10
- 1≤N≤101≤N≤10
Sample Input 1
3
1
3
5
Sample Output 1
0
10
270
Explanation
- Test case 11:
- A=[0]A=[0]. Then f(A)=0f(A)=0.
- A=[1]A=[1]. Then f(A)=0f(A)=0.
- A=[2]A=[2]. Then f(A)=0f(A)=0.
Therefore the answer is 00.
- Test case 22: There are 1010 arrays AA with f(A)=1f(A)=1:
- A=[0,1,0]A=[0,1,0].
- A=[0,2,0]A=[0,2,0].
- A=[0,2,1]A=[0,2,1].
- A=[1,2,0]A=[1,2,0].
- A=[1,2,1]A=[1,2,1].
- A=[2,1,2]A=[2,1,2].
- A=[2,0,2]A=[2,0,2].
- A=[2,0,1]A=[2,0,1].
- A=[1,0,2]A=[1,0,2].
- A=[1,0,1]A=[1,0,1].
The remaining AA’s has f(A)=0f(A)=0. Therefore the answer is 1⋅10=101⋅10=10.
Solution: Count Number of Peaks CodeChef Solution
Count Number of Peaks CodeChef Solution Using C++
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define range(a,b) substr(a,b-a+1)
#define endl "\n"
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
int32_t main(){
FIO;
int t;cin>>t;
while(t--){
int n;cin>>n;
int ans=0;
if(n==3){
cout<<10<<endl;
}
else if(n>3){
ans=pow(3,(n-3))*8*(n-2);
ans=ans+ans/4;
cout<<ans<<endl;
}
else{
cout<<0<<endl;
}
}
}
Count Number of Peaks CodeChef Solution Using Python
# cook your dish here
for _ in range(int(input())):
n = int(input())
s = 0
if(n==3): print(10)
elif(n>3):
s = pow(3,(n-3))*8*(n-2)
s+= s//4
print(s)
else:
print(0)
Contest Details:
- This is an External Rated Contest.
- Duration: 3 Hours
- Start time: 30th December 2021, 20:00 hrs IST
- End time: 30th December 2021, 23:00 hrs IST
You may check your timezone here. - This contest is rated only for Division 2 and Division 3 users. Division 1 users can participate unofficially in this contest.
Get More CodeChef Solution >>
Substring Minimum Function FizzBuzz Solution
Magical Planks Fizzbuzz Solution
Favourite String of Chef CodeChef Solution
Sum this up Code Chef Solution
Game between friends CodeChef Solution