prefix adding solution. Higher altitude solution

// calculate the total of the sum of prefix

#include <iostream>

using namespace std;


int main() {

    int arr[5] = {1, 2, 3, 4, 5}; // Example array

    int prefix_sum = 0; // Array to store prefix sums


    for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {

        int sum = 0;

        for(int j = 0; j <= i; j++) {

            sum += arr[j];

        }

        prefix_sum = sum;

    }


    // Optional: Print the prefix sums to verify the result

    

        cout << prefix_sum;


    return 0;

}

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! OR !!!!!!!!!!!!!!!!!!!!

//

#include <iostream>


int main() {

    int arr[5]= {1,2,3,4,5};

    int storeSum = 0;

    for(int i =0;i<sizeof(arr)/sizeof(arr[0]); i++){

        storeSum += arr[i];

    }

    // Write C++ code here

   std::cout << storeSum <<"" ;


    return 0;

}

++++++++++++++++

 

//calculate the highest altuited of the bicker achived

#include <iostream>

int main() {
    int n = 6;
    int arr [n] = {0,-5,1,5,0,-7};
    int original[n]={0};
    int higher=0;
    int arr1=0;
    
    for(int i=0; i<n; i++ ){
         arr1+=arr[i];
        original[i]= arr1;
    }
    
    for(int j=0; j<n; j++){
        if(higher <= original[j]){
            higher= original [j];
        } else{
            int lower=0;
            lower= original[j];
        }
    }
    
    // Write C++ code here
    std::cout << higher<< "";

    return 0;
}

::::::::::::::::Optimize solution of this code:::::::::::::

#include <iostream>

int main() {
    int n = 6;
    int arr[n] = {0, -5, 1, 5, 0, -7};

    int higher = arr[0];
    int sum = arr[0];

    for (int i = 1; i < n; ++i) {
        sum += arr[i];
        higher = std::max(higher, sum);
    }

    std::cout << higher << std::endl;

    return 0;
}

=========================================
calculate the frequency of a string .

 

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "hello"; // The input string
    int store[26] = {0}; // Array to store frequency of each letter, initialized to zero

    // Calculate frequency of each character using a traditional for loop
    for (int i = 0; i < str.length(); i++) {
        char c = str[i]; // Access the character at index i
        if (c >= 'a' && c <= 'z') {
            store[c - 'a']++; // Increment the count for this character
        }
    }

    // Display the frequency of each character
    cout << "Character Frequencies:\n";
    for (int j = 0; j < 26; j++) {
        if (store[j] > 0) { // Only display characters that appear in the string
            cout << char(j + 'a') << ": " << store[j] << endl;
        }
    }

    return 0;
}

----------------------------------------------------------OR------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "hello"; // The input string
    int store[26] = {0}; // Array to store frequency of each letter, initialized to zero

    // Calculate frequency of each character using a traditional for loop
    for (int i = 0; i < str.length(); i++) {
        char c = str[i]; // Access the character at index i
        if (c >= 'a' && c <= 'z') {
            store[c - 'a']++; // Increment the count for this caharacter
        }
    }

    // Display the frequency of each character
    cout << "Character Frequencies:\n";
    for (int j = 0; j < sizeof(store)/sizeof(store[0]); j++) {
        if (store[j] > 0) { // Only display characters that appear in the string
            cout << char(j + 'a') << ": " << store[j] << endl;
        }
    }

    return 0;
}



Comments