0% found this document useful (0 votes)
12 views8 pages

21BCS22 - Mohsin-DAY 2

Uploaded by

bhat zain
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)
12 views8 pages

21BCS22 - Mohsin-DAY 2

Uploaded by

bhat zain
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/ 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

DOMAIN WINNING CAMP – IT SKILLS

Student Name: Mohsin Farooq UID: 21BCS11522


Branch: Computer Science & Engineering Section/Group: 612 (B)
Semester: 6th Date: 25/06/2024

DAY : 02

Problem 1: Dominant Element -


https://www.codechef.com/practice/course/arraysstrings-sorting/INTARR01/
problems/DOMINANT2

Program Code:
#include <bits/stdc++.h> using
namespace std;
int main() {
int t; cin>>t;
while(t--){ int n;
cin>>n; int a[n];
for(int i=1;i<=n;i++){
cin>>a[i];
}
vector <int> count(n+1,0); for(int
i=1;i<=n;i++){
count[a[i]]++;
}

sort(count.begin(),count.end());
if(count[n]!=count[n-1])
cout<<"yes"<<endl; else
cout<<"no"<<endl;
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 2: Chef and Dolls -


https://www.codechef.com/practice/course/arraysstrings-sorting/INTARR01/
problems/MISSP

Program Code:
#include <bits/stdc++.h> using
namespace std;

int main() {
// your code goes here
int t,n;cin>>t;
while(t--)
{ cin>>n;
int a,x=0;
while(n--){
cin>>a;
x^=a;
}
cout<<x<<endl;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 3: Distinct Colors -


https://www.codechef.com/practice/course/arraysstrings-sorting/INTARR01/
problems/DISTINCTCOL Program Code:
#include <bits/stdc++.h>
using namespace std; int
main() {
int t;
cin>>t;
while(t--) { int
n,c=INT_MIN;
cin>>n; int a[n];
for (int i = 0; i < n; i++) {
cin>>a[i];
if(c<a[i]) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:
c=a[i]; } }
cout<<c<<endl; }
return 0;
}

Problem 4: Find First and Last Position of Element in Sorted Array -


https://leetcode.com/problems/find-first-and-last-position-of-element-in-
sortedarray

Program Code:
class Solution { public: vector<int>
searchRange(vector<int>& nums, int target) {
int left = binarySearch(nums, target, true);
int right = binarySearch(nums, target, false);

if (left <= right) {


return {left, right};
} else {
return {-1, -1};
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:
private: int binarySearch(vector<int>& nums, int target, bool
findLeft) { int low = 0;
int high = nums.size() - 1;
int index = -1; while (low
<= high) {
int mid = low + (high - low) / 2;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (nums[mid] == target) {
index = mid;
if (findLeft)
{ high = mid -
1;
} else {
low = mid + 1;
}
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return index;
}
};

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 5: Maximum Subarray -


https://leetcode.com/problems/maximumsubarray
Program Code:
class Solution { public:
int maxSubArray(vector<int>& nums)
{ std::ios::sync_with_stdio(0);
std::cin.tie(0);

int maxi = INT_MIN;


int prefixSum = 0;

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


prefixSum += nums[i]; maxi =
max(maxi, prefixSum);
if(prefixSum < 0) prefixSum = 0;
}
return maxi;
}
};

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like