Task 1 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define SIZE 4 #define ROWS 2 void demo1() { int arr[SIZE] = {1, 9, 8, 4}; int i; // Output memory size of array arr printf("sizeof(arr) = %zu\n", sizeof(arr)); // Output address and value of each element for (i = 0; i < SI...
Three Ways to Define Arrays in C Array definition in C can be achieved through three distinct approaches, each with different characteristics regarding size determination, memory location, and lifetime. Fixed-Size Arrays Array size is determined at compile time: #include <stdio.h> int main() {...
An array can store multiple elements of the same data type. Arrays are also a data type, specifically a reference type. Array ------> a collection of data Usage Patterns Dynamic Initialization – Method 1 Array definition: dataType arrayName[] = new dataType[size] Example: int a[] = new int[5] –...
Consider the following code snippet: List<String> list = new ArrayList<>(); list.add("1"); Object[] array = list.toArray(); array[0] = 1; System.out.println(Arrays.toString(array)); This executes without error. However, modifying the code to use Arrays.asList leads to an ArrayS...
Problem Description Given a sorted array, remove the duplicates in-place such that each element appears only once and return the new length. Do not allocate extra space for another array; you must modify the input array in-place with O(1) extra memory. Example 1: Given numbers = [1, 1, 2], The funct...
Arrays are containers that store multiple values of the same data type. Array Declaration and Initialization Static Initialization - Full Syntax dataType[] arrayName = new dataType[]{element1, element2, element3}; Example: int[] numbers = new int[]{11, 22, 33}; Static Initialization - Simplified Syn...
Problem Specification Given an integer array nums containing n elements and a specific target value, the objective is to identify three distinct integers within the array such that their sum is nearest to the target. The function should return this specific sum. It is guaranteed that a unique optima...
1D and 2D Integer Array Memory Layout Analysis #include <stdio.h> #define ROWS 2 #define COLS 4 void analyze_single_dim() { int values[COLS] = {1, 9, 8, 4}; int idx; printf("Total size of values: %lu bytes\n", sizeof(values)); for (idx = 0; idx < COLS; idx++) printf("Address...
Jump Game: Greedy Reachability public boolean canJump(int[] nums) { int farthest = 0; for (int idx = 0; idx < nums.length; idx++) { if (idx > farthest) return false; farthest = Math.max(farthest, idx + nums[idx]); if (farthest >= nums.length - 1) return true; } return true; } The core idea...
Finding Element Posiitons The indexOf() method locates the first occurrence of a specified value within an array and returns its position: const sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const position = sequence.indexOf(7); console.log(position); // Output: 6 Combining Arrays Arrays can be merged usi...