Wednesday, January 3, 2024

 

Partition to K Equal Sum Subsets


Given an integer array 
nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.

 

Example 1:

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

Output: true

Explanation: It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.

Example 2:

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

Output: false

 

Constraints:

  • 1 <= k <= nums.length <= 16
  • 1 <= nums[i] <= 104
  • The frequency of each element is in the range [1, 4].

 

class Solution {

    public boolean canPartitionKSubsets(int[] nums, int k) {

        int sum = 0;

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

            sum += nums[i];

        }

 

        for (int i = 1; i <= sum/k; i++) {

            List<List<Integer>> subsets = new ArrayList<>();

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

                subsets.add(new ArrayList<Integer>());

            }

            Arrays.sort(nums);

            if (insertRecursively(subsets, nums, i, 0))

                return true;

            }

        }

        return false;

    }

 

    public boolean insertRecursively(List<List<Integer>> subsets, int[] nums, int sum, int index) {

        if (index == nums.length &&

            valid(subsets, sum)) {

                return true;

            }

        }

        for (int i = 0; i < subsets.size(); i++){

            int subsetSum = 0;

            for (int j = 0; j < subsets.get(i).size(); j++){

                subsetSum += subsets.get(i).get(j);

            }

            if (subsetSum + nums[index] <= sum){

                subsets.get(i).add(nums[index]);

                if (insertRecursively(subsets, nums, sum, index+1)) {

                    return true;

                }

                subset.get(i).remove(subset.get(i).size()-1);

            }

        }

        return false;

    }

 

    public boolean valid(List<List<Integer>> subsets, int sum){

        for (int i = 0; i < subsets.size(); i++){

            int subsetSum = 0;

            for (int j = 0; j < subsets.get(i).size(); j++){

                subsetSum += subsets.get(i).get(j);

            }

            if (subsetSum != sum){

                return false;

            }

        }

        return true;

    }

}

 

Tuesday, January 2, 2024

 

Use of Front Door for web applications in Azure Public Cloud.

Azure Front Door is typically used to ensure that users can access web applications in the event of a regional outage, balance requests between instances and to support rate limiting. It works well with CDN. Azure Front Door focuses on global load balancing and site-acceleration and Azure CDN offers static content caching and acceleration. By bringing security with threat protection and advanced OWASP capabilities, Azure Front Door makes CDN a remarkable cloud-native solution. 

By itself, Azure Front Door enables us to define, manage, and monitor global routing for our web traffic by optimizing the best performance and instant global failover for high availability. With Front Door, we can transform our global (multi-region) consumer and enterprise applications into robust, high-performance, and personalized modern applications, APIs, and content that reaches a global audience with Azure. Azure Front Door works at Layer 7 of HTTP/HTTPS layer and uses anycast protocol with split TCP and Microsoft’s global network for improving global connectivity. 

Front Door can be used to guarantee business continuity and disaster recovery. This provides robustness to web applications and function applications such that the state can be recovered after a user or application error, regional data center outage, or unplanned disruptions. By its nature of being purely logic, application and app services have varying options to target recovery of different scopes and levels, some of which are compared below:  

 

·                 Concepts to Understand 

Primary region and secondary region: Two regions are used to achieve higher availability and the application is deployed to each region. The designated primary receives traffic normally and the secondary receives traffic on failover.  

 

Front Door can be configured for priority routing which sends traffic to primary region until it becomes unavailable and routes traffic to secondary instead. Front Door has both routing configuration and health probes to monitor the health of each backend. Ideally, health probes should check for critical dependencies such as apps, queues, and databases.  

 

Geo-replication: is configured for storage accounts, SQL databases and Cosmos DB.  

 

 

·                 Configuration patterns

 

There are several general approaches to achieve high availability across regions which include:  

·                 Active/Passive with hot standby: Traffic goes to one region, while the other is running and ready to accept connections. The other is usually allocated in a different region and is always running.  

·                 Active/Passive with cold standby: Traffic goes to one region while the other waits on cold standby. The secondary region isn’t allocated until needed for a failover. This is less cost but takes longer.  

·                 Active/active – both regions are active, and load balanced equally. If one of the regions becomes unavailable, it is taken out of rotation.  

Monday, January 1, 2024

 

Automated Cloud IaC using Copilot:

A Copilot is an AI companion that can communicate with a user over a prompt and a response. It can be used for various services such as Azure and Security, and it respects subscription filters. Copilots help users figure out workflows, queries, code and even the links to documentation. They can even obey commands such as changing the theme to light or dark mode. Copilots are well-integrated with many connectors and types of data sources supported. They implement different Natural Language Processing models and are available in various flagship products such as Microsoft 365 and GitHub. They can help create emails, code and collaboration artifacts faster and better.   

 

This article delves into the creation of a copilot to suggest IaC code relevant to a query. It follows the same precedence as a GitHub Copilot that helps developers write code in programming languages. It is powered by the OpenAI Codex model, which is a modified production version of the Generative Pre-trained Transformer-3 aka (GPT-3). The GPT-3 AI model created by OpenAI features 175 billion parameters for language processing. This is a collaboration effort between OpenAI, Microsoft and GitHub.   

 

A copilot can be developed with no code using Azure OpenAI studio. We just need to instantiate a studio, associate a model, add the data sources, and allow the model to train. The models differ in syntactic or semantic search.  The latter uses a concept called embedding that discovers the latent meaning behind the occurrences of tokens in the given data. So it is more inclusive than the former.  A search for time will specifically search for that keyword with the GPT-3 but a search for clock will include the references to time with a model that leverages embeddings.  Either way, a search service is required to create an index over the dataset because it facilitates fast retrieval. A database such as Azure Cosmos DB can be used to assist with vector search.  

 

At present, all these resources are created in a cloud, but their functionality can also be recreated on a local Windows machine with the upcoming release of the Windows AI Studio. This helps to train the model on documents that are available only locally. Usually, the time to set up the resources is only a couple of minutes but the time to train the model on all the data is the bulk of the duration after which the model can start making responses to the queries posed by the user. The time for the model to respond once it is trained is usually in the order of a couple of seconds.  A cloud storage account has the luxury to retain documents indefinitely and with no limit to size but the training of a model on the corresponding data accrues cost and increases with the size of the data ingested to form an index.  

 

References to build the first co-pilot:  

1.      https://github.com/raja0034/azureml-examples 

2.      https://github.com/raja0034/openaidemo/blob/main/copilot.py 

References: previous articles on IaC

 

Sample test run: