0% found this document useful (0 votes)
1 views9 pages

CP Codes Notes

The document contains various programming tasks and algorithms in C and Python, including Fibonacci sequences, GCD, LCM, and binary search implementations. It also includes interview questions and coding challenges such as creating Pascal's Triangle and reversing words in a string. Additionally, it references resources like GeeksforGeeks for further coding interview preparation.

Uploaded by

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

CP Codes Notes

The document contains various programming tasks and algorithms in C and Python, including Fibonacci sequences, GCD, LCM, and binary search implementations. It also includes interview questions and coding challenges such as creating Pascal's Triangle and reversing words in a string. Additionally, it references resources like GeeksforGeeks for further coding interview preparation.

Uploaded by

01fe22bcs073
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C programming

Oops m jitna c++ h

Python touchup

Projects in detail , resume padho

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.

1. Iterative Fibonacci (Efficient)

python

CopyEdit

def fibonacci_iter(n):

a, b = 0, 1

for _ in range(n):

print(a, end=' ')

a, b = b, a + b

2. Recursive Fibonacci (Simple but inefficient)

python

CopyEdit

def fibonacci_rec(n):

if n <= 1:

return n

return fibonacci_rec(n-1) + fibonacci_rec(n-2)

# Print first 10 Fibonacci numbers

for i in range(10):

print(fibonacci_rec(i), end=' ')


GFG

https://www.geeksforgeeks.org/c/c-coding-interview-questions/

5. *** Write a Program to Replace all 0’s with 1’s in a Number

ans = ans + (N % 10) * pow(10, i);

Armstrong number = 153 = 1^3 +5^3 + 3^3

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;

return gcd(b, a % b);

19. Write a Program to create a pyramid pattern using C

// inner Loop for space printing

for (int j = 1; j <= N - i; j++)

printf(" ");

// inner Loop for star printing

for (int j = 1; j < 2 * i; j++)

printf("*");

printf("\n");

20. Write a program to form Pascal Triangle using numbers.

// C Program to print

// Pascal's Triangle

#include <stdio.h>
int main()

int n = 5;

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

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

printf(" ");

int x = 1;

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

printf("%d ", x);

x = x * (i - j) / j;

printf("\n");

return 0;

#include <stdlib.h> -> qsort() for quick sort

qsort(arr, n, sizeof(int), compare);

int compare(const void* a, const void* b) {

return (*(int*)a - *(int*)b);


}

or

this could also work

int compareIntegers(const void* a, const void* b) {

int *A = (int*)a;

int *B = (int*)b;

return (*A - *B);

27. Write a Program to print sums of all subsets in an array.?????????????????????????


someone explain to me

29. Write a C program to Implement Kadane's Algorithm

// C program to implement Kadane's Algorithm

#include <limits.h>

#include <stdio.h>

int main()

int a[] = { -2, -3, 1, -1,-3 , 4, -2, 1, 5, -3 };

int n = sizeof(a) / sizeof(a[0]);

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;

printf("Maximum contiguous sum is %d\n", maxSum);

printf("Start index: %d End index: %d\n", max_start, max_end);

return 0;

### iteratre through a string


char s[] = "124259";

int ans = 0;

// iterate through all the number

for (int i = 0; s[i] != '\0'; i++)

42. Write a C Program to search elements in an array using Binary Search.

// C program to Search element


// in Array using Binary Search

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2;

// If the element is present at the middle

// itself

if (arr[mid] == x)

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present

// in right subarray

return binarySearch(arr, mid + 1, r, x);

return -1;

# hare and tortoise algo

// For odd nodes, fast->next is head and


// for even nodes, fast->next->next is head

while (fast->next != head

&& fast->next->next != head) {

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/

##stocks buy and sell

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

Minimum number of increment-other operations to make all array elements equal.

STRING!!
char *token = strtok(str, "."); , to tokenize

Q )Reverse words in a string

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
#define MAX 1000

// Function to reverse words separated by dots

void reverseWords(char str[]) {

char words[MAX][MAX]; // 2D array to store each word

int count = 0;

// Tokenize using '.'

char *token = strtok(str, ".");

while (token != NULL) {

if (strlen(token) > 0) { // Ignore empty tokens from multiple dots

strcpy(words[count++], token);

token = strtok(NULL, ".");

// Print words in reverse

for (int i = count - 1; i >= 0; i--) {

printf("%s", words[i]);

if (i > 0)

printf(".");

printf("\n");

int main() {

char str1[] = "i.like.this.program.very.much";

char str2[] = "..geeks..for.geeks.";


char str3[] = "...home......";

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;

You might also like