0% found this document useful (0 votes)
5 views6 pages

Assignment-Array

The document contains an assignment for the Data Structure course (CSE 212.10) submitted by Shreela Dhar, detailing three programming problems. Problem #1 involves detecting whether a sequence is arithmetic or geometric, Problem #2 focuses on removing duplicates from a sorted array, and Problem #3 addresses adding one to a number represented as an array. Each problem includes sample code, input, output, and an explanation of the functionality.

Uploaded by

arkadasgupta51
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)
5 views6 pages

Assignment-Array

The document contains an assignment for the Data Structure course (CSE 212.10) submitted by Shreela Dhar, detailing three programming problems. Problem #1 involves detecting whether a sequence is arithmetic or geometric, Problem #2 focuses on removing duplicates from a sorted array, and Problem #3 addresses adding one to a number represented as an array. Each problem includes sample code, input, output, and an explanation of the functionality.

Uploaded by

arkadasgupta51
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/ 6

Course title: Data Structure

Course Code: CSE 212.10

Assignment

Submitted by
Shreela Dhar
ID: 242005312
Section: 4
Department: Computer Science and
Engineering (CSE)

Submitted To,
Name: Mohammad Akbar Bin Shah
Designation: Lecturer
Problem #1: ArithGeo (Arithmetic or Geometric Sequence
Detection):
#include <iostream>
#include <vector>

using namespace std;

string ArithGeo(vector<int> arr) {


if (arr.size() < 2) return "-1"; // A sequence with less than 2
numbers can't be classified

bool isArithmetic = true, isGeometric = true;


int diff = arr[1] - arr[0];
double ratio = (double)arr[1] / arr[0];

for (size_t i = 1; i < arr.size() - 1; i++) {


if (arr[i + 1] - arr[i] != diff) {
isArithmetic = false;
}
if ((double)arr[i + 1] / arr[i] != ratio) {
isGeometric = false;
}
}

if (isArithmetic) return "Arithmetic";


if (isGeometric) return "Geometric";
return "-1";
}

int main() {
vector<int> arr = {5, 10, 15, 20}; // Example input
cout << "Output: " << ArithGeo(arr) << endl;
return 0;
}
Sample Input & Output
Input:
5 10 15 20
Output:

Input:
2 4 8 16
Output:

Explanation
The function checks if the sequence follows an arithmetic pattern
(constant difference) or a geometric pattern (constant ratio). If
neither pattern is found, it returns -1.

Problem #2: Remove Duplicates from Sorted Array


#include <iostream>
#include <vector>

using namespace std;

int removeDuplicates(vector<int>& nums) {


if (nums.empty()) return 0;

int index = 0;
for (size_t i = 1; i < nums.size(); i++) {
if (nums[i] != nums[index]) {
index++;
nums[index] = nums[i];
}
}

for (size_t i = index + 1; i < nums.size(); i++) {


nums[i] = '_'; // Represent empty spaces with underscores
}

return index + 1;
}

int main() {
vector<int> nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; // Example input
int uniqueCount = removeDuplicates(nums);

cout << "Output: " << uniqueCount << ", nums = [";
for (size_t i = 0; i < nums.size(); i++) {
if (i > 0) cout << ", ";
if (nums[i] == '_') cout << "_";
else cout << nums[i];
}
cout << "]" << endl;

return 0;
}

Sample Input & Output


Input:
nums = [0,0,1,1,1,2,2,3,3,4]
Output:
Explanation
The function modifies the input array in-place to remove duplicate
elements while maintaining order. It replaces extra spaces with
underscores _ and returns the count of unique elements.

Problem #3: Add One to a Number Represented as an Array:


#include <iostream>
#include <vector>

using namespace std;

vector<int> plusOne(vector<int>& digits) {


int n = digits.size();

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


if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}

digits.insert(digits.begin(), 1); // If all digits were 9, add a new


digit at the start
return digits;
}

int main() {
vector<int> digits = {0, 9, 9}; // Example input
vector<int> result = plusOne(digits);

cout << "Output: [";


for (size_t i = 0; i < result.size(); i++) {
if (i > 0) cout << ", ";
cout << result[i];
}
cout << "]" << endl;

return 0;
}
Sample Input & Output
Input:
digits = [0, 9, 9]
Output:

Explanation
The function iterates from the last digit, incrementing it. If a carry
occurs (e.g., 9+1=10), it sets that digit to 0 and propagates the carry
leftward. If all digits turn to zero, a new 1 is added at the beginning.

You might also like