Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

Maximum Number of Aqua Curtains in a Box in C

Maximum Aqua Curtains in a Box

Maximum Number of Aqua Curtains in a Box

A furnishing company is manufacturing a new collection of curtains. The curtains are of two colors aqua(a) and black (b). The curtains color is represented as a string(str) consisting of a’s and b’s of length N.

Then, they are packed (substring) into L number of curtains in each box. The box with the maximum number of ‘aqua’ (a) color curtains is labeled. The task here is to find the number of ‘aqua’ color curtains in the labeled box.

Note :

If ‘L’ is not a multiple of N, the remaining number of curtains should be considered as a substring too.

Example 1:

Input :

bbbaaababa -> Value of str

3    -> Value of L

Output:

3   -> Maximum number of a’s
  

Explanation:

Dividing the string into sets of 3 characters each 

Set 1: {b,b,b}

Set 2: {a,a,a}

Set 3: {b,a,b}

Set 4: {a} -> leftover characters also as taken as another set

Among all the sets, Set 2 has more number of a’s.
The number of a’s in set 2 is 3.

Hence, the output is 3.
  

Example 2:

Input :

abbbaabbb -> Value of str

5   -> Value of L

Output:

2   -> Maximum number of a’s
  

Constraints:

1<=L<=10
1<=N<=50
  

Input Format:

First input - Accept string that contains character a and b only
Second input - Accept value for N (Positive integer)
  

#include <stdio.h>
#include <string.h>

// Function to check if a character is a vowel
int isVowel(char c)
{
    return (c == 'a' || c == 'e' || c == 'i' ||
            c == 'o' || c == 'u');
}

// LeetCode function
int maxVowels(char* s, int k)
{
    int count = 0, maxCount = 0;

    // Count vowels in the first window
    for (int i = 0; i < k && s[i] != '\0'; i++)
    {
        if (isVowel(s[i]))
            count++;
    }

    maxCount = count;

    // Sliding window
    for (int i = k; s[i] != '\0'; i++)
    {
        if (isVowel(s[i]))
            count++;

        if (isVowel(s[i - k]))
            count--;

        if (count > maxCount)
            maxCount = count;
    }

    return maxCount;
}

int main()
{
    char s[100];
    int k;

    printf("Enter the string: ");
    scanf("%s", s);

    printf("Enter the value of k: ");
    scanf("%d", &k);

    int result = maxVowels(s, k);

    printf("Maximum number of vowels in any substring of length %d is: %d\n", k, result);

    return 0;
}
  

๐Ÿ“Œ Hashtags:

#CProgramming #StringProblems #SlidingWindow #ProblemSolving #1printf

Comments

Popular Posts

๐ŸŒ™