0% found this document useful (0 votes)
7 views24 pages

Array Manipulation Techniques in C#

The document contains multiple C# programs that perform various operations on arrays, strings, and numbers. These include finding the largest and smallest elements, reversing arrays, counting distinct elements, checking for palindromes, and determining if a year is a leap year. Each program is structured to read input, process data, and output results accordingly.

Uploaded by

Sushmitha R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Array Manipulation Techniques in C#

The document contains multiple C# programs that perform various operations on arrays, strings, and numbers. These include finding the largest and smallest elements, reversing arrays, counting distinct elements, checking for palindromes, and determining if a year is a leap year. Each program is structured to read input, process data, and output results accordingly.

Uploaded by

Sushmitha R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// 1.

Find Largest Element in an Array

using System;

public class Program1 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

int max = arr[0];

foreach (int num in arr) {

if (num > max) max = num;

[Link](max);

// 2. Find Smallest Element in an Array

using System;

public class Program2 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

int min = arr[0];

foreach (int num in arr) {

if (num < min) min = num;

[Link](min);

}
}

// 3. Find Second Smallest Element in an Array

using System;

public class Program3 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

[Link](arr);

int first = arr[0];

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

if (arr[i] != first) {

[Link](arr[i]);

return;

[Link]("No second smallest");

// 4. Calculate the Sum of Elements in an Array

using System;

public class Program4 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);


int sum = 0;

foreach (int num in arr) sum += num;

[Link](sum);

// 5. Reverse an Array

using System;

public class Program5 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

[Link](arr);

[Link]([Link](" ", arr));

// 6. Sort the Elements of an Array

using System;

public class Program6 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

[Link](arr);

[Link]([Link](" ", arr));

}
}

// 7. Finding the Frequency of Elements in an Array

using System;

using [Link];

public class Program7 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

Dictionary<int, int> freq = new Dictionary<int, int>();

foreach (int num in arr) {

if ([Link](num)) freq[num]++;

else freq[num] = 1;

foreach (var kvp in freq) {

[Link]($"{[Link]} occurs {[Link]} times");

// 8. Counting Distinct Elements in an Array

using System;

using [Link];

public class Program8 {

public static void Main() {

int n = [Link]([Link]());
int[] arr = [Link]([Link]().Split(), [Link]);

HashSet<int> set = new HashSet<int>(arr);

[Link]([Link]);

// 9. Find Repeating Elements in an Array

using System;

using [Link];

public class Program9 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

Dictionary<int, int> freq = new Dictionary<int, int>();

foreach (int num in arr) {

if ([Link](num)) freq[num]++;

else freq[num] = 1;

foreach (var kvp in freq) {

if ([Link] > 1)

[Link]([Link]);

// 10. Find Non-Repeating Elements in an Array

using System;
using [Link];

public class Program10 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

Dictionary<int, int> freq = new Dictionary<int, int>();

foreach (int num in arr) {

if ([Link](num)) freq[num]++;

else freq[num] = 1;

foreach (var kvp in freq) {

if ([Link] == 1)

[Link]([Link]);

// 11. Removing Duplicate Elements from an Array

using System;

using [Link];

public class Program11 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

HashSet<int> seen = new HashSet<int>();

foreach (int num in arr) {


if (![Link](num)) {

[Link](num + " ");

[Link](num);

// 12. Count Even and Odd Elements in an Array

using System;

public class Program12 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

int even = 0, odd = 0;

foreach (int num in arr) {

if (num % 2 == 0) even++;

else odd++;

[Link]($"Even: {even}, Odd: {odd}");

// 13. Find Maximum Product Subarray

using System;

public class Program13 {


public static void Main() {

int n = [Link]([Link]());

int[] nums = [Link]([Link]().Split(), [Link]);

int maxProduct = nums[0], minProduct = nums[0], result = nums[0];

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

if (nums[i] < 0) {

int temp = maxProduct;

maxProduct = minProduct;

minProduct = temp;

maxProduct = [Link](nums[i], maxProduct * nums[i]);

minProduct = [Link](nums[i], minProduct * nums[i]);

result = [Link](result, maxProduct);

[Link](result);

// 14. Determine Array is a Subset of Another

using System;

using [Link];

public class Program14 {

public static void Main() {

string[] size = [Link]().Split();

int n = [Link](size[0]), m = [Link](size[1]);

int[] arr1 = [Link]([Link]().Split(), [Link]);


int[] arr2 = [Link]([Link]().Split(), [Link]);

HashSet<int> set = new HashSet<int>(arr1);

bool isSubset = true;

foreach (int val in arr2) {

if (![Link](val)) {

isSubset = false;

break;

[Link](isSubset ? "Yes" : "No");

// 15. Rotate Array Left and Right by K

using System;

public class Program15 {

public static void Main() {

int[] info = [Link]([Link]().Split(), [Link]);

int n = info[0], k = info[1];

int[] arr = [Link]([Link]().Split(), [Link]);

// Rotate left by k

k %= n;

Reverse(arr, 0, k - 1);

Reverse(arr, k, n - 1);

Reverse(arr, 0, n - 1);

[Link]("Left Rotation: " + [Link](" ", arr));


// Right rotate by k (re-reverse)

Reverse(arr, 0, n - 1);

Reverse(arr, 0, k - 1);

Reverse(arr, k, n - 1);

[Link]("Right Rotation: " + [Link](" ", arr));

static void Reverse(int[] arr, int start, int end) {

while (start < end) {

int temp = arr[start];

arr[start++] = arr[end];

arr[end--] = temp;

// 16. Kth Max and Min Elements

using System;

public class Program16 {

public static void Main() {

string[] parts = [Link]().Split();

int n = [Link](parts[0]);

int k = [Link](parts[1]);

int[] arr = [Link]([Link]().Split(), [Link]);

[Link](arr);

[Link]("Kth Min: " + arr[k - 1]);


[Link]("Kth Max: " + arr[n - k]);

// 17. Kadane's Algorithm - Largest Sum Subarray

using System;

public class Program17 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

int maxSoFar = arr[0], currMax = arr[0];

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

currMax = [Link](arr[i], currMax + arr[i]);

maxSoFar = [Link](maxSoFar, currMax);

[Link](maxSoFar);

// 18. Move Negative Elements to One Side

using System;

public class Program18 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

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

if (arr[i] < 0) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

j++;

[Link]([Link](" ", arr));

// 19. Merge Two Sorted Arrays Without Extra Space

using System;

public class Program19 {

public static void Main() {

string[] sizes = [Link]().Split();

int n = [Link](sizes[0]);

int m = [Link](sizes[1]);

int[] a = [Link]([Link]().Split(), [Link]);

int[] b = [Link]([Link]().Split(), [Link]);

int i = n - 1, j = 0;

while (i >= 0 && j < m && a[i] > b[j]) {

int temp = a[i];

a[i] = b[j];

b[j] = temp;
i--; j++;

[Link](a);

[Link](b);

[Link]([Link](" ", a));

[Link]([Link](" ", b));

// 20. Rearrange Array in Alternating Positive and Negative Items

using System;

public class Program20 {

public static void Main() {

int n = [Link]([Link]());

int[] arr = [Link]([Link]().Split(), [Link]);

int[] result = new int[n];

int pos = 0, neg = 0;

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

if (arr[i] >= 0) pos++;

else neg++;

int iPos = 0, iNeg = 1;

foreach (int num in arr) {

if (num >= 0 && iPos < n) {

result[iPos] = num;
iPos += 2;

} else if (num < 0 && iNeg < n) {

result[iNeg] = num;

iNeg += 2;

[Link]([Link](" ", result));

}
Strings

// 1. Check Whether a Character is Vowel or Consonant

using System;

public class VowelCheck {

public static void Main() {

char ch = [Link]([Link]().ToLower());

if ("aeiou".Contains(ch)) [Link]("Vowel");

else if ([Link](ch)) [Link]("Consonant");

else [Link]("Not an alphabet");

// 2. Check Whether a Character is Alphabet or Not

using System;

public class AlphabetCheck {

public static void Main() {

char ch = [Link]([Link]());

if ([Link](ch)) [Link]("Alphabet");

else [Link]("Not an alphabet");

// 3. Find ASCII Value of a Character

using System;
public class AsciiValue {

public static void Main() {

char ch = [Link]([Link]());

[Link]((int)ch);

// 4. Length of String Without Using Length()

using System;

public class ManualStringLength {

public static void Main() {

string str = [Link]();

int length = 0;

foreach (char c in str) length++;

[Link](length);

// 5. Toggle Each Character in a String

using System;

public class ToggleCase {

public static void Main() {

string input = [Link]();

char[] result = new char[[Link]];

for (int i = 0; i < [Link]; i++) {


if ([Link](input[i])) result[i] = [Link](input[i]);

else if ([Link](input[i])) result[i] = [Link](input[i]);

else result[i] = input[i];

[Link](new string(result));

// 6. Count Number of Vowels in a String

using System;

public class VowelCount {

public static void Main() {

string input = [Link]().ToLower();

int count = 0;

foreach (char c in input) {

if ("aeiou".Contains(c)) count++;

[Link](count);

// 7. Remove Vowels from a String

using System;

public class RemoveVowels {

public static void Main() {

string input = [Link]();


string result = "";

foreach (char c in input) {

if (!"aeiouAEIOU".Contains(c)) result += c;

[Link](result);

// 8. Check if String is a Palindrome

using System;

public class PalindromeCheck {

public static void Main() {

string str = [Link]();

string rev = new string([Link]().ToArray());

[Link](str == rev ? "Palindrome" : "Not Palindrome");

// 9. Reverse a String

using System;

public class ReverseString {

public static void Main() {

string str = [Link]();

[Link](new string([Link]().ToArray()));

}
Numbers

// 10. Even or Odd Number

using System;

public class EvenOddCheck {

public static void Main() {

int num = [Link]([Link]());

[Link](num % 2 == 0 ? "Even" : "Odd");

// 11. Sum of First N Natural Numbers

using System;

public class SumNaturalNumbers {

public static void Main() {

int n = [Link]([Link]());

int sum = n * (n + 1) / 2;

[Link](sum);

// 12. Sum of Numbers in a Given Range

using System;

public class SumInRange {

public static void Main() {


string[] input = [Link]().Split();

int start = [Link](input[0]);

int end = [Link](input[1]);

int sum = 0;

for (int i = start; i <= end; i++) sum += i;

[Link](sum);

// 13. Greatest of Two Numbers

using System;

public class GreatestOfTwo {

public static void Main() {

string[] nums = [Link]().Split();

int a = [Link](nums[0]);

int b = [Link](nums[1]);

[Link]([Link](a, b));

// 14. Greatest of Three Numbers

using System;

public class GreatestOfThree {

public static void Main() {

string[] nums = [Link]().Split();

int a = [Link](nums[0]);
int b = [Link](nums[1]);

int c = [Link](nums[2]);

[Link]([Link](a, [Link](b, c)));

// 15. Leap Year or Not

using System;

public class LeapYearCheck {

public static void Main() {

int year = [Link]([Link]());

[Link]((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? "Leap


Year" : "Not Leap Year");

// 16. Prime Number Check

using System;

public class PrimeCheck {

public static void Main() {

int num = [Link]([Link]());

bool isPrime = num > 1;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) {

isPrime = false;

break;
}

[Link](isPrime ? "Prime" : "Not Prime");

// 17. Prime Numbers in Given Range

using System;

public class PrimeInRange {

public static void Main() {

string[] input = [Link]().Split();

int start = [Link](input[0]);

int end = [Link](input[1]);

for (int num = start; num <= end; num++) {

bool isPrime = num > 1;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) {

isPrime = false;

break;

if (isPrime) [Link](num + " ");

// 18. Sum of Digits of a Number


using System;

public class SumOfDigits {

public static void Main() {

int num = [Link]([Link]());

int sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

[Link](sum);

// 19. Reverse of a Number

using System;

public class ReverseNumber {

public static void Main() {

int num = [Link]([Link]());

int rev = 0;

while (num > 0) {

rev = rev * 10 + num % 10;

num /= 10;

[Link](rev);

}
// 20. Palindrome Number

using System;

public class PalindromeNumber {

public static void Main() {

int num = [Link]([Link]());

int orig = num, rev = 0;

while (num > 0) {

rev = rev * 10 + num % 10;

num /= 10;

[Link](orig == rev ? "Palindrome" : "Not Palindrome");

You might also like