This repository was archived by the owner on Sep 7, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +83
-0
lines changed Expand file tree Collapse file tree 2 files changed +83
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Given an integer array of size 2N + 1. In this given array,
3+ N numbers are present twice and one number is present only once in the array.
4+ You need to find and return that number which is unique in the array.
5+ */
6+
7+ #include<iostream>
8+ #include <algorithm>
9+ #include "solution.h"
10+ using namespace std;
11+
12+ int FindUnique(int arr[], int size){
13+ int xor1 = 0;
14+ for(int i = 0; i < size; i++)
15+ xor1 ^= arr[i];
16+ return xor1;
17+ }
18+
19+ int main() {
20+
21+ int size;
22+
23+ cin>>size;
24+ int *input=new int[1+size];
25+
26+ for(int i=0;i<size;i++)
27+ cin>>input[i];
28+
29+ cout<<FindUnique(input,size)<<endl;
30+
31+ return 0;
32+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Given an array consisting of positive and negative integers, find the length of the longest continuous subset whose sum is zero.
3+ To be done in O(n) time complexity.
4+
5+ Eg: Array size: 10
6+ Array: 95 -97 -387 -435 -5 -70 897 127 23 284
7+ Then the length longest continious subset/sequence which sums up to zero is 5
8+ Since the longest continious subset which sums up to zero is -387 -435 -5 -70 897.
9+
10+ This program makes use of hashmaps and the STL is unordered_map.
11+ */
12+ #include<iostream>
13+ #include <unordered_map>
14+ using namespace std;
15+
16+ int lengthOfLongestSubsetWithZeroSum(int* arr, int size){
17+ unordered_map<int, int> mymap;
18+ int sum = 0;
19+ int maxLength = -1;
20+ for(int i = 0; i < size; i++){
21+ sum += arr[i];
22+ int length = 0;
23+
24+ if(sum == 0){
25+ length = i+1;
26+ }else if(mymap.count(sum)){
27+ length = i - mymap[sum];
28+
29+ }else{
30+ mymap[sum] = i;
31+ }
32+
33+ if(length > maxLength){
34+ maxLength = length;
35+ }
36+ }
37+ return maxLength;
38+ }
39+
40+ int main(){
41+ int size;
42+
43+ cin >> size;
44+ int* arr = new int[size];
45+ for(int i = 0; i < size; i++){
46+ cin >> arr[i];
47+ }
48+ int ans = lengthOfLongestSubsetWithZeroSum(arr,size);
49+ cout << ans << endl;
50+ delete arr;
51+ }
You can’t perform that action at this time.
0 commit comments