Problem 3

34 comments on “Problem 3”


  • Raman

    // Online C compiler to run C program online
    #include

    int main() {
    int x,count=0;
    int arr[] = {1, 1, 2, 2, 2, 2, 3,};
    int r=sizeof(arr)/sizeof(arr[0]);
    scanf(“%d”,&x);
    for(int i=0;i<r;i++){
    if(arr[i]==x){
    count++;
    }
    }
    if(count==0){
    printf("it do not appear");
    }
    else{
    printf("appears %d times ",count);
    }
    return 0;
    }


  • kartik

    int freq(int arr[],int start,int end,int find){
    if(start>end){
    return -1;
    }
    int mid=(start+end)/2;
    if(arr[mid]==find){
    int count=1;
    for(int i=mid-1;i>=start;i–){
    if(arr[i]==find){
    count++;
    }
    else{
    break;
    }
    }
    for(int i=mid+1;i<=end;i++){
    if(arr[i]==find){
    count++;
    }
    else{
    break;
    }
    }
    return count;
    }
    else if (arr[mid]<find)
    {
    return freq(arr,mid+1,end,find);
    }
    else{
    return freq(arr,start,mid-1,find);
    }
    }

    main(){
    int arr[]={1,1,2,2,3,3,3,3,4,4,5,5,6,6,6};
    int occ=freq(arr,0,14,1);
    cout<<occ;
    }


  • Rohini Ganorkar

    #include
    int main()
    {

    int arr[100],n,x=2,count=0,i;
    scanf(“%d”,&n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&arr[i]);

    }
    for(i=0;i<n;i++)
    {
    if(arr[i]==x)
    count++;
    }
    printf("%d",count);

    return 0;
    }


  • rohan

    #include
    void main()
    {
    int i,n,y[30],x,count=0;
    printf(“Enter number of terms”);
    scanf(“%d”,&n);
    printf(“Enter the numbers”);
    for(i=0;i<n;++i)
    scanf("%d",&y[i]);
    printf("Enter value of x \n");
    scanf("%d", &x);
    for(i=0;i<n;++i)
    {
    if(y[i]==x)
    count++;
    }
    if(count==0)
    {
    printf("value doesnt exist in given array");
    }
    else
    {
    printf("%d",count);
    }
    }


  • Sandipan

    def foo(m,k):
    l=0
    r=len(m)-1
    count=0

    while lk:
    r=mid-1
    elif(m[mid]<k):
    l=mid+1

    return count

    m=[1,2,3,4,5,5,5,6]

    print(foo(m,5))


  • Sameer

    // O(log(n)) Solution in C++

    #include
    using namespace std;
    int main() {
    int arr[] = {1, 1, 2, 2, 2, 2, 3};
    int n = sizeof(arr)/sizeof(arr[0]);
    int x = 2, cnt = 0;
    for(int i=0;ix)
    break;
    }
    cout<<cnt<<endl;
    return 0;
    }


  • Nikkala

    import java.util.*;
    class Main
    {
    public static void main (String[] args)
    {
    Scanner n=new Scanner(System.in);
    int a=n.nextInt();
    int x=n.nextInt();
    int a1[]=new int[a];
    int count=0;
    System.out.println(“enter the elements”);
    for(int i=0;i<a;i++)
    {
    a1[i]=n.nextInt();
    }
    for(int i=0;i<a;i++)
    {
    if(x==a1[i])
    {
    count=count+1;

    }
    }
    System.out.println("the elemnt");
    System.out.println(count);
    }
    }


  • subhojit

    #Simple python Code whose expected Time Complexity is O(log n)
    arr=[int(x) for x in input().split(‘ ‘)]
    n=int(input())
    low=0
    ct=0
    high=len(arr)-1

    for i in range(low,high+1,1):
    mid=(low+high)//2
    if arr[low]==n:
    while True:
    if arr[low]==n:
    ct+=1
    low=low+1
    else:
    break
    break
    elif arr[high]==n:
    while True:
    if arr[high]==n:
    ct+=1
    high-=1
    else:
    break
    break
    elif arr[mid]>n:
    high=mid-1
    elif arr[mid]<n:
    low=mid+1
    else:
    p=mid-1
    while True:
    if arr[mid]==n:
    ct+=1
    mid+=1
    else:
    break
    while True:
    if arr[p]==n:
    ct+=1
    p-=1
    else:
    break
    break

    print(ct)


  • kandukuri

    #include
    void main()
    {
    int n;
    printf(“enter the length: “);
    scanf(“%d”, &n);
    int arr[n];
    for (int i = 0; i < n; i++)
    {
    scanf("%d", &arr[i]);
    }
    int freq, count = 0;
    printf("enter the freq: ");
    scanf("%d",&freq);
    for (int i = 0; i 0)
    {
    printf(“%d”, count);
    }
    else
    {
    printf(“-1 the number is not found!”);
    }
    }


  • Mohd Saif

    arr=[7,4,5,2]
    for i in range(0,len(arr)):
    for j in range(i):
    if(arr[j]>arr[j+1]):
    temp=arr[j]
    arr[j]=arr[j+1]
    arr[j+1]=temp
    print(arr)


  • Mohd Saif

    arr=[14,33,27,10,35,19,42,44]
    for i in range(0,len(arr)):
    for j in range(i):
    if(arr[j]>arr[j+1]):
    temp=arr[j]
    arr[j]=arr[j+1]
    arr[j+1]=temp
    print(arr)


  • Hritik

    #include
    void bubblesort(int [],int);
    int main(){
    int arr[] = {33,65,23,1,3};
    int i;
    bubblesort(arr,sizeof(arr)/sizeof(arr[0]));
    for(i=0;i<sizeof(arr)/sizeof(int);i++){
    printf("%d\t",arr[i]);
    }
    return 0;
    }
    void bubblesort(int arr[] , int n){
    int i,j,temp;
    for(i=1;i<=n-1;i++){
    for(j=0;jarr[j+1]){
    temp = arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
    }
    }
    }