CCC March
CCC March
C++
-------------------------------------------------------------
int maxLength = 0;
maxLength = length;
int maxProfit = 0;
for (int i = 1; i < maxLength; i++) {
int sumOfLengths = 0;
int sumOfCutCounts = 0;
int sumOfCutWastes = 0;
for (int length : lengths) {
sumOfLengths += length;
if (length % i == 0) {
sumOfCutCounts += (length/i - 1);
} else {
sumOfCutCounts += (length/i);
}
sumOfCutWastes += (length%i);
}
}
return maxProfit;
}
-------------------------------------------------------------
Permutations Divisible by 8
Python 3
def solve(n):
p = list(pr(n, 3))
for i in p:
if (int(''.join(i)) % 8 == 0):
return 1
return 0
for _ in range(int(input())):
n = input()
if len(n) <= 2:
n = list(n)
if len(n) == 1 and int(''.join(n)) % 8 == 0:
print('YES')
elif len(n) == 2 and (int(''.join(n)) % 8 == 0 or int(''.join(reversed(n))) % 8 == 0):
print('YES')
else:
print('NO')
continue
if solve(n):
print('YES')
else:
print('NO')
-------------------------------------------------------------
Efficient Janitor
Python 3
def efficientJanitor(weight):
# Write your code here
count = 0
i,j = 0,len(weight)-1
weight.sort()
while i<=j:
count+=1
if weight[i] + weight[j] <= 3:
i+=1
j -= 1
return count
-------------------------------------------------------------
Character Reprogramming
C++14
int getMaxDeletions(string s) {
int x=0,y=0,count=0;
for(char ch :s){
if(ch=='R')
{
x++;
count++;
}
else if(ch=='L'){
x--;
count ++;
}
else if(ch=='U'){
y++;
count++;
}
else if(ch=='D'){
y--;
count++;
}
}
return count-abs(x)-abs(y);
}
}
return count-abs(x)-abs(y);
}
-------------------------------------------------------------
Conference Schedule
Python 3
-------------------------------------------------------------
# Python 2
-------------------------------------------------------------
Balancing Paranthesis
C++
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string p;
cin>>p;
return 0;
}
-------------------------------------------------------------
Dam Design
Python 3
return int(mud_max)
-------------------------------------------------------------
Duplicated Products
public static int numDuplicates(List<String> name, List<Integer> price, List<Integer> weight) {
Set<String> uniqueProducts = new HashSet<String>();
for (int i = 0; i < name.size(); i++)
uniqueProducts.add(name.get(i) + " " + price.get(i) + " " + weight.get(i));
return name.size() - uniqueProducts.size();
}
-------------------------------------------------------------
4th Bit
Python 3
def fourthBit(num):
return string1[-4]
-------------------------------------------------------------
Balanced Array
Python 3
def balancedSum(arr):
n=len(arr)
prefixSum = [0] * n
prefixSum[0] = arr[0]
for i in range(1, n) :
prefixSum[i] = prefixSum[i - 1] + arr[i]
suffixSum = [0] * n
suffixSum[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1) :
suffixSum[i] = suffixSum[i + 1] + arr[i]
for i in range(1, n - 1, 1) :
if prefixSum[i] == suffixSum[i] :
return i
return -1
____________________________________________________________________________
Triangle or Not -
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
String[] res;
int a_size = 0;
a_size = Integer.parseInt(in.nextLine().trim());
int b_size = 0;
b_size = Integer.parseInt(in.nextLine().trim());
int c_size = 0;
c_size = Integer.parseInt(in.nextLine().trim());
bw.close();
}
}
____________________________________________________________________________
Duplicate products
int numDuplicates(vector<string> name, vector<int> price, vector<int> weight) {
int count=0;
string product="";
unordered_map<string,int>freq;
for(int i=0;i<name.size();i++)
{
product=name[i]+" "+to_string(price[i])+" "+to_string(weight[i]);
if(freq[product])
{
count++;
}
else
{
freq[product]++;
}
}
return count;
____________________________________________________________________________
Circular Printer
sum=0
sum+=(min(abs(ord('A')-ord(s[0])),26-abs(ord('A')-ord(s[0]))))
for i in range(len(s)-1):
sum+=(min(abs(ord(s[i])-ord(s[i+1])),26-abs(ord(s[i])-ord(s[i+1]))))
return sum
Bit Logic
flag = 0
while(lo<hi):
for i in range(lo+1,hi+1):
temp = lo^i
if(temp>flag and temp<=k):
flag = temp
lo+=1
return flag
____________________________________________________________________________
Largest String
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'getLargestString' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
# 1. STRING s
# 2. INTEGER k
#
return ''.join(ans)
# Write your code here
if _name_ == '_main_':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
k = int(input().strip())
result = getLargestString(s, k)
fptr.write(result + '\n')
fptr.close()
____________________________________________________________________________
Character Reprogramming
C
int getMaxDeletions(char* s)
{
int i = 0, size = 0;
while(s[i] != '\0')
{
size++;
i++;
}
i = 0;
int *array;
array = calloc(size, sizeof(int));
while(s[i] != '\0')
{
if(s[i] == 'U')
{
array[i] = 1;
}
i++;
}
else if(array[i] == 2)
{
Rcounter++;
}
int answer = 0;
if(Ucounter>= Dcounter)
{
answer = answer + Dcounter;
}
else
{
answer = answer + Ucounter;
}
else
{
answer = answer+Rcounter;
}
return 2*answer;
}
____________________________________________________________________________
Reverse Queries
for i in operations:
x = i[0]
y = i[1]
temp = arr[x:y+1]
temp = temp[::-1]
for i in range(x,y+1):
arr[i] = temp[i-x]
return arr
____________________________________________________________________________
____________________________________________________________________________
Fixbuzz
def fizzbuzz(n):
if n % 3 == 0 and n % 5 == 0:
print('FizzBuzz')
elif n % 3 == 0:
print('Fizz')
elif n % 5 == 0:
print('Buzz')
else:
print(n)
x=(int)(input())
for i in range(1,x+1):
fizzbuzz(i)
____________________________________________________________________________
n=int(input())
p=int(input())
pthFactor(n,p)
____________________________________________________________________________
Condensed List
SinglyLinkedListNode* condense(SinglyLinkedListNode* head) {
struct SinglyLinkedListNode *p;
unordered_set<int> s;
p=head;
s.insert(head->data);
while(p!=NULL && p->next!=NULL)
{
if(s.find(p->next->data)==s.end())
{
s.insert(p->next->data);
p=p->next;
}
else {
p->next=p->next->next;
}
}
for (auto it = s.begin(); it !=s.end(); ++it)
cout << ' ' << *it;
return head;
}
____________________________________________________________________________
No paired allowed
def minimalOperations(word_collection):
counter = []
for words in word_collection:
words = list(words)
count = 0
i=0
while i < len(words)-1:
if words[i] == words[i+1]:
count += 1
i += 1
i += 1
counter.append(count)
return counter
____________________________________________________________________________
Product Sort
def itemsSort(items):
l=items.copy()
r=[]
s=[]
l=set(l)
for i in l:
c=items.count(i)
s.append([c,i])
s.sort(key=lambda x:x[0])
for i in s:
q=i[0]
while q!=0:
r.append(i[1])
q-=1
return r
____________________________________________________________________________
import re
# satisfy constraints
_sentence = str(sentence)
assert len(_sentence) >= 1 and len(_sentence) <= 10**5
assert re.match(sentence_rgx, _sentence)
return arranged
___________________________________________________________________________
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'minDiff' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
def minDiff(arr):
# Write your code here
sum_=0
arr.sort()
for i in range(len(arr)-1):
sum_+=arr[i+1]-arr[i]
return sum_
if _name_ == '_main_':
___________________________________________________________________________
Maximum index
int maxIndex(int steps, int badIndex) {
int i=0;
int j=1;
int tempStep = steps;
int scene1, scene2;
while (steps--) {
if (i+j != badIndex)
i = i+j;
j++;
}
scene1 = i;
i = 0;
tempStep = tempStep - 1;
j = 2;
while (tempStep--) {
if (i+j != badIndex)
i = i+j;
j++;
}
scene2 = i;
return scene1 > scene2 ? scene1 : scene2;
}
___________________________________________________________________________
Product Defects
def largestArea(samples):
# `T[i][j]` stores the size of maximum square submatrix ending at `M[i][j]`
T = [[0 for x in range(len(samples[0]))] for y in range(len(samples))]
_______________________________________________________________________