0% found this document useful (0 votes)
40 views5 pages

DAA LAB 1 Ghanshyam

Uploaded by

Kamlesh Kumar
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)
40 views5 pages

DAA LAB 1 Ghanshyam

Uploaded by

Kamlesh Kumar
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/ 5

Dr.

B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY


JALANDHAR, PUNJAB

LAB FILE

Department of Computer Science & Engineering

Design & Analysis of Algorithms

Submitted To: Submitted By:


DR. AMRITPAL SINGH GHANSHYAM GUPTA

22103057
DEPARTMENT OF COMPUTER SECTION – A
SCIENCE AND ENGINEERING GROUP - G3
DSA
LAB-01
TASK – 01.1
Reverse a 1-D Array
Q1) Given a Binary 2-D Matrix in which 0 represents water and 1 represents land. An island is surrounded by water and is
formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the Matrix are all
surrounded by water. Print Number of islands.
Input

#include <iostream>
#include <vector>

void nofisland(std::vector<std::vector<int>>& island,std::vector<std::vector<int>>& visited,int i,int j)


{
if((i<island.size() && j<island.size()) && (i>=0 && j>=0) && (visited[i][j]!=1))
{
visited[i][j]=1;
if (island[i][j]==1)
{
nofisland(island,visited,i-1,j-1);
nofisland(island,visited,i-1,j);
nofisland(island,visited,i-1,j+1);
nofisland(island,visited,i,j-1);
nofisland(island,visited,i,j+1);
nofisland(island,visited,i+1,j-1);
nofisland(island,visited,i+1,j);
nofisland(island,visited,i+1,j+1);
}
}
}
int main()
{
std::vector<std::vector<int>> island;
island = {
{1,1,0,0,0},
{0,1,0,0,1},
{1,0,0,1,1},
{0,0,0,0,0},
{1,0,1,1,0}
};
int n=island.size();
std::vector<std::vector<int>> visited(n,std::vector<int>(n,0));
int count=0,i=0;
bool found=0;
for( i ; i< n; i++)
{
for( int j=0;j<n;j++)
{
if(island[i][j]==1 && visited[i][j]!=1)
{
found=1;
nofisland(island,visited,i,j);
}
if (found)
{
found=0;
count++;
}
}
}
std::cout<<count;
}

Output
TASK - 02
Maximum Subarray [Leetcode-Q53]
Q2) Given an integer array nums, find the Subarray with the largest sum, and return its sum.
Constraints:

 1 <= nums.length <= 105


4 4
 −10 <= nums[i] <= 10

Input
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int MAX=INT_MIN;
int sum=0;
for (int x: nums){
sum+=x;
if (sum>MAX)
MAX=sum;
if(sum<0)
sum=0;
}
return MAX;
}
};

Output

You might also like