CP Codes Notes
CP Codes Notes
Python touchup
OS ka thoda
"Why did you apply for Toshiba?", "What do you do when you are demotivated?", "What all
books do you read?" were asked.
python
CopyEdit
def fibonacci_iter(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
python
CopyEdit
def fibonacci_rec(n):
if n <= 1:
return n
for i in range(10):
https://www.geeksforgeeks.org/c/c-coding-interview-questions/
LCM
LCM(a, b) = (a * b) / GCD(a, b)
GCD= chote number ko reduce karte jao jab tak dono a,b ka us number ke sath % 0 nahi ho
jata(better use recursion)
int gcd(int a, int b) {
if (b == 0)
return a;
printf(" ");
printf("*");
printf("\n");
// C Program to print
// Pascal's Triangle
#include <stdio.h>
int main()
int n = 5;
printf(" ");
int x = 1;
x = x * (i - j) / j;
printf("\n");
return 0;
or
int *A = (int*)a;
int *B = (int*)b;
#include <limits.h>
#include <stdio.h>
int main()
int maxSum=INT_MIN;
int currSum=0,max_start=0,max_end=0,curr_start=0;
for(int i=0;i<n;i++){
currSum+=a[i];
if(currSum>maxSum){
maxSum = currSum;
max_start=curr_start;
max_end=i;
if(currSum<0)
currSum = 0;
curr_start = i + 1;
return 0;
int ans = 0;
#include <stdio.h>
if (r >= l) {
int mid = l + (r - l) / 2;
// itself
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
// in right subarray
return -1;
fast = fast->next->next;
slow = slow->next;
Traverse the circular linked list using the fast and slow pointer technique to find
the middle and last nodes. The slow pointer will reach the middle, and the
fast pointer will reach the end.
If the list has an odd number of nodes, the fast pointer will reach the last node. If the
list has an even number, it will stop just before the last node.
## permutation
https://www.geeksforgeeks.org/dsa/next-permutation/
Set a locak minima , until a value less than that is found , and find the local maxima after
that too….
if a new minimum is found , update the minimum and find profits with next values
here sore the min and max index!!!
https://www.geeksforgeeks.org/dsa/stock-buy-sell/
array qn
STRING!!
char *token = strtok(str, "."); , to tokenize
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
int count = 0;
strcpy(words[count++], token);
printf("%s", words[i]);
if (i > 0)
printf(".");
printf("\n");
int main() {
printf("Output 1: ");
reverseWords(str1); // much.very.program.this.like.i
printf("Output 2: ");
reverseWords(str2); // geeks.for.geeks
printf("Output 3: ");
reverseWords(str3); // home
return 0;