- Overview of TCS CodeVita
- TCS CodeVita Questions Pattern
- TCS CodeVita Previous Year Questions with Answers
- Top 5 TCS CodeVita Questions with Answers
- Conclusion
- Frequently Asked Questions (FAQs)
TCS CodeVita Previous Year Questions: MCQs and Tips for Freshers
Tata Consultancy Services (TCS) CodeVita is one of the most sought-after coding competitions for freshers aiming to kick-start their corporate careers. As a global coding challenge, CodeVita tests participants on problem-solving skills, algorithms, and programming proficiency. Being well-prepared for the TCS CodeVita competition is crucial to securing a position at one of the world’s leading IT companies.
In this article, we will explore everything freshers need to know about TCS CodeVita, including questions from previous years, patterns, and tips to ace the competition. Whether you’re a beginner or have already started practising, this guide will help you understand what to expect, how to prepare, and where to focus your efforts.
Overview of TCS CodeVita
TCS CodeVita is a global online programming competition aimed at hiring the best coding talent. The competition usually consists of multiple rounds, and participants need to solve coding challenges under time constraints. The test assesses skills in problem-solving, algorithmic thinking, and programming.
Key Features of TCS CodeVita
|
Feature |
Details |
|
Eligibility |
Open to all engineering and computer science students |
|
Rounds |
Typically 2-3 rounds, starting with qualifying and followed by finals |
|
Languages |
It supports multiple programming languages (C, C++, Java, Python, etc.). |
|
Duration |
Ranges from 2-3 hours depending on the round |
|
Scoring |
Based on problem-solving speed and correctness of solutions |
Tip for Freshers: Since TCS CodeVita is highly competitive, it's important to practice coding regularly and be familiar with the programming languages supported by the platform.
TCS CodeVita Questions Pattern
The pattern of questions in TCS CodeVita typically includes algorithm-based challenges and problem-solving tasks that test your logic and coding skills. Here is an overview of the most common question types:
|
Category |
Description |
Example Questions |
|
Algorithmic Problems |
Problems that require an understanding of algorithms to solve efficiently |
"Given a list of integers, find the longest subsequence of non-repeating numbers." |
|
Mathematical Problems |
Questions based on mathematical concepts like prime numbers, GCD, etc. |
"Find the greatest common divisor (GCD) of two numbers." |
|
Dynamic Programming |
Problems that can be solved by breaking them down into simpler subproblems |
"Find the number of ways to climb a staircase with n steps." |
|
Greedy Algorithms |
Problems that require choosing the locally optimal solution at each step |
"You are given an array of intervals. Merge all overlapping intervals." |
|
Data Structures |
Questions based on data structures like arrays, linked lists, trees, and graphs |
"Implement a stack using two queues." |
|
Puzzle Problems |
Logical problems that test reasoning and problem-solving ability |
"You are given two ropes that each take one hour to burn. How can you measure 45 minutes?" |
Quick Tip: Be prepared for a combination of these problem types and focus on understanding the underlying principles of algorithms and data structures.
TCS CodeVita Previous Year Questions with Answers
Reviewing previous year’s questions is one of the most effective ways to prepare for TCS CodeVita. Here’s a look at some commonly asked questions and their solutions:
|
Previous Year Question |
Solution |
|
"Find the smallest missing positive integer from an unsorted array." |
Solution: Use a hash set to track the elements, then iterate through the array to find the smallest missing integer. |
|
"Given a string, write a function to check if it is a palindrome." |
Solution: Reverse the string and compare it with the original. |
|
"Implement an algorithm to find the longest common subsequence (LCS)." |
Solution: Dynamic programming is used to solve this problem by constructing a 2D array to store the lengths of common subsequences. |
Are you looking for coding assessment questions related to job placement? Click here to access coding practice sessions from moderate to challenging levels.
Top 5 TCS CodeVita Questions with Answers
Problem Statement 1
Assert whether the given string has all the letters of the English alphabet or not.
If yes return "True" else return "False".
Assume the string contains nothing but lowercase English letters.
Input Format
The string to be checked.
Output Format
Display "True" if all the letters in English alphabets are present else display "False".
Note: Output is case-sensitive.
Constraints
1<=|S|<=1e^5
Solution C++
#include <bits/stdc++.h>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string s;
cin>>s;
int arr[26];
memset(arr,0,sizeof(arr));
for(int i=0;i<s.size();i++){
arr[s[i]-'a']++;
}
int f=0;
for(int i=0;i<26;i++){
if(arr[i]==0)
{
f=1;
break;
}
}
if(f)
cout<<"False"<<endl;
else
cout<<"True"<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCiAgICAvKiBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUICovIAogICAgc3RyaW5nIHM7CiAgICBjaW4+PnM7CgogICAgaW50IGFyclsyNl07CgogICAgbWVtc2V0KGFyciwwLHNpemVvZihhcnIpKTsKCiAgICBmb3IoaW50IGk9MDtpPHMuc2l6ZSgpO2krKyl7CiAgICAgIGFycltzW2ldLSdhJ10rKzsKICAgIH0KICAgIGludCBmPTA7CiAgICAKCiAgICBmb3IoaW50IGk9MDtpPDI2O2krKyl7CgogICAgICBpZihhcnJbaV09PTApCiAgICAgIHsKICAgICAgICBmPTE7CiAgICAgICAgYnJlYWs7CiAgICAgIH0KICAgIH0KCiAgICBpZihmKQogICAgY291dDw8IkZhbHNlIjw8ZW5kbDsKICAgIGVsc2UKICAgIGNvdXQ8PCJUcnVlIjw8ZW5kbDsKCgogICAgcmV0dXJuIDA7Cn0K
Solution Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
TreeSet set=new TreeSet<>();
for(int i=0;i<str.length();i++)
set.add(str.charAt(i));
if(set.size()==26)
System.out.print("True");
else
System.out.print("False");
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLm1hdGguKjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC4qOwoKY2xhc3MgU29sdXRpb24gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjPW5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgU3RyaW5nIHN0cj1zYy5uZXh0KCk7CiAgICAgICAgVHJlZVNldDxDaGFyYWN0ZXI+IHNldD1uZXcgVHJlZVNldDw+KCk7CiAgICAgICAgZm9yKGludCBpPTA7aTxzdHIubGVuZ3RoKCk7aSsrKQogICAgICAgICAgICBzZXQuYWRkKHN0ci5jaGFyQXQoaSkpOwogICAgICAgIGlmKHNldC5zaXplKCk9PTI2KQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJUcnVlIik7CiAgICAgICAgZWxzZQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJGYWxzZSIpOwogICAgfQp9Cg==
Solution Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
def has_all_letters(s):
# Define the set of all lowercase English letters
alphabet_set = set('abcdefghijklmnopqrstuvwxyz')
# Convert the input string to a set
input_set = set(s)
# Check if all letters are present in the input set
if alphabet_set.issubset(input_set):
return "True"
else:
return "False"
# Input reading
input_string = input().strip() # Read input and remove any extra spaces
# Output the result
print(has_all_letters(input_string))
IyBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUDQpkZWYgaGFzX2FsbF9sZXR0ZXJzKHMpOg0KICAgICMgRGVmaW5lIHRoZSBzZXQgb2YgYWxsIGxvd2VyY2FzZSBFbmdsaXNoIGxldHRlcnMNCiAgICBhbHBoYWJldF9zZXQgPSBzZXQoJ2FiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6JykNCiAgICANCiAgICAjIENvbnZlcnQgdGhlIGlucHV0IHN0cmluZyB0byBhIHNldA0KICAgIGlucHV0X3NldCA9IHNldChzKQ0KICAgIA0KICAgICMgQ2hlY2sgaWYgYWxsIGxldHRlcnMgYXJlIHByZXNlbnQgaW4gdGhlIGlucHV0IHNldA0KICAgIGlmIGFscGhhYmV0X3NldC5pc3N1YnNldChpbnB1dF9zZXQpOg0KICAgICAgICByZXR1cm4gIlRydWUiDQogICAgZWxzZToNCiAgICAgICAgcmV0dXJuICJGYWxzZSINCg0KIyBJbnB1dCByZWFkaW5nDQppbnB1dF9zdHJpbmcgPSBpbnB1dCgpLnN0cmlwKCkgICMgUmVhZCBpbnB1dCBhbmQgcmVtb3ZlIGFueSBleHRyYSBzcGFjZXMNCg0KIyBPdXRwdXQgdGhlIHJlc3VsdA0KcHJpbnQoaGFzX2FsbF9sZXR0ZXJzKGlucHV0X3N0cmluZykpDQo=
Problem Statement 2
You are provided with a 2D array(N*M). Your task is to create an ArrayList of Node objects, where each row of the 2D array corresponds to one entry in the ArrayList. After that, a doubly-linked list is constructed, arranging nodes first by even indices from the ArrayList, followed by the odd indices.
Input Format
The first line contains an integer N, representing the size of the array row.
The second line contains an integer M, representing the size of array col.
The third line contains an array.
Output Format
return the linked list
Constraints
1 <= N < 10^2
1 <= M < 10^2
Solution C
#include
#include
#include
#include
typedef struct Node {
int value;
struct Node* prev;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to print the doubly linked list
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d", current->value);
if (current->next != NULL) {
printf(" <---> ");
}
current = current->next;
}
printf(" <---> null\n");
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int N, M;
scanf("%d", &N); // Read the number of rows
scanf("%d", &M); // Read the number of columns
int arr[N][M]; // Declare the 2D array to hold the input values
// Read the array from input
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf("%d", &arr[i][j]);
}
}
// Array to store the nodes from even and odd indices
Node* evenHead = NULL;
Node* oddHead = NULL;
Node* evenTail = NULL;
Node* oddTail = NULL;
// Traverse each row and create nodes for each element
for (int i = 0; i < N; i++) {
Node* rowHead = NULL;
Node* rowTail = NULL;
// Create nodes for the current row
for (int j = 0; j < M; j++) {
Node* newNode = createNode(arr[i][j]);
// If this is the first node in the row
if (rowHead == NULL) {
rowHead = newNode;
}
if (rowTail != NULL) {
rowTail->next = newNode;
newNode->prev = rowTail;
}
rowTail = newNode;
}
// Add the row's nodes to the appropriate list (even or odd index row)
if (i % 2 == 0) {
// Even index row (0, 2, 4,...)
if (evenHead == NULL) {
evenHead = rowHead;
evenTail = rowTail;
} else {
evenTail->next = rowHead;
rowHead->prev = evenTail;
evenTail = rowTail;
}
} else {
// Odd index row (1, 3, 5,...)
if (oddHead == NULL) {
oddHead = rowHead;
oddTail = rowTail;
} else {
oddTail->next = rowHead;
rowHead->prev = oddTail;
oddTail = rowTail;
}
}
}
// Merge even and odd lists into one final list
if (evenTail != NULL) {
evenTail->next = oddHead;
if (oddHead != NULL) {
oddHead->prev = evenTail;
}
}
// The head of the list will be the head of the even list
Node* finalHead = evenHead;
// Print the final linked list
printList(finalHead);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgp0eXBlZGVmIHN0cnVjdCBOb2RlIHsKICAgIGludCB2YWx1ZTsKICAgIHN0cnVjdCBOb2RlKiBwcmV2OwogICAgc3RydWN0IE5vZGUqIG5leHQ7Cn0gTm9kZTsKCi8vIEZ1bmN0aW9uIHRvIGNyZWF0ZSBhIG5ldyBub2RlCk5vZGUqIGNyZWF0ZU5vZGUoaW50IHZhbHVlKSB7CiAgICBOb2RlKiBuZXdOb2RlID0gKE5vZGUqKW1hbGxvYyhzaXplb2YoTm9kZSkpOwogICAgbmV3Tm9kZS0+dmFsdWUgPSB2YWx1ZTsKICAgIG5ld05vZGUtPnByZXYgPSBOVUxMOwogICAgbmV3Tm9kZS0+bmV4dCA9IE5VTEw7CiAgICByZXR1cm4gbmV3Tm9kZTsKfQoKLy8gRnVuY3Rpb24gdG8gcHJpbnQgdGhlIGRvdWJseSBsaW5rZWQgbGlzdAp2b2lkIHByaW50TGlzdChOb2RlKiBoZWFkKSB7CiAgICBOb2RlKiBjdXJyZW50ID0gaGVhZDsKICAgIHdoaWxlIChjdXJyZW50ICE9IE5VTEwpIHsKICAgICAgICBwcmludGYoIiVkIiwgY3VycmVudC0+dmFsdWUpOwogICAgICAgIGlmIChjdXJyZW50LT5uZXh0ICE9IE5VTEwpIHsKICAgICAgICAgICAgcHJpbnRmKCIgPC0tLT4gIik7CiAgICAgICAgfQogICAgICAgIGN1cnJlbnQgPSBjdXJyZW50LT5uZXh0OwogICAgfQogICAgcHJpbnRmKCIgPC0tLT4gbnVsbFxuIik7Cn0KCgppbnQgbWFpbigpIHsKCiAgICAvKiBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUICovICAgIAogICAgIGludCBOLCBNOwogICAgc2NhbmYoIiVkIiwgJk4pOyAgLy8gUmVhZCB0aGUgbnVtYmVyIG9mIHJvd3MKICAgIHNjYW5mKCIlZCIsICZNKTsgIC8vIFJlYWQgdGhlIG51bWJlciBvZiBjb2x1bW5zCgogICAgaW50IGFycltOXVtNXTsgIC8vIERlY2xhcmUgdGhlIDJEIGFycmF5IHRvIGhvbGQgdGhlIGlucHV0IHZhbHVlcwoKICAgIC8vIFJlYWQgdGhlIGFycmF5IGZyb20gaW5wdXQKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBNOyBqKyspIHsKICAgICAgICAgICAgc2NhbmYoIiVkIiwgJmFycltpXVtqXSk7CiAgICAgICAgfQogICAgfQoKICAgIC8vIEFycmF5IHRvIHN0b3JlIHRoZSBub2RlcyBmcm9tIGV2ZW4gYW5kIG9kZCBpbmRpY2VzCiAgICBOb2RlKiBldmVuSGVhZCA9IE5VTEw7CiAgICBOb2RlKiBvZGRIZWFkID0gTlVMTDsKICAgIE5vZGUqIGV2ZW5UYWlsID0gTlVMTDsKICAgIE5vZGUqIG9kZFRhaWwgPSBOVUxMOwoKICAgIC8vIFRyYXZlcnNlIGVhY2ggcm93IGFuZCBjcmVhdGUgbm9kZXMgZm9yIGVhY2ggZWxlbWVudAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICBOb2RlKiByb3dIZWFkID0gTlVMTDsKICAgICAgICBOb2RlKiByb3dUYWlsID0gTlVMTDsKCiAgICAgICAgLy8gQ3JlYXRlIG5vZGVzIGZvciB0aGUgY3VycmVudCByb3cKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IE07IGorKykgewogICAgICAgICAgICBOb2RlKiBuZXdOb2RlID0gY3JlYXRlTm9kZShhcnJbaV1bal0pOwoKICAgICAgICAgICAgLy8gSWYgdGhpcyBpcyB0aGUgZmlyc3Qgbm9kZSBpbiB0aGUgcm93CiAgICAgICAgICAgIGlmIChyb3dIZWFkID09IE5VTEwpIHsKICAgICAgICAgICAgICAgIHJvd0hlYWQgPSBuZXdOb2RlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChyb3dUYWlsICE9IE5VTEwpIHsKICAgICAgICAgICAgICAgIHJvd1RhaWwtPm5leHQgPSBuZXdOb2RlOwogICAgICAgICAgICAgICAgbmV3Tm9kZS0+cHJldiA9IHJvd1RhaWw7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcm93VGFpbCA9IG5ld05vZGU7CiAgICAgICAgfQoKICAgICAgICAvLyBBZGQgdGhlIHJvdydzIG5vZGVzIHRvIHRoZSBhcHByb3ByaWF0ZSBsaXN0IChldmVuIG9yIG9kZCBpbmRleCByb3cpCiAgICAgICAgaWYgKGkgJSAyID09IDApIHsKICAgICAgICAgICAgLy8gRXZlbiBpbmRleCByb3cgKDAsIDIsIDQsLi4uKQogICAgICAgICAgICBpZiAoZXZlbkhlYWQgPT0gTlVMTCkgewogICAgICAgICAgICAgICAgZXZlbkhlYWQgPSByb3dIZWFkOwogICAgICAgICAgICAgICAgZXZlblRhaWwgPSByb3dUYWlsOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZXZlblRhaWwtPm5leHQgPSByb3dIZWFkOwogICAgICAgICAgICAgICAgcm93SGVhZC0+cHJldiA9IGV2ZW5UYWlsOwogICAgICAgICAgICAgICAgZXZlblRhaWwgPSByb3dUYWlsOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgLy8gT2RkIGluZGV4IHJvdyAoMSwgMywgNSwuLi4pCiAgICAgICAgICAgIGlmIChvZGRIZWFkID09IE5VTEwpIHsKICAgICAgICAgICAgICAgIG9kZEhlYWQgPSByb3dIZWFkOwogICAgICAgICAgICAgICAgb2RkVGFpbCA9IHJvd1RhaWw7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBvZGRUYWlsLT5uZXh0ID0gcm93SGVhZDsKICAgICAgICAgICAgICAgIHJvd0hlYWQtPnByZXYgPSBvZGRUYWlsOwogICAgICAgICAgICAgICAgb2RkVGFpbCA9IHJvd1RhaWw7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8gTWVyZ2UgZXZlbiBhbmQgb2RkIGxpc3RzIGludG8gb25lIGZpbmFsIGxpc3QKICAgIGlmIChldmVuVGFpbCAhPSBOVUxMKSB7CiAgICAgICAgZXZlblRhaWwtPm5leHQgPSBvZGRIZWFkOwogICAgICAgIGlmIChvZGRIZWFkICE9IE5VTEwpIHsKICAgICAgICAgICAgb2RkSGVhZC0+cHJldiA9IGV2ZW5UYWlsOwogICAgICAgIH0KICAgIH0KCiAgICAvLyBUaGUgaGVhZCBvZiB0aGUgbGlzdCB3aWxsIGJlIHRoZSBoZWFkIG9mIHRoZSBldmVuIGxpc3QKICAgIE5vZGUqIGZpbmFsSGVhZCA9IGV2ZW5IZWFkOwoKICAgIC8vIFByaW50IHRoZSBmaW5hbCBsaW5rZWQgbGlzdAogICAgcHJpbnRMaXN0KGZpbmFsSGVhZCk7CgogICAgcmV0dXJuIDA7Cn0K
Solution Python
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def construct_linked_list(N, M, array):
# Step 1: Create an ArrayList of linked list heads for each row
node_rows = []
for i in range(N):
head = None
tail = None
for j in range(M):
new_node = Node(array[i][j])
if head is None: # Initialize the head of the row
head = new_node
tail = new_node
else: # Add new node to the row's linked list
tail.next = new_node
new_node.prev = tail
tail = new_node
node_rows.append(head)
# Step 2: Separate even-indexed and odd-indexed rows
even_rows = [node_rows[i] for i in range(N) if i % 2 == 0]
odd_rows = [node_rows[i] for i in range(N) if i % 2 != 0]
# Step 3: Merge the even and odd rows into a single doubly linked list
final_head = None
final_tail = None
def append_row_to_list(row_head):
nonlocal final_head, final_tail
current = row_head
while current:
new_node = Node(current.data)
if final_head is None: # Initialize the final list
final_head = new_node
final_tail = new_node
else: # Add to the final list
final_tail.next = new_node
new_node.prev = final_tail
final_tail = new_node
current = current.next
# Append all even rows first, then odd rows
for row in even_rows:
append_row_to_list(row)
for row in odd_rows:
append_row_to_list(row)
# Step 4: Print the final doubly linked list
result = []
current = final_head
while current:
result.append(str(current.data))
current = current.next
result.append("null")
return " <---> ".join(result)
# Input
N = int(input()) # Number of rows
M = int(input()) # Number of columns
array = []
for _ in range(N):
array.append(list(map(int, input().split())))
# Output
print(construct_linked_list(N, M, array))
Y2xhc3MgTm9kZToNCiAgICBkZWYgX19pbml0X18oc2VsZiwgZGF0YSk6DQogICAgICAgIHNlbGYuZGF0YSA9IGRhdGENCiAgICAgICAgc2VsZi5wcmV2ID0gTm9uZQ0KICAgICAgICBzZWxmLm5leHQgPSBOb25lDQoNCmRlZiBjb25zdHJ1Y3RfbGlua2VkX2xpc3QoTiwgTSwgYXJyYXkpOg0KICAgICMgU3RlcCAxOiBDcmVhdGUgYW4gQXJyYXlMaXN0IG9mIGxpbmtlZCBsaXN0IGhlYWRzIGZvciBlYWNoIHJvdw0KICAgIG5vZGVfcm93cyA9IFtdDQogICAgZm9yIGkgaW4gcmFuZ2UoTik6DQogICAgICAgIGhlYWQgPSBOb25lDQogICAgICAgIHRhaWwgPSBOb25lDQogICAgICAgIGZvciBqIGluIHJhbmdlKE0pOg0KICAgICAgICAgICAgbmV3X25vZGUgPSBOb2RlKGFycmF5W2ldW2pdKQ0KICAgICAgICAgICAgaWYgaGVhZCBpcyBOb25lOiAgIyBJbml0aWFsaXplIHRoZSBoZWFkIG9mIHRoZSByb3cNCiAgICAgICAgICAgICAgICBoZWFkID0gbmV3X25vZGUNCiAgICAgICAgICAgICAgICB0YWlsID0gbmV3X25vZGUNCiAgICAgICAgICAgIGVsc2U6ICAjIEFkZCBuZXcgbm9kZSB0byB0aGUgcm93J3MgbGlua2VkIGxpc3QNCiAgICAgICAgICAgICAgICB0YWlsLm5leHQgPSBuZXdfbm9kZQ0KICAgICAgICAgICAgICAgIG5ld19ub2RlLnByZXYgPSB0YWlsDQogICAgICAgICAgICAgICAgdGFpbCA9IG5ld19ub2RlDQogICAgICAgIG5vZGVfcm93cy5hcHBlbmQoaGVhZCkNCg0KICAgICMgU3RlcCAyOiBTZXBhcmF0ZSBldmVuLWluZGV4ZWQgYW5kIG9kZC1pbmRleGVkIHJvd3MNCiAgICBldmVuX3Jvd3MgPSBbbm9kZV9yb3dzW2ldIGZvciBpIGluIHJhbmdlKE4pIGlmIGkgJSAyID09IDBdDQogICAgb2RkX3Jvd3MgPSBbbm9kZV9yb3dzW2ldIGZvciBpIGluIHJhbmdlKE4pIGlmIGkgJSAyICE9IDBdDQoNCiAgICAjIFN0ZXAgMzogTWVyZ2UgdGhlIGV2ZW4gYW5kIG9kZCByb3dzIGludG8gYSBzaW5nbGUgZG91Ymx5IGxpbmtlZCBsaXN0DQogICAgZmluYWxfaGVhZCA9IE5vbmUNCiAgICBmaW5hbF90YWlsID0gTm9uZQ0KDQogICAgZGVmIGFwcGVuZF9yb3dfdG9fbGlzdChyb3dfaGVhZCk6DQogICAgICAgIG5vbmxvY2FsIGZpbmFsX2hlYWQsIGZpbmFsX3RhaWwNCiAgICAgICAgY3VycmVudCA9IHJvd19oZWFkDQogICAgICAgIHdoaWxlIGN1cnJlbnQ6DQogICAgICAgICAgICBuZXdfbm9kZSA9IE5vZGUoY3VycmVudC5kYXRhKQ0KICAgICAgICAgICAgaWYgZmluYWxfaGVhZCBpcyBOb25lOiAgIyBJbml0aWFsaXplIHRoZSBmaW5hbCBsaXN0DQogICAgICAgICAgICAgICAgZmluYWxfaGVhZCA9IG5ld19ub2RlDQogICAgICAgICAgICAgICAgZmluYWxfdGFpbCA9IG5ld19ub2RlDQogICAgICAgICAgICBlbHNlOiAgIyBBZGQgdG8gdGhlIGZpbmFsIGxpc3QNCiAgICAgICAgICAgICAgICBmaW5hbF90YWlsLm5leHQgPSBuZXdfbm9kZQ0KICAgICAgICAgICAgICAgIG5ld19ub2RlLnByZXYgPSBmaW5hbF90YWlsDQogICAgICAgICAgICAgICAgZmluYWxfdGFpbCA9IG5ld19ub2RlDQogICAgICAgICAgICBjdXJyZW50ID0gY3VycmVudC5uZXh0DQoNCiAgICAjIEFwcGVuZCBhbGwgZXZlbiByb3dzIGZpcnN0LCB0aGVuIG9kZCByb3dzDQogICAgZm9yIHJvdyBpbiBldmVuX3Jvd3M6DQogICAgICAgIGFwcGVuZF9yb3dfdG9fbGlzdChyb3cpDQogICAgZm9yIHJvdyBpbiBvZGRfcm93czoNCiAgICAgICAgYXBwZW5kX3Jvd190b19saXN0KHJvdykNCg0KICAgICMgU3RlcCA0OiBQcmludCB0aGUgZmluYWwgZG91Ymx5IGxpbmtlZCBsaXN0DQogICAgcmVzdWx0ID0gW10NCiAgICBjdXJyZW50ID0gZmluYWxfaGVhZA0KICAgIHdoaWxlIGN1cnJlbnQ6DQogICAgICAgIHJlc3VsdC5hcHBlbmQoc3RyKGN1cnJlbnQuZGF0YSkpDQogICAgICAgIGN1cnJlbnQgPSBjdXJyZW50Lm5leHQNCiAgICByZXN1bHQuYXBwZW5kKCJudWxsIikNCg0KICAgIHJldHVybiAiIDwtLS0+ICIuam9pbihyZXN1bHQpDQoNCiMgSW5wdXQNCk4gPSBpbnQoaW5wdXQoKSkgICMgTnVtYmVyIG9mIHJvd3MNCk0gPSBpbnQoaW5wdXQoKSkgICMgTnVtYmVyIG9mIGNvbHVtbnMNCmFycmF5ID0gW10NCmZvciBfIGluIHJhbmdlKE4pOg0KICAgIGFycmF5LmFwcGVuZChsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpKQ0KDQojIE91dHB1dA0KcHJpbnQoY29uc3RydWN0X2xpbmtlZF9saXN0KE4sIE0sIGFycmF5KSkNCg==
Solution Java
import java.util.*;
public class Main {
static class doubleNode {
int doubledata;
doubleNode doubleprev;
doubleNode doublenext;
doubleNode(int doubledata) {
this.doubledata = doubledata;
}
}
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
static void CreateNodeList(int [][] arr , List list ){
for(int i = 0 ; i< arr.length; i++){
Node head = null, prev = null;
for(int j = 0; j < arr[0].length; j++){
if (head == null) {
head = new Node(arr[i][j]);
prev = head;
} else {
prev.next = new Node(arr[i][j]);
prev = prev.next;
}
}
list.add(head);
}
}
static doubleNode CreateLinkedList(List list ){
doubleNode head = null ;
doubleNode tail = null;
for(int i =0 ; i < list.size(); i++){
Node nn = list.get(i);
while( nn != null){
doubleNode n = new doubleNode(nn.data);
if(head == null){
head = n;
tail = head;
head.doubleprev = null;
tail.doublenext = null;
}else{
tail.doublenext = n;
n.doubleprev = tail;
tail = n;
tail.doublenext = null;
}
nn = nn.next;
}
i++;
}
for(int i =1 ; i < list.size(); i++){
Node nn = list.get(i);
while( nn != null){
doubleNode n = new doubleNode(nn.data);
if(head == null){
head = n;
tail = head;
head.doubleprev = null;
tail.doublenext = null;
}else{
tail.doublenext = n;
n.doubleprev = tail;
tail = n;
tail.doublenext = null;
}
nn = nn.next;
}
i++;
}
return head;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] arr = new int[n][m];
// Input the 2D array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = scanner.nextInt();
}
}
// Create node ArrayList
List list = new ArrayList<>();
CreateNodeList(arr, list);
// Create linked list
doubleNode current = CreateLinkedList(list);
// Print linked list
while (current != null) {
System.out.print(current.doubledata + " <---> ");
current = current.doublenext;
}
System.out.print("null");
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogIAogICAgc3RhdGljIGNsYXNzIGRvdWJsZU5vZGUgewogICAgICAgIGludCBkb3VibGVkYXRhOwogICAgICAgIGRvdWJsZU5vZGUgZG91YmxlcHJldjsKICAgICAgICBkb3VibGVOb2RlIGRvdWJsZW5leHQ7CgogICAgICAgZG91YmxlTm9kZShpbnQgZG91YmxlZGF0YSkgewogICAgICAgICAgICB0aGlzLmRvdWJsZWRhdGEgPSBkb3VibGVkYXRhOwogICAgICAgIH0KICAgIH0gIAogIAogICAgc3RhdGljIGNsYXNzIE5vZGUgewogICAgICAgIGludCBkYXRhOwogICAgICAgIE5vZGUgbmV4dDsKCiAgICAgICAgTm9kZShpbnQgZGF0YSkgewogICAgICAgICAgICB0aGlzLmRhdGEgPSBkYXRhOwogICAgICAgICAgICBuZXh0ID0gbnVsbDsKICAgICAgICB9CiAgICB9CiAgICBzdGF0aWMgdm9pZCBDcmVhdGVOb2RlTGlzdChpbnQgW11bXSBhcnIgLCAgTGlzdDxOb2RlPiBsaXN0ICl7CiAgICAgIAogICAgIGZvcihpbnQgaSA9IDAgOyBpPCBhcnIubGVuZ3RoOyBpKyspewogICAgICAgICBOb2RlIGhlYWQgPSBudWxsLCBwcmV2ID0gbnVsbDsKICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBhcnJbMF0ubGVuZ3RoOyBqKyspewogICAgICAgICBpZiAoaGVhZCA9PSBudWxsKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGhlYWQgPSBuZXcgTm9kZShhcnJbaV1bal0pOwogICAgICAgICAgICAgICAgICAgICAgICBwcmV2ID0gaGVhZDsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBwcmV2Lm5leHQgPSBuZXcgTm9kZShhcnJbaV1bal0pOwogICAgICAgICAgICAgICAgICAgICAgICBwcmV2ID0gcHJldi5uZXh0OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgIH0KICAgICAgIGxpc3QuYWRkKGhlYWQpOwogICAgIH0KICAgIH0KICAgIAogICAgc3RhdGljIGRvdWJsZU5vZGUgQ3JlYXRlTGlua2VkTGlzdChMaXN0PE5vZGU+IGxpc3QgKXsKICAgICAgZG91YmxlTm9kZSBoZWFkID0gbnVsbCA7CiAgICAgIGRvdWJsZU5vZGUgdGFpbCA9IG51bGw7CiAgICAKICAgIGZvcihpbnQgaSA9MCA7IGkgPCBsaXN0LnNpemUoKTsgaSsrKXsKICAgICAgTm9kZSBubiA9IGxpc3QuZ2V0KGkpOwogICAgICB3aGlsZSggbm4gIT0gbnVsbCl7CiAgICAgICAgZG91YmxlTm9kZSBuID0gbmV3IGRvdWJsZU5vZGUobm4uZGF0YSk7CiAgICAgICAgICBpZihoZWFkID09IG51bGwpewogICAgICAgICAgICBoZWFkID0gbjsKICAgICAgICAgICAgdGFpbCA9IGhlYWQ7CiAgICAgICAgICAgIGhlYWQuZG91YmxlcHJldiA9IG51bGw7CiAgICAgICAgICAgIHRhaWwuZG91YmxlbmV4dCA9IG51bGw7CiAgICAgICAgICAgIAogICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIHRhaWwuZG91YmxlbmV4dCA9IG47CiAgICAgICAgICAgIG4uZG91YmxlcHJldiA9IHRhaWw7CiAgICAgICAgICAgIHRhaWwgPSBuOwogICAgICAgICAgICB0YWlsLmRvdWJsZW5leHQgPSBudWxsOwogICAgICAgICAgCiAgICAgICAgICB9CiAgICAgICAgICBubiA9IG5uLm5leHQ7CiAgICAgIH0KICAgICAgaSsrOwogICAgfQogICAgCiAgICAgZm9yKGludCBpID0xIDsgaSA8IGxpc3Quc2l6ZSgpOyBpKyspewogICAgICBOb2RlIG5uID0gbGlzdC5nZXQoaSk7CiAgICAgIAogICAgICAgd2hpbGUoIG5uICE9IG51bGwpewogICAgICAgICBkb3VibGVOb2RlIG4gPSBuZXcgZG91YmxlTm9kZShubi5kYXRhKTsKICAgICAgICAgIGlmKGhlYWQgPT0gbnVsbCl7CiAgICAgICAgICAgIGhlYWQgPSBuOwogICAgICAgICAgICB0YWlsID0gaGVhZDsKICAgICAgICAgICAgaGVhZC5kb3VibGVwcmV2ID0gbnVsbDsKICAgICAgICAgICAgdGFpbC5kb3VibGVuZXh0ID0gbnVsbDsKICAgICAgICAgICAgCiAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgdGFpbC5kb3VibGVuZXh0ID0gbjsKICAgICAgICAgICAgbi5kb3VibGVwcmV2ID0gdGFpbDsKICAgICAgICAgICAgdGFpbCA9IG47CiAgICAgICAgICAgIHRhaWwuZG91YmxlbmV4dCA9IG51bGw7CiAgICAgICAgICAKICAgICAgICAgIH0KICAgICAgICAgICBubiA9IG5uLm5leHQ7CiAgICAgIH0KICAgICAgaSsrOwogICAgfQogICAgICAgIAogICAgICAgIAogICAgICByZXR1cm4gaGVhZDsKICAgIH0KCiAgICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgaW50IG4gPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBpbnQgbSA9IHNjYW5uZXIubmV4dEludCgpOwogICAgICAgIGludFtdW10gYXJyID0gbmV3IGludFtuXVttXTsKCiAgICAgICAgLy8gSW5wdXQgdGhlIDJEIGFycmF5CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgICAgIGFycltpXVtqXSA9IHNjYW5uZXIubmV4dEludCgpOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvLyBDcmVhdGUgbm9kZSBBcnJheUxpc3QKICAgICAgICBMaXN0PE5vZGU+IGxpc3QgPSBuZXcgQXJyYXlMaXN0PD4oKTsKICAgICAgICBDcmVhdGVOb2RlTGlzdChhcnIsIGxpc3QpOwoKICAgICAgICAvLyBDcmVhdGUgbGlua2VkIGxpc3QKICAgICAgICBkb3VibGVOb2RlIGN1cnJlbnQgPSBDcmVhdGVMaW5rZWRMaXN0KGxpc3QpOwoKICAgICAgICAvLyBQcmludCBsaW5rZWQgbGlzdAogICAgICAgIHdoaWxlIChjdXJyZW50ICE9IG51bGwpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludChjdXJyZW50LmRvdWJsZWRhdGEgKyAiIDwtLS0+ICIpOwogICAgICAgICAgICBjdXJyZW50ID0gY3VycmVudC5kb3VibGVuZXh0OwogICAgICAgIH0KICAgICAgICBTeXN0ZW0ub3V0LnByaW50KCJudWxsIik7CiAgICB9Cn0K
Problem Statement 3
You're in a world where magical creatures engage in a peculiar contest, and you've been entrusted with keeping track of the matches.
The rules are as follows:
If there is an even number of creatures, they form pairs, and a total of n/2 matches are played. The victors of these matches advance to the next round. When there's an odd number of creatures, an intriguing twist unfolds.
One of them is granted a special power and advances directly to the next stage, while the remaining creatures pair off for matches. The number of matches in this scenario is (n - 1)/2. The creature with the special power joins the winners of these matches in the next round.
Your mission is to determine the overall number of matches that take place in this magical contest until a single champion emerges. How many matches will be required to decide the ultimate winner?
Input Format
An integer n represents the initial number of magical creatures in the contest.
Output Format
An integer represents the total number of matches played in the magical contest until a single champion is determined.
Constraints
1 <= n <= 200
Solution C++
#include
#include
#include
#include
#include
using namespace std;
int main() {
int n,ans=0;
cin>>n;
while(n>1)
{
if(n%2==0)
{
ans+=n/2;
n/=2;
}
else
{
ans+=(n-1)/2;
n=n/2+1;
}
}
cout<<ans<<endl;
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKaW50IG1haW4oKSB7CiAgICBpbnQgbixhbnM9MDsKICAgIGNpbj4+bjsKICAgIHdoaWxlKG4+MSkKICAgIHsKICAgICAgICBpZihuJTI9PTApCiAgICAgICAgewogICAgICAgICAgICBhbnMrPW4vMjsKICAgICAgICAgICAgbi89MjsKICAgICAgICAgICAgCiAgICAgICAgfQogICAgICAgIGVsc2UgCiAgICAgICAgewogICAgICAgICAgICBhbnMrPShuLTEpLzI7CiAgICAgICAgICAgIG49bi8yKzE7CiAgICAgICAgICAgIAogICAgICAgIH0KICAgIH0KICAgIGNvdXQ8PGFuczw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
while(n>1){
if(n%2==0){
res+=(n/2);
n = n/2;
}
else{
res += (n-1)/2;
n = (n-1)/2 + 1;
}
}
System.out.print(res);
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLm1hdGguKjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC4qOwoKY2xhc3MgU29sdXRpb24gewoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICAvKiBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VULiBZb3VyIGNsYXNzIHNob3VsZCBiZSBuYW1lZCBTb2x1dGlvbi4gKi8KICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBpbnQgbiA9IHNjLm5leHRJbnQoKTsKICAgICAgICBpbnQgcmVzID0gMDsKICAgICAgICB3aGlsZShuPjEpewogICAgICAgICAgICBpZihuJTI9PTApewogICAgICAgICAgICAgICAgcmVzKz0obi8yKTsKICAgICAgICAgICAgICAgIG4gPSBuLzI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIHJlcyArPSAobi0xKS8yOwogICAgICAgICAgICAgICAgbiA9IChuLTEpLzIgKyAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIFN5c3RlbS5vdXQucHJpbnQocmVzKTsKICAgIH0KfQo=
Solution Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
n=int(input())
c=0
while n!=1:
c+=n//2
if n%2==1:
n//=2
n+=1
else:
n//=2
print(c)
IyBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUDQpuPWludChpbnB1dCgpKQ0KYz0wDQp3aGlsZSBuIT0xOg0KICAgIGMrPW4vLzINCiAgICBpZiBuJTI9PTE6DQogICAgICAgIG4vLz0yDQogICAgICAgIG4rPTENCiAgICBlbHNlOg0KICAgICAgICBuLy89Mg0KcHJpbnQoYykNCg==
Problem Statement 4
You are a shopkeeper and bought N pairs of socks of several colours in bulk. The colour of each pair of socks is represented as a non-negative integer. The socks are sold as sets of 3 each. A set of socks consists of 3 socks of the same colour.
You want to find the number of different sets that can be made from the N pairs of socks you bought today.
Note: The order of indices of sock pairs in the set does not matter.
Input Format
The first line of the input contains a single integer N which denotes the number of pairs of socks that you have.
The second line of the input contains n space-separated integers a1, a2, …, an, where ai represents the colour of the ith pair.
Output Format
Print a single integer representing the total number of different sets of 3 socks that can be formed from the N pairs of socks.
Constraints
1 <= N <= 105
0 <= ai <= 10^3
Solution C++
#include <bits/stdc++.h>
using namespace std;
// Function to compute nC3
int combinations(int n) {
if (n < 3) {
return 0; // nC3 is 0 if n < 3
}
return (n * (n - 1) * (n - 2)) / 6;
}
int main() {
int n;
cin >> n;
unordered_map<int, int> m;
int maxe = 0;
for (int i = 0; i < n; i++) {
int ele;
cin >> ele;
m[ele]++;
maxe = max(maxe, m[ele]);
}
int cnt = 0;
for (auto itr : m) {
cnt += combinations(itr.second); // Calculate nC3 for each frequency
}
cout << cnt << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBGdW5jdGlvbiB0byBjb21wdXRlIG5DMwppbnQgY29tYmluYXRpb25zKGludCBuKSB7CiAgICBpZiAobiA8IDMpIHsKICAgICAgICByZXR1cm4gMDsgLy8gbkMzIGlzIDAgaWYgbiA8IDMKICAgIH0KICAgIHJldHVybiAobiAqIChuIC0gMSkgKiAobiAtIDIpKSAvIDY7Cn0KCmludCBtYWluKCkgewogICAgaW50IG47CiAgICBjaW4gPj4gbjsKCiAgICB1bm9yZGVyZWRfbWFwPGludCwgaW50PiBtOwogICAgaW50IG1heGUgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBpbnQgZWxlOwogICAgICAgIGNpbiA+PiBlbGU7CgogICAgICAgIG1bZWxlXSsrOwogICAgICAgIG1heGUgPSBtYXgobWF4ZSwgbVtlbGVdKTsKICAgIH0KCiAgICBpbnQgY250ID0gMDsKICAgIGZvciAoYXV0byBpdHIgOiBtKSB7CiAgICAgICAgY250ICs9IGNvbWJpbmF0aW9ucyhpdHIuc2Vjb25kKTsgLy8gQ2FsY3VsYXRlIG5DMyBmb3IgZWFjaCBmcmVxdWVuY3kKICAgIH0KCiAgICBjb3V0IDw8IGNudCA8PCBlbmRsOwoKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Main {
public static int combination(int n, int r) {
if (n < r) return 0;
int result = 1;
for (int i = 0; i < r; i++) {
result *= (n - i);
result /= (i + 1);
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
HashMap<Integer, Integer> socks = new HashMap<>();
for (int i = 0; i < n; i++) {
socks.put(arr[i], socks.getOrDefault(arr[i], 0) + 1);
}
int result = 0;
for (int count : socks.values()) {
result += combination(count, 3);
}
System.out.println(result);
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLm1hdGguKjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC4qOwoKY2xhc3MgTWFpbiB7CiAgICBwdWJsaWMgc3RhdGljIGludCBjb21iaW5hdGlvbihpbnQgbiwgaW50IHIpIHsKICAgICAgICBpZiAobiA8IHIpIHJldHVybiAwOwogICAgICAgIGludCByZXN1bHQgPSAxOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcjsgaSsrKSB7CiAgICAgICAgICAgIHJlc3VsdCAqPSAobiAtIGkpOwogICAgICAgICAgICByZXN1bHQgLz0gKGkgKyAxKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU2Nhbm5lciBzYyA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgaW50IG4gPSBzYy5uZXh0SW50KCk7CiAgICAgICAgaW50W10gYXJyID0gbmV3IGludFtuXTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIGFycltpXSA9IHNjLm5leHRJbnQoKTsKCiAgICAgICAgSGFzaE1hcDxJbnRlZ2VyLCBJbnRlZ2VyPiBzb2NrcyA9IG5ldyBIYXNoTWFwPD4oKTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgc29ja3MucHV0KGFycltpXSwgc29ja3MuZ2V0T3JEZWZhdWx0KGFycltpXSwgMCkgKyAxKTsKICAgICAgICB9CgogICAgICAgIGludCByZXN1bHQgPSAwOwoKICAgICAgICBmb3IgKGludCBjb3VudCA6IHNvY2tzLnZhbHVlcygpKSB7CiAgICAgICAgICAgIHJlc3VsdCArPSBjb21iaW5hdGlvbihjb3VudCwgMyk7CiAgICAgICAgfQoKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVzdWx0KTsKICAgIH0KfQo=
Solution Python
from collections import Counter
from math import comb
def count_sock_sets(n, sock_colors):
color_count = Counter(sock_colors)
total_sets = 0
for count in color_count.values():
if count >= 3:
total_sets += comb(count, 3)
return total_sets
n = int(input())
sock_colors = list(map(int, input().split()))
print(count_sock_sets(n, sock_colors))
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgQ291bnRlcg0KZnJvbSBtYXRoIGltcG9ydCBjb21iDQoNCmRlZiBjb3VudF9zb2NrX3NldHMobiwgc29ja19jb2xvcnMpOg0KICAgIGNvbG9yX2NvdW50ID0gQ291bnRlcihzb2NrX2NvbG9ycykNCiAgICB0b3RhbF9zZXRzID0gMA0KICAgIGZvciBjb3VudCBpbiBjb2xvcl9jb3VudC52YWx1ZXMoKToNCiAgICAgICAgaWYgY291bnQgPj0gMzoNCiAgICAgICAgICAgIHRvdGFsX3NldHMgKz0gY29tYihjb3VudCwgMykNCiAgICANCiAgICByZXR1cm4gdG90YWxfc2V0cw0KDQpuID0gaW50KGlucHV0KCkpIA0Kc29ja19jb2xvcnMgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpDQpwcmludChjb3VudF9zb2NrX3NldHMobiwgc29ja19jb2xvcnMpKQ0K
Problem Statement 5
We are given a string s consisting of only lowercase letters of English and a character ch.
Your task is to reverse a portion of a string from the last occurrence of a given character ch to the end of the string.
Input Format
First line contains a string s and character ch.
Output Format
Print the transformed string.
Constraints
1<=|s.length|<=10^5
Solution C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string strin;
char cha;
cin>>strin>>cha;
int x=strin.size()-1;
while(x>=0 and strin[x]!=cha)
x--;
if(x==-1)
{
cout<<strin<<endl;
return 0;
}
int y=strin.size()-1;
while(x<y)
{
swap(strin[x],strin[y]);
x++;
y--;
}
cout<<strin<<endl;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG1haW4oKSAKewogICAgc3RyaW5nIHN0cmluOwogICAgY2hhciBjaGE7CiAgICBjaW4+PnN0cmluPj5jaGE7CiAgICBpbnQgeD1zdHJpbi5zaXplKCktMTsKICAgIHdoaWxlKHg+PTAgYW5kIHN0cmluW3hdIT1jaGEpCiAgICB4LS07CiAgICBpZih4PT0tMSkKICAgIHsKICAgICAgICBjb3V0PDxzdHJpbjw8ZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGludCB5PXN0cmluLnNpemUoKS0xOwogICAgd2hpbGUoeDx5KQogICAgewogICAgICAgIHN3YXAoc3RyaW5beF0sc3RyaW5beV0pOwogICAgICAgIHgrKzsKICAgICAgICB5LS07CiAgICB9CiAgICBjb3V0PDxzdHJpbjw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
if(s2.equals(""))
{
System.out.println(s1);
}
else if((s1.lastIndexOf(s2.charAt(0)))==-1)
{
System.out.println(s1);
}
else
{
char c=s2.charAt(0);
int r=s1.lastIndexOf(c);
String p=s1.substring(r,s1.length());
StringBuilder b1=new StringBuilder(p);
b1.reverse();
p=b1.toString();
StringBuilder b2=new StringBuilder(s1);
b2.delete(r,s1.length());
s1=b2.toString();
s1=s1+p;
System.out.println(s1);
}
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLm1hdGguKjsKaW1wb3J0IGphdmEudXRpbC5yZWdleC4qOwoKcHVibGljIGNsYXNzIFNvbHV0aW9uIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7IAogICAgICAgIFNjYW5uZXIgc2M9bmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKICAgICAgICBTdHJpbmcgczE9c2MubmV4dCgpOwogICAgICAgIFN0cmluZyBzMj1zYy5uZXh0KCk7CiAgICAgICAgaWYoczIuZXF1YWxzKCIiKSkKICAgICAgICB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihzMSk7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoKHMxLmxhc3RJbmRleE9mKHMyLmNoYXJBdCgwKSkpPT0tMSkKICAgICAgICB7CiAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oczEpOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgIGNoYXIgYz1zMi5jaGFyQXQoMCk7CiAgICAgICAgaW50IHI9czEubGFzdEluZGV4T2YoYyk7CiAgICAgICAgU3RyaW5nIHA9czEuc3Vic3RyaW5nKHIsczEubGVuZ3RoKCkpOwogICAgICAgIFN0cmluZ0J1aWxkZXIgYjE9bmV3IFN0cmluZ0J1aWxkZXIocCk7CiAgICAgICAgYjEucmV2ZXJzZSgpOwogICAgICAgIAogICAgICAgIHA9YjEudG9TdHJpbmcoKTsKICAgICAgICBTdHJpbmdCdWlsZGVyIGIyPW5ldyBTdHJpbmdCdWlsZGVyKHMxKTsKICAgICAgICBiMi5kZWxldGUocixzMS5sZW5ndGgoKSk7CiAgICAgICAgczE9YjIudG9TdHJpbmcoKTsKICAgICAgICBzMT1zMStwOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihzMSk7CiAgICAgICAgfQogICAgICB9Cn0K
Solution Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=input()
A=a.split()
s=A[0]
ch=A[1]
srev=''
temp=''
final=''
for i in range(len(s)):
srev=srev+s[len(s)-i-1]
index = srev.find(ch)
if index==-1:
print(s)
else:
for j in range(index+1):
temp=temp+srev[index-j]
srev=temp+srev[index+1:]
for k in range(len(srev)):
final=final+srev[len(srev)-k-1]
print(final)
IyBFbnRlciB5b3VyIGNvZGUgaGVyZS4gUmVhZCBpbnB1dCBmcm9tIFNURElOLiBQcmludCBvdXRwdXQgdG8gU1RET1VUDQphPWlucHV0KCkNCkE9YS5zcGxpdCgpDQpzPUFbMF0NCmNoPUFbMV0NCnNyZXY9JycNCnRlbXA9JycNCmZpbmFsPScnDQpmb3IgaSBpbiByYW5nZShsZW4ocykpOg0KICBzcmV2PXNyZXYrc1tsZW4ocyktaS0xXQ0KaW5kZXggPSBzcmV2LmZpbmQoY2gpDQppZiBpbmRleD09LTE6DQogIHByaW50KHMpDQplbHNlOg0KICBmb3IgaiBpbiByYW5nZShpbmRleCsxKToNCiAgICB0ZW1wPXRlbXArc3JldltpbmRleC1qXQ0KICBzcmV2PXRlbXArc3JldltpbmRleCsxOl0NCiAgZm9yIGsgaW4gcmFuZ2UobGVuKHNyZXYpKToNCiAgICBmaW5hbD1maW5hbCtzcmV2W2xlbihzcmV2KS1rLTFdDQogIHByaW50KGZpbmFsKQ0K
Conclusion
TCS CodeVita is a wonderful opportunity for freshers to demonstrate their coding skills and secure a job at a prestigious company like Tata Consultancy Services. To stand out in the competition, it's essential to familiarise yourself with the question patterns, solve previous year's questions, and practice coding problems regularly.
By focusing on algorithms, problem-solving, and data structures, you can significantly enhance your performance. Good luck with your TCS CodeVita preparation!
Frequently Asked Questions (FAQs)
1. What is the TCS CodeVita question pattern?
The pattern includes algorithm-based challenges, mathematical problems, data structure questions, dynamic programming, and puzzle-based problems.
2. How do I prepare for TCS CodeVita as a fresher?
Practice coding regularly on available online platforms for coding. Focus on understanding algorithms, data structures, and problem-solving techniques.
3. Are there any TCS CodeVita questions available from previous years?
Yes, reviewing the previous year's questions is crucial for preparation. You can find sample questions with solutions to practice.
4. What programming languages are supported in TCS CodeVita?
TCS CodeVita supports languages like C, C++, Java, Python, and others.
5. What type of questions are asked in the TCS CodeVita interview?
The interview typically involves technical questions related to problem-solving, data structures, algorithms, and past project experiences, along with behavioural questions.
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:
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