Static int MaxJuftSoni(int toplam[], int a)


Download 53.86 Kb.
Sana09.11.2021
Hajmi53.86 Kb.
#172579
Bog'liq
1-amaliyXDVI


1. N natural soni va n ta butun sondan iborat to’plam berilgan. To’plamda ketmaket keladigan juft elementlarning maksimal sonini aniqlovchi sodda dastur yozing

class GFG


{
static int MaxJuftSoni(int toplam[], int a)
{
int joriy_max = 0, max_kiyingi_qiymat = 0;

for (int i = 0; i < a; i++)


{
if (toplam[i] % 2 != 0)
joriy_max = 0;
else
{
joriy_max++; // increase count
max_kiyingi_qiymat = Math.max(joriy_max,
max_kiyingi_qiymat);
}
}
return max_kiyingi_qiymat;
}
public static void main (String[] args)
{
int toplam[] = { 11,36,28,40,0,14,49 };
int a = toplam.length;
System.out.println(MaxJuftSoni(toplam, a));
}
}

Natija1:


2) N natural soni va n ta butun sondan iborat to’plam berilgan. Ikkita qo’shni son yig’indisining eng katta qiymatini aniqlovchi sodda dastur yozing.

public class class3 {
public static void main(String[] args) {
int[] a = { 9,7,11,18,0,-8,5 };
int[] sum = new int[a.length - 1];
int j = 0;

for (int i = 0; i < a.length - 1; i++) {


sum[j] = a[i] + a[i + 1];
j++;
}
int max = sum[0];
for (int k = 1; k < sum.length; k++) {
if (max < sum[k]) {
max = sum[k];
}
}
System.out.println(max);
}

}

3) N natural soni va n ta butun sondan iborat to’plam berilgan. To’plamda ketmaket keladigan eng katta elementning maksimal sonini aniqlovchi sodda dastur yozing

import math as mt

def maxumSets(s, y):

cnt = 0

num = 0


l = len(s)

f = 0


for i in range(l):

num = num * 10 + (ord(s[i]) - ord('0'))

if (num >= y):

f = 1


else:

if (f):


cnt += 1

num = ord(s[i]) - ord('0')

f = 0

# Check for current number



if (num <= y):

f = 1


else:

num = 0


if (f):

cnt += 1


return cnt

# Driver Code

s = "12345"

y = 5


print(maxumSets(s, y))
Download 53.86 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling