- Overview of CoCubes Coding Module
- Insights from Previous Year CoCubes Coding Questions
- Commonly Repeated CoCubes Coding Questions
- Breakdown of Coding Topics in CoCubes
- Top 5 Coding Questions for CoCubes
- Conclusion
- Frequently Asked Questions (FAQs)
CoCubes Coding Questions: Top 5 Coding Problems and Solutions 2025
With the competition in today’s job market, freshers looking to join top companies need to stand out, and one way to do so is through acing standardised assessments like the CoCubes test.
CoCubes is widely used by organisations to evaluate candidates’ technical and analytical skills, and for many freshers, the coding section is the highlight. Understanding the types of coding questions asked in CoCubes, analysing patterns from previous years, and practising frequently repeated questions are crucial to success.
Overview of CoCubes Coding Module
The CoCubes coding module assesses key programming skills across a range of topics and difficulty levels. Most coding questions are designed to be language-agnostic, focusing instead on problem-solving logic and efficiency.
|
Difficulty Level |
Question Types |
Common Topics |
|---|---|---|
|
Beginner |
Basic programming questions |
Loops, Arrays, Simple I/O |
|
Intermediate |
Data structures and algorithms |
Sorting, Stacks, Strings |
|
Advanced |
Algorithmic problems |
Recursion, Optimization |
Quick Tip: Although CoCubes allows programming in languages like C, C++, Java, and Python, your choice of language should be the one you’re most comfortable with, as time efficiency is key.
Insights from Previous Year CoCubes Coding Questions
Analysing past questions offers insight into the structure and focus of CoCubes coding tests. Questions tend to cover fundamental concepts and practical applications. Here are some examples from previous years:
|
Year |
Key Topics |
Example Question |
|
2021 |
Arrays & Loops |
Reverse an array in place |
|
2022 |
String Manipulation |
Check if a string is a palindrome |
|
2023 |
Recursion & Backtracking |
Solve the N-Queens problem |
Tip for Freshers: Review a variety of questions from each year to identify recurring concepts, as many companies prefer these tried-and-tested questions to evaluate core skills.
Commonly Repeated CoCubes Coding Questions
Certain question types tend to reappear over the years, providing an excellent focus for preparation. Below are some popular examples:
|
Topic |
Sample Question |
|
Sorting Algorithms |
Implement QuickSort for an unsorted array |
|
String Manipulation |
Count the frequency of each character in a string |
|
Number Theory |
Check if a given number is prime |
Pro Tip: Prioritize these repeated questions in your practice sessions, as familiarity with them will increase your accuracy and confidence.
Breakdown of Coding Topics in CoCubes
Here’s a structured look at the key areas covered in the CoCubes coding test to help freshers focus their preparation:
- Basic Programming Concepts – Covers fundamental operations such as basic I/O, loops, and data type manipulations.
- Data Structures – Questions on arrays, linked lists, stacks, queues, and binary trees are common.
- Algorithms – Includes sorting (e.g., Bubble, QuickSort) and searching (e.g., Binary Search) algorithms.
- Mathematics – Includes problems like prime number checking, factorial calculations, and Fibonacci series generation.
- String Manipulation – Encompasses palindrome checking, anagram verification, and substring extraction.
Top 5 Coding Questions for CoCubes
Problem Statement 1
One day, Mary wanted to give a present to her friend. She decided on a beautiful bouquet of flowers and began collecting them. She needed precisely 2 types of flowers, and the total number of flowers required was 't'. To gather these, she started picking from her garden, which contained 'N' types of flowers.
Each type was arranged in a queue in non-decreasing order, such as 1, 3, 6, 15, and so forth. Now, she seeks your help in determining the indexes of the flowers she should collect.
Note: For every case, there will always be a pair of flowers whose sum equals 't'. If multiple pairs exist, select the first occurrence.
Input Format
The first line contains integers N and t, where N is the total types of flowers and t is the total number of flowers needed. The second line contains n integers a1,a2,…, an- elements of the a array.
Output Format
Print the indexes of the two flowers that sum up to 't'. The first index should be smaller than the second index. Both indexes should be zero-based.
Constraints
2 <= N <= 10^4
1 <= a[i] <= 10^3
2 <= t <= 2*10^3
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t;
cin >> n >> t; // initializing the variables
vector arr(n); // initialize the array
int a = -1, b = -1;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int i = 0, j = n - 1; // initialize the two pointers i and j
while (i < j) {
if (arr[i] + arr[j] == t) { // when the condition is met break the while loop
a = i;
b = j;
break;
}
else if (arr[i] + arr[j] > t) {
j--; // if the sum is greater than the target
}
else {
i++; // if the sum is less than target
}
}
cout << a << " " << b; // print the two indexes
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+IAp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7IAogICAgaW50IG4sIHQ7IAogICAgY2luID4+IG4gPj4gdDsgICAgLy8gaW5pdGlhbGl6aW5nIHRoZSB2YXJpYWJsZXMgCiAgICB2ZWN0b3I8aW50PiBhcnIobik7ICAgLy8gaW5pdGlhbGl6ZSB0aGUgYXJyYXkKICAgIGludCBhID0gLTEsIGIgPSAtMTsgCgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsgCiAgICAgICAgY2luID4+IGFycltpXTsgCiAgICB9IAogICAgCiAgICBpbnQgaSA9IDAsIGogPSBuIC0gMTsgIC8vIGluaXRpYWxpemUgdGhlIHR3byBwb2ludGVycyBpIGFuZCBqIAoKICAgIHdoaWxlIChpIDwgaikgeyAKICAgICAgICBpZiAoYXJyW2ldICsgYXJyW2pdID09IHQpIHsgIC8vIHdoZW4gdGhlIGNvbmRpdGlvbiBpcyBtZXQgYnJlYWsgdGhlIHdoaWxlIGxvb3AgCiAgICAgICAgICAgIGEgPSBpOyAKICAgICAgICAgICAgYiA9IGo7IAogICAgICAgICAgICBicmVhazsgIAogICAgICAgIH0gCiAgICAgICAgZWxzZSBpZiAoYXJyW2ldICsgYXJyW2pdID4gdCkgewogICAgICAgICAgICBqLS07IC8vIGlmIHRoZSBzdW0gaXMgZ3JlYXRlciB0aGFuIHRoZSB0YXJnZXQKICAgICAgICB9IAogICAgICAgIGVsc2UgewogICAgICAgICAgICBpKys7ICAvLyBpZiB0aGUgc3VtIGlzIGxlc3MgdGhhbiB0YXJnZXQgCiAgICAgICAgfQogICAgfQoKICAgIGNvdXQgPDwgYSA8PCAiICIgPDwgYjsgLy8gcHJpbnQgdGhlIHR3byBpbmRleGVzIAogICAgcmV0dXJuIDA7IAp9Cg==
Solution Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int t = scanner.nextInt(); // initializing the variables
int[] arr = new int[n]; // initialize the array
int a = -1, b = -1; // index variables
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt(); // filling the array
}
int i = 0, j = n - 1; // initialize the two pointers i and j
while (i < j) {
if (arr[i] + arr[j] == t) { // when the condition is met
a = i;
b = j;
break;
} else if (arr[i] + arr[j] > t) {
j--; // if the sum is greater than the target
} else {
i++; // if the sum is less than the target
}
}
System.out.println(a + " " + b); // print the two indexes
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgCiAgICAgICAgaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBpbnQgdCA9IHNjYW5uZXIubmV4dEludCgpOyAgLy8gaW5pdGlhbGl6aW5nIHRoZSB2YXJpYWJsZXMgCiAgICAgICAgaW50W10gYXJyID0gbmV3IGludFtuXTsgICAgIC8vIGluaXRpYWxpemUgdGhlIGFycmF5CiAgICAgICAgaW50IGEgPSAtMSwgYiA9IC0xOyAgICAgICAgIC8vIGluZGV4IHZhcmlhYmxlcyAKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgYXJyW2ldID0gc2Nhbm5lci5uZXh0SW50KCk7ICAvLyBmaWxsaW5nIHRoZSBhcnJheSAKICAgICAgICB9CgogICAgICAgIGludCBpID0gMCwgaiA9IG4gLSAxOyAgLy8gaW5pdGlhbGl6ZSB0aGUgdHdvIHBvaW50ZXJzIGkgYW5kIGogCgogICAgICAgIHdoaWxlIChpIDwgaikgeyAKICAgICAgICAgICAgaWYgKGFycltpXSArIGFycltqXSA9PSB0KSB7ICAvLyB3aGVuIHRoZSBjb25kaXRpb24gaXMgbWV0CiAgICAgICAgICAgICAgICBhID0gaTsgCiAgICAgICAgICAgICAgICBiID0gajsgCiAgICAgICAgICAgICAgICBicmVhazsgIAogICAgICAgICAgICB9IGVsc2UgaWYgKGFycltpXSArIGFycltqXSA+IHQpIHsKICAgICAgICAgICAgICAgIGotLTsgIC8vIGlmIHRoZSBzdW0gaXMgZ3JlYXRlciB0aGFuIHRoZSB0YXJnZXQKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGkrKzsgIC8vIGlmIHRoZSBzdW0gaXMgbGVzcyB0aGFuIHRoZSB0YXJnZXQgCiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihhICsgIiAiICsgYik7IC8vIHByaW50IHRoZSB0d28gaW5kZXhlcyAKICAgICAgICBzY2FubmVyLmNsb3NlKCk7CiAgICB9Cn0K
Soultion Python
n, t = map(int, input().split()) # initializing the variables
arr = list(map(int, input().split())) # initialize the array
a, b = -1, -1 # index variables
i, j = 0, n - 1 # initialize the two pointers i and j
while i < j:
if arr[i] + arr[j] == t: # when the condition is met
a = i
b = j
break
elif arr[i] + arr[j] > t:
j -= 1 # if the sum is greater than the target
else:
i += 1 # if the sum is less than the target
print(a, b) # print the two indexes
biwgdCA9IG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkgICMgaW5pdGlhbGl6aW5nIHRoZSB2YXJpYWJsZXMgDQphcnIgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpICAjIGluaXRpYWxpemUgdGhlIGFycmF5DQphLCBiID0gLTEsIC0xICAjIGluZGV4IHZhcmlhYmxlcyANCg0KaSwgaiA9IDAsIG4gLSAxICAjIGluaXRpYWxpemUgdGhlIHR3byBwb2ludGVycyBpIGFuZCBqIA0KDQp3aGlsZSBpIDwgajogDQogICAgaWYgYXJyW2ldICsgYXJyW2pdID09IHQ6ICAjIHdoZW4gdGhlIGNvbmRpdGlvbiBpcyBtZXQNCiAgICAgICAgYSA9IGkgDQogICAgICAgIGIgPSBqIA0KICAgICAgICBicmVhayAgDQogICAgZWxpZiBhcnJbaV0gKyBhcnJbal0gPiB0Og0KICAgICAgICBqIC09IDEgICMgaWYgdGhlIHN1bSBpcyBncmVhdGVyIHRoYW4gdGhlIHRhcmdldA0KICAgIGVsc2U6DQogICAgICAgIGkgKz0gMSAgIyBpZiB0aGUgc3VtIGlzIGxlc3MgdGhhbiB0aGUgdGFyZ2V0IA0KDQpwcmludChhLCBiKSAgIyBwcmludCB0aGUgdHdvIGluZGV4ZXMNCg==
Problem Statement 2
John was learning about arrays when his friend Ram arrived. Seeing John, Ram thought of a challenge for him. He challenged John to calculate the "measurement" of an array a of size n.
Given a 0-indexed array, Ram defined the measurement of the array as the sum of the "sanities" of all the elements in the array. To calculate the sanity of each element in the array, the following operations need to be performed: Sort the given array.
Calculate the sanity of an element in the array, which is defined as the sum of its original index (before sorting) and the index of its last occurrence in the new array (after sorting).
Your task is to help John calculate the measurement of the array. Since the output may be too large, print it modulo 10^9+7.
Note that duplicate elements may exist in the array.
Input Format
The first line contains ‘n’ denoting the number of elements in the array a.
The next line contains n elements denoting the elements of the array a.
Output Format
Print the measurement of the given array
Constraints
1<=n<=10^6
1<=ai<=10^6
Solution C++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
vector<pair<int, int>> arr;
// Reading the array and storing original indices
for (int i = 0; i < n; i++) {
int value;
cin >> value;
arr.push_back({value, i});
}
// Sort the array while maintaining original indices
sort(arr.begin(), arr.end());
// Calculate the measurement
int measurement = 0;
for (int i = 0; i < n; i++) {
int value = arr[i].first;
int originalIndex = arr[i].second;
// Find the last occurrence by iterating backward
int lastIndex = i;
while (lastIndex + 1 < n && arr[lastIndex + 1].first == value) {
lastIndex++;
}
// Calculate sanity and add to measurement
int sanity = (originalIndex + lastIndex) % MOD;
measurement = (measurement + sanity) % MOD;
}
cout << measurement << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTU9EID0gMWU5ICsgNzsKCmludCBtYWluKCkgewogICAgaW50IG47CiAgICBjaW4gPj4gbjsKICAgIHZlY3RvcjxwYWlyPGludCwgaW50Pj4gYXJyOwogICAgCiAgICAvLyBSZWFkaW5nIHRoZSBhcnJheSBhbmQgc3RvcmluZyBvcmlnaW5hbCBpbmRpY2VzCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGludCB2YWx1ZTsKICAgICAgICBjaW4gPj4gdmFsdWU7CiAgICAgICAgYXJyLnB1c2hfYmFjayh7dmFsdWUsIGl9KTsKICAgIH0KICAgIAogICAgLy8gU29ydCB0aGUgYXJyYXkgd2hpbGUgbWFpbnRhaW5pbmcgb3JpZ2luYWwgaW5kaWNlcwogICAgc29ydChhcnIuYmVnaW4oKSwgYXJyLmVuZCgpKTsKICAgIAogICAgLy8gQ2FsY3VsYXRlIHRoZSBtZWFzdXJlbWVudAogICAgaW50IG1lYXN1cmVtZW50ID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaW50IHZhbHVlID0gYXJyW2ldLmZpcnN0OwogICAgICAgIGludCBvcmlnaW5hbEluZGV4ID0gYXJyW2ldLnNlY29uZDsKICAgICAgICAKICAgICAgICAvLyBGaW5kIHRoZSBsYXN0IG9jY3VycmVuY2UgYnkgaXRlcmF0aW5nIGJhY2t3YXJkCiAgICAgICAgaW50IGxhc3RJbmRleCA9IGk7CiAgICAgICAgd2hpbGUgKGxhc3RJbmRleCArIDEgPCBuICYmIGFycltsYXN0SW5kZXggKyAxXS5maXJzdCA9PSB2YWx1ZSkgewogICAgICAgICAgICBsYXN0SW5kZXgrKzsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLy8gQ2FsY3VsYXRlIHNhbml0eSBhbmQgYWRkIHRvIG1lYXN1cmVtZW50CiAgICAgICAgaW50IHNhbml0eSA9IChvcmlnaW5hbEluZGV4ICsgbGFzdEluZGV4KSAlIE1PRDsKICAgICAgICBtZWFzdXJlbWVudCA9IChtZWFzdXJlbWVudCArIHNhbml0eSkgJSBNT0Q7CiAgICB9CiAgICAKICAgIGNvdXQgPDwgbWVhc3VyZW1lbnQgPDwgZW5kbDsKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.util.*;
public class Main {
// Define MOD as a constant
private static final int MOD = 1000000007;
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
List arr = new ArrayList<>();
// Reading the array and storing original indices
for (int i = 0; i < n; i++) {
int value = scn.nextInt();
arr.add(new Pair(value, i));
}
// Sort the array while maintaining original indices
arr.sort((a, b) -> Integer.compare(a.value, b.value));
// Calculate the measurement
int measurement = 0;
for (int i = 0; i < n; i++) {
int value = arr.get(i).value;
int originalIndex = arr.get(i).index;
// Find the last occurrence by iterating forward
int lastIndex = i;
while (lastIndex + 1 < n && arr.get(lastIndex + 1).value == value) {
lastIndex++;
}
// Calculate sanity and add to measurement
int sanity = (originalIndex + lastIndex) % MOD;
measurement = (measurement + sanity) % MOD;
}
System.out.println(measurement);
}
// Define a Pair class to store value and index
static class Pair {
int value, index;
Pair(int value, int index) {
this.value = value;
this.index = index;
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIC8vIERlZmluZSBNT0QgYXMgYSBjb25zdGFudAogICAgcHJpdmF0ZSBzdGF0aWMgZmluYWwgaW50IE1PRCA9IDEwMDAwMDAwMDc7CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2NuID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKCiAgICAgICAgaW50IG4gPSBzY24ubmV4dEludCgpOwogICAgICAgIExpc3Q8UGFpcj4gYXJyID0gbmV3IEFycmF5TGlzdDw+KCk7CgogICAgICAgIC8vIFJlYWRpbmcgdGhlIGFycmF5IGFuZCBzdG9yaW5nIG9yaWdpbmFsIGluZGljZXMKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBpbnQgdmFsdWUgPSBzY24ubmV4dEludCgpOwogICAgICAgICAgICBhcnIuYWRkKG5ldyBQYWlyKHZhbHVlLCBpKSk7CiAgICAgICAgfQoKICAgICAgICAvLyBTb3J0IHRoZSBhcnJheSB3aGlsZSBtYWludGFpbmluZyBvcmlnaW5hbCBpbmRpY2VzCiAgICAgICAgYXJyLnNvcnQoKGEsIGIpIC0+IEludGVnZXIuY29tcGFyZShhLnZhbHVlLCBiLnZhbHVlKSk7CgogICAgICAgIC8vIENhbGN1bGF0ZSB0aGUgbWVhc3VyZW1lbnQKICAgICAgICBpbnQgbWVhc3VyZW1lbnQgPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGludCB2YWx1ZSA9IGFyci5nZXQoaSkudmFsdWU7CiAgICAgICAgICAgIGludCBvcmlnaW5hbEluZGV4ID0gYXJyLmdldChpKS5pbmRleDsKCiAgICAgICAgICAgIC8vIEZpbmQgdGhlIGxhc3Qgb2NjdXJyZW5jZSBieSBpdGVyYXRpbmcgZm9yd2FyZAogICAgICAgICAgICBpbnQgbGFzdEluZGV4ID0gaTsKICAgICAgICAgICAgd2hpbGUgKGxhc3RJbmRleCArIDEgPCBuICYmIGFyci5nZXQobGFzdEluZGV4ICsgMSkudmFsdWUgPT0gdmFsdWUpIHsKICAgICAgICAgICAgICAgIGxhc3RJbmRleCsrOwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBDYWxjdWxhdGUgc2FuaXR5IGFuZCBhZGQgdG8gbWVhc3VyZW1lbnQKICAgICAgICAgICAgaW50IHNhbml0eSA9IChvcmlnaW5hbEluZGV4ICsgbGFzdEluZGV4KSAlIE1PRDsKICAgICAgICAgICAgbWVhc3VyZW1lbnQgPSAobWVhc3VyZW1lbnQgKyBzYW5pdHkpICUgTU9EOwogICAgICAgIH0KCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG1lYXN1cmVtZW50KTsKICAgIH0KCiAgICAvLyBEZWZpbmUgYSBQYWlyIGNsYXNzIHRvIHN0b3JlIHZhbHVlIGFuZCBpbmRleAogICAgc3RhdGljIGNsYXNzIFBhaXIgewogICAgICAgIGludCB2YWx1ZSwgaW5kZXg7CgogICAgICAgIFBhaXIoaW50IHZhbHVlLCBpbnQgaW5kZXgpIHsKICAgICAgICAgICAgdGhpcy52YWx1ZSA9IHZhbHVlOwogICAgICAgICAgICB0aGlzLmluZGV4ID0gaW5kZXg7CiAgICAgICAgfQogICAgfQp9Cg==
Solution Python
MOD = 10**9+7
def calculate_measurement(n,an):
original_index=[(val,idx) for idx,val in enumerate(an)]
original_index.sort()
last_occurence={}
for idx,(val,_) in enumerate(original_index):
last_occurence[val]=idx
measure=0
for idx,val in enumerate(an):
original_index=idx
last_sorted_idx=last_occurence[val]
sanity=(original_index+last_sorted_idx)%MOD
measure=(measure+sanity)%MOD
print(measure)
n=int(input())
an=list(map(int,input().split()))
calculate_measurement(n,an)
TU9EID0gMTAqKjkrNw0KZGVmIGNhbGN1bGF0ZV9tZWFzdXJlbWVudChuLGFuKToNCiAgb3JpZ2luYWxfaW5kZXg9Wyh2YWwsaWR4KSBmb3IgaWR4LHZhbCBpbiBlbnVtZXJhdGUoYW4pXQ0KICBvcmlnaW5hbF9pbmRleC5zb3J0KCkNCg0KICBsYXN0X29jY3VyZW5jZT17fQ0KICBmb3IgaWR4LCh2YWwsXykgaW4gZW51bWVyYXRlKG9yaWdpbmFsX2luZGV4KToNCiAgICBsYXN0X29jY3VyZW5jZVt2YWxdPWlkeA0KDQogIG1lYXN1cmU9MA0KICBmb3IgaWR4LHZhbCBpbiBlbnVtZXJhdGUoYW4pOg0KICAgIG9yaWdpbmFsX2luZGV4PWlkeA0KICAgIGxhc3Rfc29ydGVkX2lkeD1sYXN0X29jY3VyZW5jZVt2YWxdDQogICAgc2FuaXR5PShvcmlnaW5hbF9pbmRleCtsYXN0X3NvcnRlZF9pZHgpJU1PRA0KICAgIG1lYXN1cmU9KG1lYXN1cmUrc2FuaXR5KSVNT0QNCg0KICBwcmludChtZWFzdXJlKQ0KDQpuPWludChpbnB1dCgpKQ0KYW49bGlzdChtYXAoaW50LGlucHV0KCkuc3BsaXQoKSkpDQpjYWxjdWxhdGVfbWVhc3VyZW1lbnQobixhbikNCg==
Problem Statement 3
Ravi discovered that some strings read the same forwards and backwards, which are called palindromes. He noticed that every string he encountered had at least one palindromic substring. He wants to know how to find the longest palindromic substring in a given string, S.
Can you help him determine the length of this longest palindromic substring?
Input Format
The first line of input contains an integer N representing the length of the string.
The second line of input a string S representing the given string whose longest substring we have to find.
Output Format
Display an integer representing the length of the longest palindromic substring possible.
Constraints
1<= N <=10^7
Solution C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
cin>>s;
int len = 1;
for(int i=0; i<n; i++)
{
int ptr1 = i, ptr2 = i;
while(ptr1 >=0 && ptr2 < n && s[ptr1] == s[ptr2]) //checking for odd length palindromes
{
len = max(len, ptr2 - ptr1 + 1);
ptr1--;
ptr2++;
}
ptr1 = i, ptr2 = i+1;
while(ptr1 >=0 && ptr2
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkKewogIGludCBuOwogIGNpbj4+bjsKICBzdHJpbmcgczsKICBjaW4+PnM7CiAgaW50IGxlbiA9IDE7CiAgZm9yKGludCBpPTA7IGk8bjsgaSsrKQogIHsKICAgIGludCBwdHIxID0gaSwgcHRyMiA9IGk7CiAgICB3aGlsZShwdHIxID49MCAmJiBwdHIyIDwgbiAmJiBzW3B0cjFdID09IHNbcHRyMl0pIC8vY2hlY2tpbmcgZm9yIG9kZCBsZW5ndGggcGFsaW5kcm9tZXMKICAgIHsKICAgICAgbGVuID0gbWF4KGxlbiwgcHRyMiAtIHB0cjEgKyAxKTsKICAgICAgcHRyMS0tOwogICAgICBwdHIyKys7CiAgICB9CgogICAgcHRyMSA9IGksIHB0cjIgPSBpKzE7CiAgICB3aGlsZShwdHIxID49MCAmJiBwdHIyIDxuICYmIHNbcHRyMV0gPT0gc1twdHIyXSkgLy9jaGVja2luZyBmb3IgZXZlbiBsZW5ndGggcGFsaW5kcm9tZXMKICAgIHsKICAgICAgbGVuID0gbWF4KGxlbiwgcHRyMiAtIHB0cjEgKyAxKTsKICAgICAgcHRyMS0tOwogICAgICBwdHIyKys7CiAgICB9CiAgfQogIGNvdXQ8PGxlbjw8ZW5kbDsgLy9wcmludGluZyB0aGUgbGVuZ3RoCn0K
Solution Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Read the length of the string
String s = sc.next(); // Read the string
int length = 1;
for (int i = 0; i < n; i++) {
// Checking for odd length palindromes
int ptr1 = i, ptr2 = i;
while (ptr1 >= 0 && ptr2 < n && s.charAt(ptr1) == s.charAt(ptr2)) {
length = Math.max(length, ptr2 - ptr1 + 1);
ptr1--;
ptr2++;
}
// Checking for even length palindromes
ptr1 = i;
ptr2 = i + 1;
while (ptr1 >= 0 && ptr2 < n && s.charAt(ptr1) == s.charAt(ptr2)) {
length = Math.max(length, ptr2 - ptr1 + 1);
ptr1--;
ptr2++;
}
}
System.out.println(length); // Print the maximum length of the palindrome
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBpbnQgbiA9IHNjLm5leHRJbnQoKTsgIC8vIFJlYWQgdGhlIGxlbmd0aCBvZiB0aGUgc3RyaW5nCiAgICAgICAgU3RyaW5nIHMgPSBzYy5uZXh0KCk7ICAvLyBSZWFkIHRoZSBzdHJpbmcKICAgICAgICBpbnQgbGVuZ3RoID0gMTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgLy8gQ2hlY2tpbmcgZm9yIG9kZCBsZW5ndGggcGFsaW5kcm9tZXMKICAgICAgICAgICAgaW50IHB0cjEgPSBpLCBwdHIyID0gaTsKICAgICAgICAgICAgd2hpbGUgKHB0cjEgPj0gMCAmJiBwdHIyIDwgbiAmJiBzLmNoYXJBdChwdHIxKSA9PSBzLmNoYXJBdChwdHIyKSkgewogICAgICAgICAgICAgICAgbGVuZ3RoID0gTWF0aC5tYXgobGVuZ3RoLCBwdHIyIC0gcHRyMSArIDEpOwogICAgICAgICAgICAgICAgcHRyMS0tOwogICAgICAgICAgICAgICAgcHRyMisrOwogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyBDaGVja2luZyBmb3IgZXZlbiBsZW5ndGggcGFsaW5kcm9tZXMKICAgICAgICAgICAgcHRyMSA9IGk7CiAgICAgICAgICAgIHB0cjIgPSBpICsgMTsKICAgICAgICAgICAgd2hpbGUgKHB0cjEgPj0gMCAmJiBwdHIyIDwgbiAmJiBzLmNoYXJBdChwdHIxKSA9PSBzLmNoYXJBdChwdHIyKSkgewogICAgICAgICAgICAgICAgbGVuZ3RoID0gTWF0aC5tYXgobGVuZ3RoLCBwdHIyIC0gcHRyMSArIDEpOwogICAgICAgICAgICAgICAgcHRyMS0tOwogICAgICAgICAgICAgICAgcHRyMisrOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4obGVuZ3RoKTsgIC8vIFByaW50IHRoZSBtYXhpbXVtIGxlbmd0aCBvZiB0aGUgcGFsaW5kcm9tZQogICAgfQp9Cg==
Solution Python
def main():
n = int(input()) # Read the length of the string
s = input() # Read the string
length = 1
for i in range(n):
# Checking for odd length palindromes
ptr1, ptr2 = i, i
while ptr1 >= 0 and ptr2 < n and s[ptr1] == s[ptr2]:
length = max(length, ptr2 - ptr1 + 1)
ptr1 -= 1
ptr2 += 1
# Checking for even length palindromes
ptr1, ptr2 = i, i + 1
while ptr1 >= 0 and ptr2 < n and s[ptr1] == s[ptr2]:
length = max(length, ptr2 - ptr1 + 1)
ptr1 -= 1
ptr2 += 1
print(length) # Print the maximum length of the palindrome
if __name__ == "__main__":
main()
ZGVmIG1haW4oKToNCiAgICBuID0gaW50KGlucHV0KCkpICAjIFJlYWQgdGhlIGxlbmd0aCBvZiB0aGUgc3RyaW5nDQogICAgcyA9IGlucHV0KCkgICAgICAgIyBSZWFkIHRoZSBzdHJpbmcNCiAgICBsZW5ndGggPSAxDQogICAgDQogICAgZm9yIGkgaW4gcmFuZ2Uobik6DQogICAgICAgICMgQ2hlY2tpbmcgZm9yIG9kZCBsZW5ndGggcGFsaW5kcm9tZXMNCiAgICAgICAgcHRyMSwgcHRyMiA9IGksIGkNCiAgICAgICAgd2hpbGUgcHRyMSA+PSAwIGFuZCBwdHIyIDwgbiBhbmQgc1twdHIxXSA9PSBzW3B0cjJdOg0KICAgICAgICAgICAgbGVuZ3RoID0gbWF4KGxlbmd0aCwgcHRyMiAtIHB0cjEgKyAxKQ0KICAgICAgICAgICAgcHRyMSAtPSAxDQogICAgICAgICAgICBwdHIyICs9IDENCg0KICAgICAgICAjIENoZWNraW5nIGZvciBldmVuIGxlbmd0aCBwYWxpbmRyb21lcw0KICAgICAgICBwdHIxLCBwdHIyID0gaSwgaSArIDENCiAgICAgICAgd2hpbGUgcHRyMSA+PSAwIGFuZCBwdHIyIDwgbiBhbmQgc1twdHIxXSA9PSBzW3B0cjJdOg0KICAgICAgICAgICAgbGVuZ3RoID0gbWF4KGxlbmd0aCwgcHRyMiAtIHB0cjEgKyAxKQ0KICAgICAgICAgICAgcHRyMSAtPSAxDQogICAgICAgICAgICBwdHIyICs9IDENCg0KICAgIHByaW50KGxlbmd0aCkgICMgUHJpbnQgdGhlIG1heGltdW0gbGVuZ3RoIG9mIHRoZSBwYWxpbmRyb21lDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgbWFpbigpDQo=
Problem Statement 4
In a shoe factory, left and right shoes are produced separately, each with an integral ID according to its design and size. After manufacturing, the shoes are sent to Bob, who pairs the left and right shoes with matching IDs. Each shoe can only be paired once, and the manager wants to know the IDs of all matching pairs in sorted order before final packaging.
Input Format
The first line of the input contains two integers, n and m – the number of left and right shoes, respectively.
The second line contains n space-separated integers a1, a2, ..., an – the IDs of all the left shoes
The third line contains m space-separated integers b1, b2, ..., bm – the IDs of all the right shoes.
Output Format
In the first line, print p – the number of pairs that will be ready.
In the next line, print p space-separated integers – the IDs of all the pairs that will be ready in sorted order.
Constraints
1 <= n, m <= 10^4
0 <= ai, bi <= 10^4
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main(){
// read inputs
int n, m;
cin >> n >> m;
int left[n], right[m];
for(int i = 0; i < n; i++)
cin >> left[i];
for(int i = 0; i < m; i++)
cin >> right[i];
// declare map to store frequency of
// each right shoe
map<int, int> freq;
for(int i = 0; i < m; i++)
freq[right[i]]++;
// declare the result vector
vector res;
// iterate through the left shoe array
for(int i = 0; i < n; i++){
// if there is a matching right shoe,
// insert it in the result vector
// and dedcrease the frequency of
// right shoe of the same ID
if(freq[left[i]] > 0){
res.push_back(left[i]);
freq[left[i]]--;
}
}
// sorting the result
sort(res.begin(), res.end());
// print the result
cout << (int)res.size() << endl;
for(int i: res) cout << i << ' ';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpewogICAgLy8gcmVhZCBpbnB1dHMKICAgIGludCBuLCBtOwogICAgY2luID4+IG4gPj4gbTsKICAgIGludCBsZWZ0W25dLCByaWdodFttXTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgY2luID4+IGxlZnRbaV07IAogICAgZm9yKGludCBpID0gMDsgaSA8IG07IGkrKykKICAgICAgICBjaW4gPj4gcmlnaHRbaV07CgogICAgLy8gZGVjbGFyZSBtYXAgdG8gc3RvcmUgZnJlcXVlbmN5IG9mCiAgICAvLyBlYWNoIHJpZ2h0IHNob2UKICAgIG1hcDxpbnQsIGludD4gZnJlcTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBtOyBpKyspCiAgICAgICAgZnJlcVtyaWdodFtpXV0rKzsKICAgIAogICAgLy8gZGVjbGFyZSB0aGUgcmVzdWx0IHZlY3RvcgogICAgdmVjdG9yPGludD4gcmVzOwogICAgLy8gaXRlcmF0ZSB0aHJvdWdoIHRoZSBsZWZ0IHNob2UgYXJyYXkKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspewogICAgICAgIC8vIGlmIHRoZXJlIGlzIGEgbWF0Y2hpbmcgcmlnaHQgc2hvZSwKICAgICAgICAvLyBpbnNlcnQgaXQgaW4gdGhlIHJlc3VsdCB2ZWN0b3IKICAgICAgICAvLyBhbmQgZGVkY3JlYXNlIHRoZSBmcmVxdWVuY3kgb2YKICAgICAgICAvLyByaWdodCBzaG9lIG9mIHRoZSBzYW1lIElECiAgICAgICAgaWYoZnJlcVtsZWZ0W2ldXSA+IDApewogICAgICAgICAgICByZXMucHVzaF9iYWNrKGxlZnRbaV0pOwogICAgICAgICAgICBmcmVxW2xlZnRbaV1dLS07CiAgICAgICAgfQogICAgfQogICAgLy8gc29ydGluZyB0aGUgcmVzdWx0CiAgICBzb3J0KHJlcy5iZWdpbigpLCByZXMuZW5kKCkpOwogICAgLy8gcHJpbnQgdGhlIHJlc3VsdAogICAgY291dCA8PCAoaW50KXJlcy5zaXplKCkgPDwgZW5kbDsKICAgIGZvcihpbnQgaTogcmVzKSBjb3V0IDw8IGkgPDwgJyAnOwogICAgcmV0dXJuIDA7Cn0K
Solution Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] left = new int[n];
int[] right = new int[m];
for (int i = 0; i < n; i++) {
left[i] = scanner.nextInt();
}
for (int i = 0; i < m; i++) {
right[i] = scanner.nextInt();
}
// Use a map to store frequency of each right shoe
Map<Integer, Integer> freq = new HashMap<>();
for (int i = 0; i < m; i++) {
freq.put(right[i], freq.getOrDefault(right[i], 0) + 1);
}
// List to store the result
List res = new ArrayList<>();
// Iterate through the left shoe array
for (int i = 0; i < n; i++) {
// If there is a matching right shoe, add to result and decrease frequency
if (freq.getOrDefault(left[i], 0) > 0) {
res.add(left[i]);
freq.put(left[i], freq.get(left[i]) - 1);
}
}
// Sort the result
Collections.sort(res);
// Print the result
System.out.println(res.size());
for (int shoe : res) {
System.out.print(shoe + " ");
}
scanner.close();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwoKICAgICAgICAvLyBSZWFkIGlucHV0cwogICAgICAgIGludCBuID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgaW50IG0gPSBzY2FubmVyLm5leHRJbnQoKTsKCiAgICAgICAgaW50W10gbGVmdCA9IG5ldyBpbnRbbl07CiAgICAgICAgaW50W10gcmlnaHQgPSBuZXcgaW50W21dOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBsZWZ0W2ldID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgaSsrKSB7CiAgICAgICAgICAgIHJpZ2h0W2ldID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgfQoKICAgICAgICAvLyBVc2UgYSBtYXAgdG8gc3RvcmUgZnJlcXVlbmN5IG9mIGVhY2ggcmlnaHQgc2hvZQogICAgICAgIE1hcDxJbnRlZ2VyLCBJbnRlZ2VyPiBmcmVxID0gbmV3IEhhc2hNYXA8PigpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgaSsrKSB7CiAgICAgICAgICAgIGZyZXEucHV0KHJpZ2h0W2ldLCBmcmVxLmdldE9yRGVmYXVsdChyaWdodFtpXSwgMCkgKyAxKTsKICAgICAgICB9CgogICAgICAgIC8vIExpc3QgdG8gc3RvcmUgdGhlIHJlc3VsdAogICAgICAgIExpc3Q8SW50ZWdlcj4gcmVzID0gbmV3IEFycmF5TGlzdDw+KCk7CgogICAgICAgIC8vIEl0ZXJhdGUgdGhyb3VnaCB0aGUgbGVmdCBzaG9lIGFycmF5CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgLy8gSWYgdGhlcmUgaXMgYSBtYXRjaGluZyByaWdodCBzaG9lLCBhZGQgdG8gcmVzdWx0IGFuZCBkZWNyZWFzZSBmcmVxdWVuY3kKICAgICAgICAgICAgaWYgKGZyZXEuZ2V0T3JEZWZhdWx0KGxlZnRbaV0sIDApID4gMCkgewogICAgICAgICAgICAgICAgcmVzLmFkZChsZWZ0W2ldKTsKICAgICAgICAgICAgICAgIGZyZXEucHV0KGxlZnRbaV0sIGZyZXEuZ2V0KGxlZnRbaV0pIC0gMSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIC8vIFNvcnQgdGhlIHJlc3VsdAogICAgICAgIENvbGxlY3Rpb25zLnNvcnQocmVzKTsKCiAgICAgICAgLy8gUHJpbnQgdGhlIHJlc3VsdAogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXMuc2l6ZSgpKTsKICAgICAgICBmb3IgKGludCBzaG9lIDogcmVzKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoc2hvZSArICIgIik7CiAgICAgICAgfQoKICAgICAgICBzY2FubmVyLmNsb3NlKCk7CiAgICB9Cn0K
Solution Python
# Read inputs
n, m = map(int, input().split())
left = list(map(int, input().split()))
right = list(map(int, input().split()))
# Declare a dictionary to store frequency of each right shoe
freq = {}
for shoe in right:
if shoe in freq:
freq[shoe] += 1
else:
freq[shoe] = 1
# Declare the result list
res = []
# Iterate through the left shoe array
for shoe in left:
# If there is a matching right shoe, add it to the result
# and decrease the frequency of the right shoe of the same ID
if freq.get(shoe, 0) > 0:
res.append(shoe)
freq[shoe] -= 1
# Sort the result
res.sort()
# Print the result
print(len(res))
print(*res)
IyBSZWFkIGlucHV0cw0KbiwgbSA9IG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkNCmxlZnQgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpDQpyaWdodCA9IGxpc3QobWFwKGludCwgaW5wdXQoKS5zcGxpdCgpKSkNCg0KIyBEZWNsYXJlIGEgZGljdGlvbmFyeSB0byBzdG9yZSBmcmVxdWVuY3kgb2YgZWFjaCByaWdodCBzaG9lDQpmcmVxID0ge30NCg0KZm9yIHNob2UgaW4gcmlnaHQ6DQogICAgaWYgc2hvZSBpbiBmcmVxOg0KICAgICAgICBmcmVxW3Nob2VdICs9IDENCiAgICBlbHNlOg0KICAgICAgICBmcmVxW3Nob2VdID0gMQ0KDQojIERlY2xhcmUgdGhlIHJlc3VsdCBsaXN0DQpyZXMgPSBbXQ0KDQojIEl0ZXJhdGUgdGhyb3VnaCB0aGUgbGVmdCBzaG9lIGFycmF5DQpmb3Igc2hvZSBpbiBsZWZ0Og0KICAgICMgSWYgdGhlcmUgaXMgYSBtYXRjaGluZyByaWdodCBzaG9lLCBhZGQgaXQgdG8gdGhlIHJlc3VsdA0KICAgICMgYW5kIGRlY3JlYXNlIHRoZSBmcmVxdWVuY3kgb2YgdGhlIHJpZ2h0IHNob2Ugb2YgdGhlIHNhbWUgSUQNCiAgICBpZiBmcmVxLmdldChzaG9lLCAwKSA+IDA6DQogICAgICAgIHJlcy5hcHBlbmQoc2hvZSkNCiAgICAgICAgZnJlcVtzaG9lXSAtPSAxDQoNCiMgU29ydCB0aGUgcmVzdWx0DQpyZXMuc29ydCgpDQoNCiMgUHJpbnQgdGhlIHJlc3VsdA0KcHJpbnQobGVuKHJlcykpDQpwcmludCgqcmVzKQ0K
Problem Statement 5
As the festival season approaches, Simon’s mother assigns him N tasks to be completed in D days. Each task has a difficulty level represented by jobs[i] (where 0 <= i < N). Simon must complete at least one task each day, using all D days.
The difficulty Simon experiences each day is defined by the highest difficulty task he completes that day. To work on task i, all preceding tasks j (where 0 <= j < i) must be completed first.
Given an array of task difficulty and an integer D, representing the difficulty of each task and the number of days, respectively, determine the minimum total difficulty Simon faces to complete all tasks. If it’s not possible to schedule the tasks within the given days, return -1.
Input Format
The first line contains an integer B, the size of the array.
The second line contains N space-separated integers representing the array Arr[i].
The last line contains an integer D, denoting the number of days.
Output Format
Print a single integer with minimum difficulty representing the minimum difficulty Simon faces when scheduling all tasks.
Constraints
1 <= N <= 3*10^2
0<= Arr[i] <= 10^3
1<= D<=10
Solution C++
#include <bits/stdc++.h>
using namespace std;
int dp[301][11];//dp[idx][k]
int solve(vector&arr,int idx,int k)
{
if(idx==arr.size()&&k==0)return 0;
if(idx==arr.size()||k<=0)return INT_MAX/2;
if(dp[idx][k]!=-1)return dp[idx][k];
int ans=INT_MAX;
int mx=INT_MIN;
for(int i=idx;i<arr.size();i++)
{
mx=max(mx,arr[i]);
ans=min(ans,mx+solve(arr,i+1,k-1));
}
return dp[idx][k]=ans;
}
int minDifficulty(vectorjobs,int d)
{
if(d>jobs.size())return -1;//we can't divide if d>sizeof(arr)
memset(dp,-1,sizeof(dp));
return solve(jobs,0,d);
}
int main()
{
int n;
cin>>n;
vectorvec(n);
for(int i=0;i<n;i++)
cin>>vec[i];
int d;
cin>>d;
cout<<minDifficulty(vec,d)<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBkcFszMDFdWzExXTsvL2RwW2lkeF1ba10KICAgIGludCBzb2x2ZSh2ZWN0b3I8aW50PiZhcnIsaW50IGlkeCxpbnQgaykKICAgIHsKICAgICAgICBpZihpZHg9PWFyci5zaXplKCkmJms9PTApcmV0dXJuIDA7CiAgICAgICAgaWYoaWR4PT1hcnIuc2l6ZSgpfHxrPD0wKXJldHVybiBJTlRfTUFYLzI7CiAgICAgICAgaWYoZHBbaWR4XVtrXSE9LTEpcmV0dXJuIGRwW2lkeF1ba107CiAgICAgICAgaW50IGFucz1JTlRfTUFYOwogICAgICAgIGludCBteD1JTlRfTUlOOwogICAgICAgIGZvcihpbnQgaT1pZHg7aTxhcnIuc2l6ZSgpO2krKykKICAgICAgICB7CiAgICAgICAgICAgIG14PW1heChteCxhcnJbaV0pOwogICAgICAgICAgICBhbnM9bWluKGFucyxteCtzb2x2ZShhcnIsaSsxLGstMSkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gZHBbaWR4XVtrXT1hbnM7CiAgICB9CmludCBtaW5EaWZmaWN1bHR5KHZlY3RvcjxpbnQ+am9icyxpbnQgZCkKewogICAgaWYoZD5qb2JzLnNpemUoKSlyZXR1cm4gLTE7Ly93ZSBjYW4ndCBkaXZpZGUgaWYgZD5zaXplb2YoYXJyKQogICAgbWVtc2V0KGRwLC0xLHNpemVvZihkcCkpOwogICAgcmV0dXJuIHNvbHZlKGpvYnMsMCxkKTsKfQppbnQgbWFpbigpCnsKICAgIGludCBuOwogICAgY2luPj5uOwogICAgdmVjdG9yPGludD52ZWMobik7CiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgY2luPj52ZWNbaV07CiAgICBpbnQgZDsKICAgIGNpbj4+ZDsKICAgIGNvdXQ8PG1pbkRpZmZpY3VsdHkodmVjLGQpPDxlbmRsOwoKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.util.*;
public class Main {
static int[][] dp = new int[301][11]; // dp[idx][k] // creating a lookup table for the subproblem.
public static int solve(List arr, int idx, int k) {
if (idx == arr.size() && k == 0) return 0; // if no days remaining or all elements are visited.
if (idx == arr.size() || k <= 0) return Integer.MAX_VALUE / 2; // if the array is already visited or days are already finished.
if (dp[idx][k] != -1) return dp[idx][k]; // pick the answer from the lookup table for a subproblem.
int ans = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
for (int i = idx; i < arr.size(); i++) {
mx = Math.max(mx, arr.get(i)); // updating the max till the ith index
ans = Math.min(ans, mx + solve(arr, i + 1, k - 1)); // calling the function by considering the segment.
}
return dp[idx][k] = ans; // updating the answer of subproblem.
}
public static int minDifficulty(List jobs, int d) {
if (d > jobs.size()) return -1; // we can't divide if d > sizeof(arr)
for (int[] row : dp) Arrays.fill(row, -1);
return solve(jobs, 0, d);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List vec = new ArrayList<>();
for (int i = 0; i < n; i++) vec.add(sc.nextInt());
int d = sc.nextInt();
System.out.println(minDifficulty(vec, d));
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgc3RhdGljIGludFtdW10gZHAgPSBuZXcgaW50WzMwMV1bMTFdOyAvLyBkcFtpZHhdW2tdIC8vIGNyZWF0aW5nIGEgbG9va3VwIHRhYmxlIGZvciB0aGUgc3VicHJvYmxlbS4KCiAgICBwdWJsaWMgc3RhdGljIGludCBzb2x2ZShMaXN0PEludGVnZXI+IGFyciwgaW50IGlkeCwgaW50IGspIHsKICAgICAgICBpZiAoaWR4ID09IGFyci5zaXplKCkgJiYgayA9PSAwKSByZXR1cm4gMDsgLy8gaWYgbm8gZGF5cyByZW1haW5pbmcgb3IgYWxsIGVsZW1lbnRzIGFyZSB2aXNpdGVkLgogICAgICAgIGlmIChpZHggPT0gYXJyLnNpemUoKSB8fCBrIDw9IDApIHJldHVybiBJbnRlZ2VyLk1BWF9WQUxVRSAvIDI7IC8vIGlmIHRoZSBhcnJheSBpcyBhbHJlYWR5IHZpc2l0ZWQgb3IgZGF5cyBhcmUgYWxyZWFkeSBmaW5pc2hlZC4KICAgICAgICBpZiAoZHBbaWR4XVtrXSAhPSAtMSkgcmV0dXJuIGRwW2lkeF1ba107IC8vIHBpY2sgdGhlIGFuc3dlciBmcm9tIHRoZSBsb29rdXAgdGFibGUgZm9yIGEgc3VicHJvYmxlbS4KICAgICAgICBpbnQgYW5zID0gSW50ZWdlci5NQVhfVkFMVUU7CiAgICAgICAgaW50IG14ID0gSW50ZWdlci5NSU5fVkFMVUU7CiAgICAgICAgZm9yIChpbnQgaSA9IGlkeDsgaSA8IGFyci5zaXplKCk7IGkrKykgewogICAgICAgICAgICBteCA9IE1hdGgubWF4KG14LCBhcnIuZ2V0KGkpKTsgLy8gdXBkYXRpbmcgdGhlIG1heCB0aWxsIHRoZSBpdGggaW5kZXgKICAgICAgICAgICAgYW5zID0gTWF0aC5taW4oYW5zLCBteCArIHNvbHZlKGFyciwgaSArIDEsIGsgLSAxKSk7IC8vIGNhbGxpbmcgdGhlIGZ1bmN0aW9uIGJ5IGNvbnNpZGVyaW5nIHRoZSBzZWdtZW50LgogICAgICAgIH0KICAgICAgICByZXR1cm4gZHBbaWR4XVtrXSA9IGFuczsgLy8gdXBkYXRpbmcgdGhlIGFuc3dlciBvZiBzdWJwcm9ibGVtLgogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgaW50IG1pbkRpZmZpY3VsdHkoTGlzdDxJbnRlZ2VyPiBqb2JzLCBpbnQgZCkgewogICAgICAgIGlmIChkID4gam9icy5zaXplKCkpIHJldHVybiAtMTsgLy8gd2UgY2FuJ3QgZGl2aWRlIGlmIGQgPiBzaXplb2YoYXJyKQogICAgICAgIGZvciAoaW50W10gcm93IDogZHApIEFycmF5cy5maWxsKHJvdywgLTEpOwogICAgICAgIHJldHVybiBzb2x2ZShqb2JzLCAwLCBkKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU2Nhbm5lciBzYyA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgaW50IG4gPSBzYy5uZXh0SW50KCk7CiAgICAgICAgTGlzdDxJbnRlZ2VyPiB2ZWMgPSBuZXcgQXJyYXlMaXN0PD4oKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgdmVjLmFkZChzYy5uZXh0SW50KCkpOwogICAgICAgIGludCBkID0gc2MubmV4dEludCgpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihtaW5EaWZmaWN1bHR5KHZlYywgZCkpOwogICAgfQp9Cg==
Solution Python
def solve(arr, idx, k, dp):
if idx == len(arr) and k == 0:
return 0 # if no days remaining or all elements are visited.
if idx == len(arr) or k <= 0:
return float('inf') / 2 # if the array is already visited or days are already finished.
if dp[idx][k] != -1:
return dp[idx][k] # pick the answer from the lookup table for a subproblem.
ans = float('inf')
mx = float('-inf')
for i in range(idx, len(arr)):
mx = max(mx, arr[i]) # updating the max till the ith index
ans = min(ans, mx + solve(arr, i + 1, k - 1, dp)) # calling the function by considering the segment.
dp[idx][k] = ans # updating the answer of subproblem.
return dp[idx][k]
def min_difficulty(jobs, d):
if d > len(jobs):
return -1 # we can't divide if d > sizeof(arr)
dp = [[-1] * (d + 1) for _ in range(len(jobs) + 1)]
return solve(jobs, 0, d, dp)
if __name__ == "__main__":
n = int(input())
vec = list(map(int, input().split()))
d = int(input())
print(min_difficulty(vec, d))
ZGVmIHNvbHZlKGFyciwgaWR4LCBrLCBkcCk6DQogICAgaWYgaWR4ID09IGxlbihhcnIpIGFuZCBrID09IDA6DQogICAgICAgIHJldHVybiAwICAjIGlmIG5vIGRheXMgcmVtYWluaW5nIG9yIGFsbCBlbGVtZW50cyBhcmUgdmlzaXRlZC4NCiAgICBpZiBpZHggPT0gbGVuKGFycikgb3IgayA8PSAwOg0KICAgICAgICByZXR1cm4gZmxvYXQoJ2luZicpIC8gMiAgIyBpZiB0aGUgYXJyYXkgaXMgYWxyZWFkeSB2aXNpdGVkIG9yIGRheXMgYXJlIGFscmVhZHkgZmluaXNoZWQuDQogICAgaWYgZHBbaWR4XVtrXSAhPSAtMToNCiAgICAgICAgcmV0dXJuIGRwW2lkeF1ba10gICMgcGljayB0aGUgYW5zd2VyIGZyb20gdGhlIGxvb2t1cCB0YWJsZSBmb3IgYSBzdWJwcm9ibGVtLg0KICAgIGFucyA9IGZsb2F0KCdpbmYnKQ0KICAgIG14ID0gZmxvYXQoJy1pbmYnKQ0KICAgIGZvciBpIGluIHJhbmdlKGlkeCwgbGVuKGFycikpOg0KICAgICAgICBteCA9IG1heChteCwgYXJyW2ldKSAgIyB1cGRhdGluZyB0aGUgbWF4IHRpbGwgdGhlIGl0aCBpbmRleA0KICAgICAgICBhbnMgPSBtaW4oYW5zLCBteCArIHNvbHZlKGFyciwgaSArIDEsIGsgLSAxLCBkcCkpICAjIGNhbGxpbmcgdGhlIGZ1bmN0aW9uIGJ5IGNvbnNpZGVyaW5nIHRoZSBzZWdtZW50Lg0KICAgIGRwW2lkeF1ba10gPSBhbnMgICMgdXBkYXRpbmcgdGhlIGFuc3dlciBvZiBzdWJwcm9ibGVtLg0KICAgIHJldHVybiBkcFtpZHhdW2tdDQoNCmRlZiBtaW5fZGlmZmljdWx0eShqb2JzLCBkKToNCiAgICBpZiBkID4gbGVuKGpvYnMpOg0KICAgICAgICByZXR1cm4gLTEgICMgd2UgY2FuJ3QgZGl2aWRlIGlmIGQgPiBzaXplb2YoYXJyKQ0KICAgIGRwID0gW1stMV0gKiAoZCArIDEpIGZvciBfIGluIHJhbmdlKGxlbihqb2JzKSArIDEpXQ0KICAgIHJldHVybiBzb2x2ZShqb2JzLCAwLCBkLCBkcCkNCg0KaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoNCiAgICBuID0gaW50KGlucHV0KCkpDQogICAgdmVjID0gbGlzdChtYXAoaW50LCBpbnB1dCgpLnNwbGl0KCkpKQ0KICAgIGQgPSBpbnQoaW5wdXQoKSkNCiAgICBwcmludChtaW5fZGlmZmljdWx0eSh2ZWMsIGQpKQ0K
Conclusion
Cracking the CoCubes coding test is highly achievable with focused preparation. Practising a range of questions, reviewing past questions, and emphasising commonly repeated topics can give freshers the necessary advantage.
As you prepare, remember to focus on clarity, efficiency, and error-free code to maximise your score. Keep honing your skills, stay consistent, and you’ll be ready to tackle CoCubes confidently!
Frequently Asked Questions (FAQs)
1. What types of questions are asked in the CoCubes coding test?
The CoCubes coding test includes questions on basic programming, data structures, algorithms, and string manipulations, with a mix of beginner to advanced difficulty.
2. What programming languages are allowed in CoCubes coding tests?
Candidates can choose from C, C++, Java, and Python, with flexibility in selecting the language they are most comfortable in.
3. Where can I find previous CoCubes coding questions for practice?
Questions from previous years can often be found on online forums, educational platforms, and paid test-prep services that specialise in placement materials.
4. How many questions are typically repeated in CoCubes coding tests?
While exact questions may not repeat verbatim, similar problem types (e.g., palindrome checks sorting algorithms) appear frequently. Practising these can boost your familiarity.
5. What score is needed to pass the CoCubes coding test?
There isn’t a fixed passing score as requirements vary by company, but aiming for accuracy and avoiding syntax errors is essential for a strong performance.
Disclaimer: While we strive for accuracy, we do not guarantee its completeness or reliability. Readers are encouraged to verify all facts and statistics from the official company website or check independently before making decisions.
Suggested reads:
- Deloitte Coding Questions: Top 5 Coding Questions for Freshers
- TCS Digital Coding Questions: Top 5 Coding Questions & Solutions
- TCS Ninja Coding Questions: Top 5 Coding Questions with Solution
- HCL Coding Questions: Coding Pattern with Questions & Solutions
- Infosys Verbal Ability Questions: Best MCQs & Tips for Freshers 2025
Instinctively, I fall for nature, music, humor, reading, writing, listening, traveling, observing, learning, unlearning, friendship, exercise, etc., all these from the cradle to the grave- that's ME! It's my irrefutable belief in the uniqueness of all. I'll vehemently defend your right to be your best while I expect the same from you!
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Blogs you need to hog!
How To Write Finance Cover Letter For Morgan Stanley (+Free Sample!)
55+ Data Structure Interview Questions For 2025 (Detailed Answers)
How To Negotiate Salary With HR: Tips And Insider Advice
Comments
Add comment