Posts

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

Java Output Formatting | Hackerrank

  Java's   System.out.printf   function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using   printf . To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution. Input Format Every line of input will contain a  String  followed by an  integer . Each  String  will have a maximum of   alphabetic characters, and each  integer  will be in the inclusive range from   to  . Output Format In each line of output there should be two columns: The first column contains the  String  and is left justified using exactly   characters. The second column contains the  integer , expressed in exactly   digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes. Sample Input java 100 cpp 65 python 50 Sample Output ================================ java 100 cpp 065 python

Fibonacci Number (LeetCode)

  The   Fibonacci numbers , commonly denoted   F(n)   form a sequence, called the   Fibonacci sequence , such that each number is the sum of the two preceding ones, starting from   0   and   1 . That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given  n , calculate  F(n) .   Example 1: Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2. SOLUTIONS (Java): class Solution { public int fib(int n) { int x=0, y=1, sum=0; if(n==0 || n==1) return n; else{ for(int i=2; i<=n;i++){ sum= x+y; x=y; y=sum; } } return sum; } }

China will fight six wars in the next 50 years!

 (The source of this content:  Tap Here  ) China is a big country without unity. This is the shame of the Chinese nation and the shame of the descendants of Yan and Huang.  For the unity of the country and the dignity of the nation, China must wage six wars in the next 50 years. It may be a nationwide war or a local war. But no matter which war, China must wage a united war.      The first war: reunification of Taiwan (2020-2025)     Although the two sides of the strait are becoming more and more peaceful, don't assume that the Taiwan's ruling authority (whether it is the KMT or the DPP) will peacefully reunify with the mainland. This is not in line with the ruling authority's campaign needs, so it will maintain the status quo with the mainland for a long time. The party is in favor, the DPP makes trouble, the Kuomintang and Yihe each obtain more political bargaining chips from it, "independence" dare not really "independence", make trouble, "unific