Sort by

recency

|

629 Discussions

|

  • + 0 comments
    My Code in C

    int main() { int n = parse_int(ltrim(rtrim(readline())));

    char** a_temp = split_string(rtrim(readline()));
    
    int* a = malloc(n * sizeof(int));
    
    for (int i = 0; i < n; i++) {
        int a_item = parse_int(*(a_temp + i));
    
        *(a + i) = a_item;
    }
    
    // Write your code here
    int c=0,temp,s=0;
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-1;j++){
            if(a[j]>a[j+1]){
                temp=a[j+1];
                a[j+1]=a[j];
                a[j]=temp;
                s++;
            }
            else {
            c++;
            }
        }
        if(c==1){
            break;
        }
    }
    printf("Array is sorted in %d swaps.\n",s);
    printf("First Element: %d\n", a[0]);
    printf("Last Element: %d",a[n-1]);
    return 0;
    

    }

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
        List<Integer> a = Stream.of(bufferedReader.readLine().trim().split("\\s+"))
                                .map(Integer::parseInt)
                                .collect(Collectors.toList());
    
        int totalSwaps = 0;
    
        for (int i = n - 1; i > 0; i--) {
            int swapsInPass = 0;
            for (int j = 0; j < i; j++) {
                if (a.get(j) > a.get(j + 1)) {
                    Collections.swap(a, j, j + 1);
                    swapsInPass++;
                    totalSwaps++;
                }
            }
            if (swapsInPass == 0) break; // Stop early if no swaps in this pass
        }
    
        System.out.println("Array is sorted in " + totalSwaps + " swaps.");
        System.out.println("First Element: " + a.get(0));
        System.out.println("Last Element: " + a.get(n - 1));
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    HOW CAN I RECOVER A STOLEN BTC/USDT/ETH? HIRE FIXER WALLET RETRIEVAL Never took much time for me to be able to recover a stolen bitcoin worth of $124k. Reaching out to FIXER WALLET RETRIEVAL, one of the trusted and reputable hackers out here can save you the energy of becoming a stranded victim of crypto fraud. Here is how to contact these hackers; Email fixerwalletretrieval@fixer.co.site

  • + 0 comments
    count=0
        for i in range(n):
            
            for j in range(n-1):
                if a[j]>a[j+1]:
                    a[j],a[j+1]=a[j+1],a[j]
                    count+=1
            if count==0:
                break
        print('Array is sorted in',count,'swaps.')
        print('First Element:', a[0])
        print('Last Element:', a[-1])
    
  • + 0 comments

    Python3 solution *Check on the indentation python if name == 'main': n = int(input().strip())

    a = list(map(int, input().rstrip().split()))
    swap = 0
    #Traverse through all the elements in the array
    for i in range(n):
    
        for j in range(n-1):
            #Traverse the array from 0 to n-1
            #Swap the element if the element is found greater than the next element
            if a[j] > a[j+1]:
                swap += 1
                a[j], a[j+1] = a[j+1], a[j]
    
    print(f'Array is sorted in {swap} swaps.')
    print(f'First Element: {a[0]}')
    print(f'Last Element: {a[-1]}')