Posts

Showing posts from October, 2021

Final Value of Variable After Performing Operations (LeetCode)

  There is a programming language with only   four   operations and   one   variable   X : ++X  and  X++   increments  the value of the variable  X  by  1 . --X  and  X--   decrements  the value of the variable  X  by  1 . Initially, the value of  X  is  0 . Given an array of strings  operations  containing a list of operations, return  the  final  value of  X   after performing all the operations .   Example 1: Input: operations = ["--X","X++","X++"] Output: 1 Explanation:  The operations are performed as follows: Initially, X = 0. --X: X is decremented by 1, X = 0 - 1 = -1. X++: X is incremented by 1, X = -1 + 1 = 0. X++: X is incremented by 1, X = 0 + 1 = 1. Solutions: class Solution { public int finalValueAfterOperations(String[] operations) { int val=0; for(int i=0; i<operations.length; i++){ if(operations[i].charAt(1)=='+') val++; else val--; } return val

USP

  16-10 0. Random #include <stdio.h> #include <math.h> int main(void) {int y; char condition; do{ y=rand()%100; printf("%d \n",y); printf("\n want more random number? "); scanf("%c", &condition); }while(condition== 'y' || condition== 'Y'); return 0; } 1. Pointer #include <stdio.h> #include <math.h> int main(void) { int y=34; int *temp; temp=&y; printf("value at address of y is%d y=%d\n ", *temp, y);//value at printf("\n %u", &y);//value at printf("\n %u", temp);//value at return 0; } 2. Pointer #include <stdio.h> #include <math.h> int main(void) { int y=34; char arr[10]; int *temp; temp=&y; printf("value at address of y is%d y=%d\n ", *temp, y);//value at printf("\n arr %u", arr); printf("\n arr 0 %u", &arr[0])

Remove Element (LeetCode)

  27.   Remove Element Easy 2659 4179 Add to List Share Given an integer array  nums  and an integer  val , remove all occurrences of  val  in  nums   in-place . The relative order of the elements may be changed. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the  first part  of the array  nums . More formally, if there are  k  elements after removing the duplicates, then the first  k  elements of  nums  should hold the final result. It does not matter what you leave beyond the first  k  elements. Return  k  after placing the final result in the first  k  slots of  nums . Do  not  allocate extra space for another array. You must do this by  modifying the input array  in-place  with O(1) extra memory. Example 1: Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the re