Skip to content

Commit 3ee043e

Browse files
Merge branch 'smv1999:master' into redundant-braces
2 parents 2e7fac0 + 8eb0c20 commit 3ee043e

24 files changed

+1678
-0
lines changed

Arrays/Maximum_Difference.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
From all the positive integers entered in a list, the aim of the program is
3+
to subtract any two integers such that the result/output is the maximum
4+
possible difference.
5+
6+
'''
7+
8+
# class to compute the difference
9+
class Difference:
10+
def __init__(self, a):
11+
# getting all elements from the entered list
12+
self.__elements = a
13+
def computeDifference(self):
14+
# maximum difference would be the difference between the largest and the smallest integer
15+
Difference.maximumDifference = max(self.__elements)-min(self.__elements)
16+
return Difference.maximumDifference
17+
# end of Difference class
18+
19+
# getting the input
20+
_ = input()
21+
a = [int(e) for e in input().split(' ')]
22+
23+
# creating an object of the class
24+
d = Difference(a)
25+
# calling function 'computeDifference' to compute the difference
26+
d.computeDifference()
27+
28+
# printing the result
29+
print(d.maximumDifference)
30+
31+
'''
32+
33+
COMPLEXITY:
34+
35+
Time Complexity -> O(N)
36+
Space Complexity -> O(1)
37+
38+
Sample Input:
39+
3
40+
1 2 5
41+
42+
Sample Output:
43+
4
44+
45+
Explanation:
46+
Integer with max value--> 5
47+
Integer with min value--> 1
48+
Hence, maximum difference--> 5-1 = 4
49+
50+
'''

Arrays/Min_Max_Sum.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Aim: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
3+
4+
Example:
5+
arr = [1,3,5,7,9]
6+
The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24. So, the output will be:
7+
16 24
8+
9+
'''
10+
11+
def MMSum(a):
12+
# sorting the array so that it becomes easier to find the min and max set of values
13+
a.sort()
14+
minn=0
15+
maxx=0
16+
for i in range(0,4):
17+
# summing up all minimum values
18+
minn+=a[i]
19+
for i in range(1,5):
20+
# summing up all maximum values
21+
maxx+=a[i]
22+
print(minn,maxx)
23+
24+
# getting the input
25+
user_input = (input().strip().split())
26+
array = []
27+
for i in user_input:
28+
array.append(int(i))
29+
# calling the Min-Max-Sum function
30+
MMSum(array)
31+
32+
'''
33+
34+
COMPLEXITY:
35+
36+
Time Complexity -> O(N)
37+
Space Complexity -> O(N)
38+
39+
Sample Input:
40+
2 1 3 4 5
41+
42+
Sample Output:
43+
10 14
44+
45+
Explanation:
46+
Sorted array: [1,2,3,4,5]
47+
Min Sum: 1+2+3+4 = 10
48+
Max Sum: 2+3+4+5 = 14
49+
50+
'''

Arrays/integer.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Title : Enter the data elments before that mention the their total count . .
2+
After entering the data you first missed positive integer missed will
3+
be displayed .
4+
Code is done in C language .
5+
*/
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
int main()
9+
{
10+
int n ,j=0, i,k=1 , val;
11+
int opt=1;
12+
printf("\n Enter the total number of elements : \n");
13+
scanf("%d",&n);
14+
if(n<=0)
15+
{
16+
printf("\n Invalid Input , Try again later !! \n");
17+
exit(0);
18+
}
19+
int a[n];
20+
printf("\n Enter the elements : \n");
21+
for(i=0;i<n;i++)
22+
{ scanf("%d",&a[i]); }
23+
24+
for(i=0;i<n;i++)
25+
{ for(j=0;j<n-1;j++)
26+
if(a[j]>a[j+1])
27+
{ val=a[j];
28+
a[j]=a[j+1];
29+
a[j+1]=val; }
30+
}
31+
printf("\n Sorted array : \n");
32+
for(i=0;i<n;i++)
33+
{
34+
printf(" %d ", a[i]);
35+
}
36+
printf("\n \n");
37+
printf("\n Postive element missed from the data is ");
38+
if(a[n-1]<=0)
39+
{
40+
printf("\n1\n");
41+
42+
43+
}
44+
else {
45+
46+
for(i=0;i<n;i++)
47+
{
48+
if(a[i]>0)
49+
{
50+
for(j=0+i, k=1;j<=n && k<=n+1;k++,j++)
51+
{
52+
if(a[j]==k)
53+
{
54+
continue ;
55+
}
56+
else
57+
{
58+
59+
printf(" %d " ,k);
60+
exit(0);
61+
62+
}
63+
64+
65+
66+
}
67+
}
68+
}
69+
}
70+
71+
}
72+
73+
74+
/*
75+
76+
Enter the total number of elements :
77+
7
78+
79+
Enter the elements :
80+
0
81+
-1
82+
1
83+
-2
84+
-3
85+
-4
86+
-5
87+
88+
Sorted array :
89+
-5 -4 -3 -2 -1 0 1
90+
91+
92+
Postive element missed from the data is 2
93+
94+
*/

Codechef Problems/Food_Chain.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* PROBLEM STATEMENT
2+
A great deal of energy is lost as metabolic heat when the organisms from one trophic level are consumed by the next level.
3+
Suppose in Chefland the energy reduces by a factor of K, i.e, if initially, the energy is X, then the transfer of energy to the next tropic level is ⌊XK⌋. This limits the length of foodchain which is defined to be the highest level receiving non-zero energy.
4+
E is the energy at the lowest tropic level. Given E and K for an ecosystem, find the maximum length of foodchain.
5+
Note: ⌊x⌋ denoted the floor function, and it returns the greatest integer that is less than or equal to x (i.e rounds down to the nearest integer). For example, ⌊1.4⌋=1, ⌊5⌋=5, ⌊−1.5⌋=−2, ⌊−3⌋=−3 , ⌊0⌋=0.
6+
Input Format
7+
First line will contain T, number of testcases. Then the testcases follow.
8+
Each testcase contains a single line of input, two integers E,K.
9+
Output Format
10+
For each testcase, output in a single line answer to the problem.
11+
Constraints
12+
1≤T≤104
13+
1≤E≤109
14+
2≤K≤109
15+
Sample Input 1
16+
3
17+
5 3
18+
6 7
19+
10 2
20+
Sample Output 1
21+
2
22+
1
23+
4
24+
Explanation
25+
TestCase 1: The energy at first level is 5 units. For the second level energy becomes ⌊53⌋=1 units. So the length of foodchain is 2 since from the next level onwards 0 units of energy will be received.
26+
TestCase 3: The energy at different levels is:
27+
Level 1- 10 units
28+
Level 2- ⌊102⌋=5 units
29+
Level 3- ⌊52⌋=2 units
30+
Level 4- ⌊22⌋=1 units
31+
Level 5- ⌊12⌋=0 units
32+
So the answer is 4, since it is the last level to receive non-zero energy. */
33+
#include <stdio.h>
34+
35+
int main(void) {
36+
37+
int t,e,k;
38+
int i; int j=0;
39+
scanf("%d",&t);
40+
int tc=t; int arr[t];
41+
while(t--)
42+
{
43+
scanf("%d %d",&e,&k);
44+
int count=0;
45+
i=e;
46+
while(i>0)
47+
{
48+
count++;
49+
i=i/k;
50+
51+
}
52+
arr[j]=count;
53+
j++;
54+
}
55+
56+
for(j=0;j<tc;j++)
57+
printf("%d\n",arr[j]);
58+
return 0;
59+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#https://leetcode.com/problems/reverse-linked-list/
2+
3+
# Iterative method
4+
#Approach :
5+
6+
# Store the head in a temp variable called current .
7+
8+
# curr = head , prev = null
9+
10+
# Now for a normal linked list , the current will point to the next node and so on till null
11+
# For reverse linked list, the current node should point to the previous node and the first node here will point to null
12+
13+
# Keep iterating the linkedlist until the last node and keep changing the next of the current node to prev node and also
14+
# update the prev node to current node and current node to next node
15+
16+
17+
# class ListNode:
18+
# def __init__(self, val=0, next=None):
19+
# self.val = val
20+
# self.next = next
21+
22+
class Solution:
23+
24+
def reverseList(self, head):
25+
curr = head
26+
prev = None
27+
while(curr != None):
28+
next = curr.next
29+
curr.next = prev
30+
prev = curr
31+
curr = next
32+
33+
return prev
34+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach :
2+
# Divide the linked list to two halved
3+
# First half is head and the remaining as rest
4+
# The head points to the rest in a normal linked list
5+
# In the reverse linked list , the next of current points to the prev node and the head node should point to NULL
6+
# Keep continuing this process till the last node
7+
8+
9+
# Definition for singly-linked list.
10+
# class ListNode:
11+
# def __init__(self, val=0, next=None):
12+
# self.val = val
13+
# self.next = next
14+
15+
16+
class Solution:
17+
def reverseList(self, head):
18+
if head is None or head.next is None:
19+
return head
20+
rest = self.reverseList(head.next)
21+
head.next.next = head
22+
head.next = None
23+
return rest

0 commit comments

Comments
 (0)