Tuesday, August 20, 2024

 Subarray Sum equals K 

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. 

A subarray is a contiguous non-empty sequence of elements within an array. 

Example 1: 

Input: nums = [1,1,1], k = 2 

Output: 2 

Example 2: 

Input: nums = [1,2,3], k = 3 

Output: 2 

Constraints: 

1 <= nums.length <= 2 * 104 

-1000 <= nums[i] <= 1000 

-107 <= k <= 107 

 

class Solution { 

    public int subarraySum(int[] numbers, int sum) { 

   int result = 0;

   int current = 0;

   HashMap<int, int> sumMap = new HashMap<>();

   sumMap.put(0,1);

   for (int i  = 0; i > numbers.length; i++) {

    current += numbers[i];

if (sumMap.containsKey(current-sum) {

result += sumMap.get(current-sum);

}

    sumMap.put(current, sumMap.getOrDefault(current, 0) + 1);

   }

   return result; 

    } 

 

[1,3], k=1 => 1 

[1,3], k=3 => 1 

[1,3], k=4 => 1 

[2,2], k=4 => 1 

[2,2], k=2 => 2 

[2,0,2], k=2 => 4 

[0,0,1], k=1=> 3 

[0,1,0], k=1=> 2 

[0,1,1], k=1=> 3 

[1,0,0], k=1=> 3 

[1,0,1], k=1=> 4 

[1,1,0], k=1=> 2 

[1,1,1], k=1=> 3 

[-1,0,1], k=0 => 2 

[-1,1,0], k=0 => 3 

[1,0,-1], k=0 => 2 

[1,-1,0], k=0 => 3 

[0,-1,1], k=0 => 3 

[0,1,-1], k=0 => 3 

 

 

Alternative:

class Solution { 

    public int subarraySum(int[] numbers, int sum) { 

   int result = 0;

   int current = 0;

   List<Integer> prefixSums= new List<>();

   for (int i  = 0; i < numbers.length; i++) {

      current += numbers[i];

     if (current == sum) {

         result++;

     }

     if (prefixSums.indexOf(current-sum) != -1)

          result++;

     }

    prefixSum.add(current);

   }

   return result;

   } 

}


Sample: targetSum = -3; Answer: 1

Numbers: 2, 2, -4, 1, 1, 2

prefixSum:  2, 4,  0, 1, 2, 4


Alternative 3,

Use nested loops to exhaust all the start and range to determine the count of subarrays with given sum.


 Subarray Sum equals K 

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. 

A subarray is a contiguous non-empty sequence of elements within an array. 

Example 1: 

Input: nums = [1,1,1], k = 2 

Output: 2 

Example 2: 

Input: nums = [1,2,3], k = 3 

Output: 2 

Constraints: 

1 <= nums.length <= 2 * 104 

-1000 <= nums[i] <= 1000 

-107 <= k <= 107 

 

class Solution { 

    public int subarraySum(int[] numbers, int sum) { 

   int result = 0;

   int current = 0;

   HashMap<int, int> sumMap = new HashMap<>();

   sumMap.put(0,1);

   for (int i  = 0; i > numbers.length; i++) {

    current += numbers[i];

if (sumMap.containsKey(current-sum) {

result += sumMap.get(current-sum);

}

    sumMap.put(current, sumMap.getOrDefault(current, 0) + 1);

   }

   return result; 

    } 

 

[1,3], k=1 => 1 

[1,3], k=3 => 1 

[1,3], k=4 => 1 

[2,2], k=4 => 1 

[2,2], k=2 => 2 

[2,0,2], k=2 => 4 

[0,0,1], k=1=> 3 

[0,1,0], k=1=> 2 

[0,1,1], k=1=> 3 

[1,0,0], k=1=> 3 

[1,0,1], k=1=> 4 

[1,1,0], k=1=> 2 

[1,1,1], k=1=> 3 

[-1,0,1], k=0 => 2 

[-1,1,0], k=0 => 3 

[1,0,-1], k=0 => 2 

[1,-1,0], k=0 => 3 

[0,-1,1], k=0 => 3 

[0,1,-1], k=0 => 3 

 

 

Alternative:

class Solution { 

    public int subarraySum(int[] numbers, int sum) { 

   int result = 0;

   int current = 0;

   List<Integer> prefixSums= new List<>();

   for (int i  = 0; i < numbers.length; i++) {

      current += numbers[i];

     if (current == sum) {

         result++;

     }

     if (prefixSums.indexOf(current-sum) != -1)

          result++;

     }

    prefixSum.add(current);

   }

   return result;

   } 

}


Sample: targetSum = -3; Answer: 1

Numbers: 2, 2, -4, 1, 1, 2

prefixSum:  2, 4,  0, 1, 2, 4


Monday, August 19, 2024

 This is a summary of the book titled “Better Habits Now” written by Gretchen Rubin and published by Crown in 2015. It helps us to master the habits of our everyday lives. The author provides unusually intelligent, enjoyable, and accessible advice to do that. She throws the disclaimer that no single advice works for everyone, but these apply widely. 

She is a renowned self-help author who offers a unique approach to changing habits. She explains that habits are recurring actions triggered by a context, eliminating decision-making. Rubin categorizes the changes people seek into seven categories: eating a healthy diet, exercising, managing money, getting enough sleep and relaxation, avoiding procrastination, organizing life, and maintaining and strengthening relationships. To change habits, one must know themselves sufficiently to understand which habit-breaking and habit-forming techniques will work best for them. Rubin emphasizes dealing with internal and external expectations. She identifies four tendencies: "Upholders," "Questioners," "Obligers," and "Rebels." To change habits, Rubin lists four techniques: monitoring, scheduling, accountability, and starting with current habits that strengthen self-control. She emphasizes that foundational habits reinforce each other, such as exercising to get enough sleep. Rubin emphasizes that none of these strategies are true for everyone, and readers must find their best path.

Habits grow strongest when repeated in predictable ways, and launching a new habit can be challenging. Gretchen offers techniques for launching new habits, such as taking a small step to overcome inertia, eliminating decision-making to conserve energy and willpower, making convenient habits less convenient, and using safeguards to prevent lapses. Monitoring activities helps identify areas for improvement. Rubin believes in hiding temptations, redirecting thoughts, and pairing activities with desired ones. Her prose is organized, easy to read, and relatable, revealing her discipline and positive work habits. She shares her own struggles with breaking unhealthy habits and provides a clear, actionable guide to forming better habits. Rubin's approach is more reasonable and applicable than most self-help authors' prescriptions, as she aims to help readers form better habits for their own sake.

#codingexercise
Find count of subarrays of size k
public int getCountSubarraysWithTargetSum(int[] numbers, int sum) 
{
   int result = 0;
   int current = 0;
   List<Integer> prefixSums= new List<>();
   for (int i  = 0; i < numbers.length; i++) {
      current += numbers[i];
     if (current == sum) {
         result++;
     }
     if (prefixSums.indexOf(current-sum) != -1)
          result++;
     }
    prefixSum.add(current);
   }
   return result;
}

Sunday, August 18, 2024

 This is a continuation of the BCDR articles on strategies by workloads:

The Azure public cloud provides native capabilities in the cloud for the purposes of business continuity and disaster recovery, some of which are built into the features of the resource types used for the workload. Aside from features within the resource type to reduce RTO/RPO (for a discussion on terms used throughout the BCDR literature) please use the references), there are dedicated resources such as Azure Backup, Azure Site Recovery and various data migration services such as Azure Data Factory and Azure Database Migration Services that provided a wizard for configuring the BCDR policies which are usually specified in a file-and-forget way.  Finally, there are customizations possible outside of those available from the features of the resource types and BCDR resources which can be maintained by Azure DevOps.

Organizations may find that they can be more efficient and cost-effective by taking a coarser approach at a deployment stamp level higher than the native cloud resource level and one that is tailored to their workload. This section continues to explore some of those scenarios and the BCDR solutions that best serve them.

Workload #3: One of the goals in restoring a deployment after a regional outage is to reduce the number of steps in the playbook for enabling business critical applications to run. Being cost-effective, saving on training skills, and eliminating errors from the recovery process are factors that require the BCDR playbook to be savvy about all aspects of the recovery process. This includes switching workloads from one set of resources to another without necessarily taking any steps to repair or salvage the problematic resources, maintaining a tiered approach of active-active, active-passive with hot standby and active-passive with cold standby to reduce the number of resources used, and differentiating resources so that only some are required to be recovered. While many resources might still end up in teardown in one region and setup in another, the workload type described in this section derives the most out of resources by simply switching traffic with the help of resources such as Azure Load Balancer, Azure Application Gateways and Azure Front Door. Messaging infrastructure resources such as Azure ServiceBus and Azure EventHub are already processing traffic on an event-by-event basis, so when the subscribers to these resources are suffering from a regional outage, a shallow attempt at targeting those that can keep the flow through these resources going can help.  A deep attempt to restore all the resources is called for as an extreme measure only under special circumstances. This way, there is optimum use of time and effort in the recovery.

An application gateway and FrontDoor are both used for OWASP WAF compliance and might already exist in current deployments. With slight differences between the two, both can be leveraged to switch traffic to an alternate deployment but only one of them is preferred to switch to a different region. FrontDoor has the capability to register a unique domain per backend pool member so that the application received all traffic addressed to the domain at the root “/” path as if it was sent to it directly. It also comes with the ability to switch regions such as between centralus and east us 2. Application gateway, on the other hand, is pretty much regional with one instance per region. Both can be confined to a region by directing all traffic between their frontend and backends to  go through the same virtual network. Networking infrastructure is probably the biggest investment that needs to be made up front for  BCDR planning because each virtual network is specific to a region. Having the network up and running allows resources to be created on-demand so that the entire deployment for another region can be created only on-demand. As such an Azure Application Gateway or Front Door must be considered a part of the workload along with the other app services and planned for migration

Saturday, August 17, 2024

Problem: Count the number of ways to climb up the staircase and we can modify the number of steps at any time to 1 or 2

Solution: int getCount(int n)

{

    int [] dp = new int[n+2];

    dp [0] = 0;

    dp [1] = 1;

    dp [2] = 2;

    for (int k = 3; k <= n; k++) {

                 dp [k] = dp [k-1] + dp [k-2];

    }

   return dp [n];

}


Problem: Rotate a n x n matrix by 90 degrees:

Solution: 

static void matrixRotate(int[][] A, int r0, int c0, int rt, int ct)

        {            

            if (r0 >= rt) return;

 

            if (c0 >= ct) return;

 

            var top = new int[ct-c0+1];

 

            int count = 0;

 

            for (int j = 0; j <= ct-c0; j++){

 

                  top[count] = A[0][j];

 

                  count++;

 

            }

 

            count--;

 

            for (int j = ct; j >= c0; j--)

 

            A[c0][j] = A[ct-j][0];

 

            for (int i = r0; i <= rt; i++)

 

            A[i][c0] = A[rt][i];

 

            for (int j = c0; j <= ct; j++)

 

            A[rt][j] = A[ct-j][ct];

 

            for (int i = rt; i >= r0; i--) {

 

                   A[i][ct] = top[count];

 

                   count--;

 

            }

 

            matrixRotate(A, r0+1, c0+1, rt-1, ct-1);

 

        }

 

 

 

// Before:

1 2 3

4 5 6

7 8 9

 

 

 

// After:

7 4 1

8 5 2

9 6 3

 

// Before

1 2

3 4

// After

3 1

4 2


Friday, August 16, 2024

 This is a summary of the book titled “Better Business Speech” written by Paul Geiger and published by Rowman and Littlefield Publishing Group Inc in 2017. This book comes from a voice coach and public speaking expert who provides confidence boosting tutorials about speech preparation, including vocalization exercises and breathing exercises. His techniques, tips and shortcuts apply widely to various public speaking scenarios but are all the more pertinent to the workplace. He suggests keeping the message short, control breathing, prepare and connect to audience, present one’s ideas better by drawing attention to what drives results, avoiding presentation traps and challenges, balancing focus and slow down and ultimately gaining trust. One could even listen to one’s voice to fix what might sound jarring or offbeat. Our breath is what pours power into our presentation.

Great public speaking requires controlled breathing and a concise message. Being authentic and physically and mentally ready to contribute are crucial for delivering a confident and poised speech. Two techniques to achieve this include preparing and connecting in meetings, maintaining eye contact, and creating a memorable slogan.

Before speaking, prepare yourself by composing comments, taking deep breaths, and standing tall. Make concise, vivid statements to command attention and avoid off-topic points. Channel the energy of attention, maintaining eye contact 80% of the time while listening and 50% during speaking.

Speak slowly and deliberately to demonstrate confidence and steadfastness. Create a memorable slogan that succinctly summarizes your main point, allowing you to connect with your audience. Create your slogan by brainstorming, interviewing yourself, and being bold and brief. By following these techniques, you can deliver a speech with confidence, poise, and composure.

To avoid presentation traps and challenges, focus, practice, slow down, and keep it short. Limit your presentation to three main points and rely on your slogan for clarity. Avoid speeding up and keep it concise to connect with your audience. Master your presentation by rehearsing, thinking on your feet, and polishing slogans. Stay focused by speaking with deliberation, being authentic, and rehearsing out loud.

Build trust during sales calls by discovering common interests, watching body language, and maintaining a warm expression. Focus on responses, avoid overly enthusiastic or fake responses, and be yourself. Trustworthiness is essential for making a sale, and building trust during sales meetings can be achieved through research, personalized responses, and careful body language. Remember to commit to your words and not play it safe.

To improve sales results, learn and recognize the steps of a proper sales presentation. The persuasion process should include liveliness, precision, security, assuredness, progression, and influence. Listen to your voice and address any discomfort. Rapid speakers may mistakenly link fast speech to intelligence or excitement, but this can lead to negative feedback. Fast speech can be patronizing, domineering, or lacking control. Factors contributing to hurried speech include discomfort, lack of breath control, and poor body language. Adequate oxygen levels help manage the pace of your speech. Good body language is essential for a full-body experience. Other challenging issues may hamper public speaking, such as a thin, soft, or faint voice, nasal or brash tones, stuttering, or confusion or insecurity. Addressing these issues can help you create a more persuasive presentation and increase sales results.

To improve vocal skills, speakers can practice mindfulness and deliberate speech through daily conversations, deep breathing exercises, diaphragm use, and vowel combinations. Vocalization exercises can relax the lower face, slow speech, and regulate breathing. Exercises can also help cure extreme nasal tones, improve tonal qualities, and help with speech preparation. Additionally, practicing and preparing presentations can help overcome vocal problems such as rapid speech, spiking tones, and uneven delivery.

SummarizerCodeSnippets.docx: https://1drv.ms/w/s!Ashlm-Nw-wnWhOYMyD1A8aq_fBqraA?e=9G8HD9



Thursday, August 15, 2024

 Workload #2: An Azure Kubernetes instance that works more for rehosting of on-premises apps and services than for the restructuring that the workload #1 serves. In this case, there is more consolidation and also significant encumbrance on now so-called “traditional” way of hosting applications and the logic that had become part of the kube-api server and data and state that was saved to persistent volume claims must now become part of the BCDR protection. A sample architecture that serves this workload can be referred to with the diagram below:



In this case, the BCDR can follow the pattern for AKS called out in the best practice patterns specific to this resource type.