Maximizing the Function – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ replace HackerRank Solution
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int n; int q;
while(~scanf("%d%d", &n, &q)) {
vector<int> a(n);
for(int i = 0; i < n; ++ i)
scanf("%d", &a[i]);
vector<int> sum(n + 1);
rep(i, n) sum[i + 1] = sum[i] ^ a[i];
vector<int> sum2(n + 2);
rep(i, n + 1) sum2[i + 1] = sum2[i] + sum[i];
rep(ii, q) {
int x; int y; int k;
scanf("%d%d%d", &x, &y, &k), ++ y;
int cnt0 = sum2[y + 1] - sum2[x];
int cnt1 = (y + 1 - x) - cnt0;
if(cnt0 > cnt1) swap(cnt0, cnt1);
ll ans = k == 0 ? (ll)cnt0 * cnt1 :
(ll)((y + 1 - x) / 2) * ((y + 1 - x + 1) / 2);
printf("%lld\n", ans);
}
}
return 0;
}
Java rep HackerRank Solution
import java.io.*;
import java.util.*;
public class C {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
void solve() throws IOException {
int n = nextInt();
int q = nextInt();
int[] b = new int[n + 1];
for (int i = 0; i < n; i++) {
int x = nextInt();
b[i + 1] = b[i] ^ x;
}
int[] c = new int[n + 2];
for (int i = 0; i < n + 1; i++) {
c[i + 1] = c[i] + b[i];
}
while (q-- > 0) {
int x = nextInt();
int y = nextInt();
int k = nextInt();
int len = y - x + 2;
int ones;
if (k == 0) {
ones = c[y + 2] - c[x];
} else {
ones = len / 2;
}
out.println((long)ones * (len - ones));
}
}
C() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new C();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
Python 3 rep HackerRank Solution
#!/usr/bin/python3
def init(A,n):
retA = [0]*(n+1);
B = [0]*(n+1)
tmp = 0
for i in range(n):
tmp ^= A[i]
B[i+1] = tmp
retA[i+1] += retA[i] + tmp
return retA,B
if __name__ == "__main__":
n,q = list(map(int,input().rstrip().split()))
A = list(map(int,input().rstrip().split()))
newA, B = init( A, n )
while q > 0 :
x,y,k = list(map(int,input().rstrip().split()))
num = newA[y+1] - newA[x]
if B[x]:
num = (y + 1 - x) - num
size = y - x + 2
num = abs(int(size/2)) if k else num
print( num*(size-num) )
q-=1
Python 2 rep HackerRank Solution
__author__, (N, Q), A = "LaughDonor", map(int, raw_input().split()), map(int, raw_input().split())
xor = A[0]
for i, a in enumerate(A[1:], 1):
if a:
xor ^= 1
A[i] = A[i - 1] + xor
for x, y, k in (map(int, raw_input().split()) for _ in xrange(Q)):
n = y - x + 1
if k:
n, c = n / 2, (1 + n / 2) * (n & 1)
else:
c = A[y] - A[x - 2] * (x > 1)
print c + (n * (n + 1) if k else c * (n - c))
C rep HackerRank Solution
#include <stdio.h>
#include <stdlib.h>
int a[500000],odd_sum[500000],even_sum[500000];
int main(){
int n,q,x,y,k,odd,even,i;
scanf("%d%d",&n,&q);
for(i=0;i<n;i++){
scanf("%d",a+i);
if(i){
a[i]^=a[i-1];
if(a[i]){
odd_sum[i]=odd_sum[i-1]+1;
even_sum[i]=even_sum[i-1];
}
else{
odd_sum[i]=odd_sum[i-1];
even_sum[i]=even_sum[i-1]+1;
}
}
else
if(a[i]){
odd_sum[i]=1;
even_sum[i]=0;
}
else{
odd_sum[i]=0;
even_sum[i]=1;
}
}
while(q--){
scanf("%d%d%d",&x,&y,&k);
if(k)
printf("%lld\n",(y-x+2)/2*((long long)(y-x+3)/2));
else{
odd=odd_sum[y];
even=even_sum[y];
if(x){
odd-=odd_sum[x-1];
even-=even_sum[x-1];
}
if(!x || !a[x-1])
printf("%lld\n",odd*(long long)(even+1));
else
printf("%lld\n",even*(long long)(odd+1));
}
}
return 0;
}
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below