0% found this document useful (0 votes)
3 views3 pages

Problem 3: Dutch National Flag

The document contains code solutions for two problems: sorting an array of 0s, 1s, and 2s using the Dutch National Flag algorithm, and rotating an array to change values opposite to the traversal of the loop. Additionally, it includes a function to find the maximum sum of contiguous subarrays using Kadane's algorithm. The code is structured with a main function to handle multiple test cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Problem 3: Dutch National Flag

The document contains code solutions for two problems: sorting an array of 0s, 1s, and 2s using the Dutch National Flag algorithm, and rotating an array to change values opposite to the traversal of the loop. Additionally, it includes a function to find the maximum sum of contiguous subarrays using Kadane's algorithm. The code is structured with a main function to handle multiple test cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem 3: Dutch national Flag(https://www.youtube.com/watch?

v=tp8JIuCXBaU)

//{ Driver Code Starts

#include<bits/stdc++.h>

using namespace std;

// } Driver Code Ends

class Solution

public:

void sort012(int a[], int n)

int mid=0;int low=0;int high=n-1;

while(mid<=high){

if(a[mid]==0)

swap(a[mid],a[low]);

low++; mid++;

else if(a[mid]==1)

mid++;

else

swap(a[mid],a[high]);

high--;

};

//{ Driver Code Starts.


int main() {

int t;

cin >> t;

while(t--){

int n;

cin >>n;

int a[n];

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

cin >> a[i];

Solution ob;

ob.sort012(a, n);

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

cout << a[i] << " ";

cout << endl;

return 0;

// } Driver Code Ends

Problem 6:change the values opposite to the traversal of the loop

void rotate(int arr[], int n)

int temp=arr[n-1];

for(int i=n-2;i>=0;i--){

arr[i+1]=arr[i];

arr[0]=temp;
}

KADAENE ALGO

class Solution{

public:

// arr: input array

// n: size of array

//Function to find the sum of contiguous subarray with maximum sum.

long long maxSubarraySum(int arr[], int n){

long long maxi=LONG_MIN;

long long sum=0;

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

sum+=arr[i];

if(sum>maxi)

maxi=sum;

if(sum<0)

sum=0;

return maxi;

};

You might also like