Task Intermediate
Task Intermediate
// Function to count the number of unordered pairs of elements in arr such that
their bitwise AND is a power of 2
#include <stdio.h>
// Function to count the number of unordered pairs of elements in arr such that
their bitwise AND is a power of 2
int bitwiseAnd(int arr_count,int* arr)
{
int no_bwand_pwr2=0;
int i=0,j=0;
for(i=0;i<arr_count;i++)
{
for(j=0;j<arr_count-i-1;j++)
{
int bwand_value = arr[i]&arr[j+i+1];
if(bwand_value >0)
{
if((bwand_value &(bwand_value-1))==0)
{
no_bwand_pwr2+=1;
}
}
}
}
return no_bwand_pwr2;
}
int main() {
// Sample Case 1
int arr1[] = {10,7,2,8,3};
int arr_count1 = 5;
printf("Sample Output 1: %ld\n", bitwiseAnd(arr_count1, arr1));
return 0;
}
/
***********************************************************************************
*********************/
//2-equalizing array
#include <stdio.h>
#include <stdlib.h> // For qsort function
#include <limits.h> // For INT_MAX constant
for(int i=0;i<arr_count;i++)
{
int steps = 0;
while(1)
{
int element_value = arr[i];
dp[element_value][0]+=1;
dp[element_value][1]+=steps;
int main() {
// Sample Input 1
int arr1[] = {64, 30, 25, 33};
int arr_count1 = 4;
int threshold1 = 2;
int d1 = 2;
printf("Sample Output 1: %d\n", mino(arr_count1, arr1, threshold1, d1));
// Sample Input 2
int arr2[] = {1, 2, 3, 4 ,5};
int arr_count2 = 5;
int threshold2 = 3;
int d2 = 2;
printf("Sample Output 2: %d\n", mino(arr_count2, arr2, threshold2, d2));
return 0;
}
/
***********************************************************************************
*********************/
//3- file renaming
#include <stdio.h>
#include <string.h>
// Function to calculate the number of ways to create a new file name by removing
characters
int renameFile(char* newName, char* oldName) {
long new_length = strlen(newName);
long old_length = strlen(oldName);
// Update the temporary array with the values from the occurrence array
for (long z = 0; z < old_length + 1; z++) {
temp_arr[z] = occ_arr[z];
}
}
int main() {
// Test Case
char newName[] = "aba";
char oldName[] = "ababa";
return 0;
}
/
***********************************************************************************
*********************/
//4-hotel construction
#include <stdio.h>
// Function to calculate the number of ways to build exactly 3 hotels with equal
distances
int numberOfWays(int roads_rows, int roads_columns, int** roads) {
int citiesCnt = roads_rows + 1;
int distance[MAX_CITIES_CNT][MAX_CITIES_CNT];
// Initialize distances
for (int src = 0; src < citiesCnt; src++) {
// Distance from src city to itself = 0
initDistance(distance, src, src, 0, citiesCnt, isThereRoad);
}
// Count the number of ways to build exactly 3 hotels with equal distances
int cnt = 0;
return cnt;
}
int main() {
// Test Case
int roads_rows = 4;
int roads_columns = 2;
int roads_arr[][2] = {{1, 2}, {1, 3}, {1, 4}, {1, 5}};
int* roads[roads_rows];
for (int i = 0; i < roads_rows; i++) {
roads[i] = roads_arr[i];
}
return 0;
}
/
***********************************************************************************
*********************/
//5-largest area
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
*result_count = isVertical_count;
// Boundry counters
int horizontalCutsSize = 0;
int verticalCutsSize = 0;
int main() {
// Test Case
int w = 4;
int h = 4;
int isVertical_count = 2;
bool isVertical[] = {0, 1};
int distance_count = 2;
int distance[] = {3, 1};
int result_count;
return 0;
}
/
***********************************************************************************
*********************/
//6-maximizing element
#include <stdio.h>
// Initialization
int right = k; // Right pointer starting at index k
int left = k; // Left pointer starting at index k
int count = 1; // Counter for the number of steps taken
int limitRight = n - 1; // Maximum valid index in the array
// Explore elements on both sides until the sum exceeds maxSum or no more
elements to explore
while (n <= maxSum && (left > 0 || right < limitRight)) {
// Increment n by the difference between right and left plus 1
n = n + (right - left + 1);
int main() {
// Sample Input 1
int n1 = 3, maxSum1 = 7, k1 = 1;
printf("Sample Output 1: %d\n", maxElement(n1, maxSum1, k1));
// Sample Input 2
int n2 = 4, maxSum2 = 6, k2 = 2;
printf("Sample Output 2: %d\n", maxElement(n2, maxSum2, k2));
return 0;
}
/
***********************************************************************************
*********************/
// Function declaration
long maxSubarrayValue(int n, int* arr);
int main() {
// Test Case 1
int arr1[] = {-1, -4, 2};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
long result1 = maxSubarrayValue(n1, arr1);
printf("Test Case 1: %ld\n", result1);
// Test Case 2
int arr2[] = {2, -1, -4, 5};
int n2 = sizeof(arr2) / sizeof(arr2[0]);
long result2 = maxSubarrayValue(n2, arr2);
printf("Test Case 2: %ld\n", result2);
return 0;
}
/
***********************************************************************************
*********************/
//8-nice teams
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int l = 0;
int r = skillLevel_count / 2 + 1;
if (good)
l = m;
else
r = m;
}
int main() {
// Test Case
int skillLevel[] = {1, 2, 3, 4, 5, 6};
int skillLevel_count = sizeof(skillLevel) / sizeof(skillLevel[0]);
int minDiff = 4;
return 0;
}
/
***********************************************************************************
*********************/
//9. Sorted Sums
#include <stdio.h>
// Calculate the sorted sum for the current state of the array
for (int k = 0; k <= i; k++) {
ans = (ans + a[k] * 1LL * (k + 1)) % M; // Multiply with position (k +
1) and accumulate
}
ans %= M; // Take modulo to prevent overflow
}
return ans;
}
int main() {
// Sample Input 1
int a1[] = {3, 9, 5, 8};
int a_count1 = 4;
printf("Sample Output 1: %d\n", sortedSum(a_count1, a1));
// Sample Input 2
int a2[] = {5, 9};
int a_count2 = 2;
printf("Sample Output 2: %d\n", sortedSum(a_count2, a2));
return 0;
}
/
***********************************************************************************
*********************/
//10-nice teams
#include <stdio.h>
// Function to determine the maximum number of pairs that can be made from the
given supply of weights
int taskOfPairing(int freq_size, int* freq) {
int pairsCnt = 0; // Counter to track the number of pairs formed
int prevRemainder = 0; // Variable to store the remainder from the previous
iteration
// Update the pairs count based on the number of pairs formed with the
current frequency
pairsCnt += freq[i] / 2;
int main() {
// Test Case
int freq_size = 4; // Number of different weights
int freq[] = {2, 4, 3, 1}; // Frequency array representing the number of
dumbbells for each weight
return 0;
}
/
***********************************************************************************
*********************/
//11-user friendly password system
#include <stdio.h>
#include <stdlib.h>
#define P (131)
#define MOD (1000000007)
return res;
}
correctHash *= P;
correctHash %= MOD;
case 'a':
// If the event is to authorize, perform authorization and store
the result
res[resIdx] = authorize(currHash, atoi(events[i][1]));
resIdx++;
break;
}
}
int main() {
// Test Case
char ***events;
int result_count;
events[0] = setPasswordEvent;
events[1] = authorizeEvent1;
events[2] = authorizeEvent2;
events[3] = authorizeEvent3;
return 0;
}
/
***********************************************************************************
*********************/